Guest User

__.php

a guest
Sep 26th, 2021
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.44 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. namespace WPDM\AddOn\ArchivePage\__;
  5.  
  6.  
  7. use WPDM\__\Crypt;
  8. use WPDM\__\Query;
  9. use WPDM\__\Template;
  10. use WPDM\__\UI;
  11.  
  12. class __
  13. {
  14.  
  15.     function __construct()
  16.     {
  17.         add_action("init", [$this, 'getDownloads']);
  18.         //add_action("wp_ajax_get_downloads", [$this, 'getDownloads']);
  19.         //add_action("wp_ajax_nopriv_get_downloads", [$this, 'getDownloads']);
  20.  
  21.         add_action("wp_ajax_wpdm_change_cat_parent", array( $this, 'setRootCategory'));
  22.         add_action("wp_ajax_nopriv_wpdm_change_cat_parent", array( $this, 'setRootCategory'));
  23.     }
  24.  
  25.     /**
  26.      * @usage Fetch Packages
  27.      */
  28.     function getDownloads()
  29.     {
  30.  
  31.         global $wpdb, $current_user;
  32.         if(wpdm_is_ajax() && wpdm_query_var('action') === 'get_downloads') {
  33.             $sparams = Crypt::decrypt(wpdm_query_var('sc_params'), true);
  34.             if (!is_array($sparams)) $sparams = [];
  35.             $params = $sparams;
  36.  
  37.  
  38.  
  39.             $params['template'] = isset($params['template']) && trim($params['template']) !== '' ? $params['template'] : wpdm_valueof($sparams, 'link_template');
  40.             $params['toolbar'] = 0;
  41.             if (!isset($params['paging'])) $params['paging'] = 1;
  42.             if (wpdm_query_var('search') !== '') $params['s'] = wpdm_query_var('search');
  43.             wp_reset_query();
  44.             //Get category from shortcode parameter
  45.             if (isset($params['category'])) {
  46.                 $params['categories'] = $params['category'];
  47.             }
  48.  
  49.             //On the first request, load packages from mentioned featured categories
  50.             if(wpdm_query_var('init', 'int') === 1 && wpdm_valueof($params,'featured_cats') !== '') {
  51.                 $params['categories'] = wpdm_valueof($params,'featured_cats');
  52.                 unset($params['featured_cats']);
  53.             }
  54.  
  55.             //Get category from query var, override shortcode parameter
  56.             if (is_array(wpdm_query_var('category'))) {
  57.                 $params['categories'] = implode(",", wpdm_query_var('category'));
  58.                 $params['operator'] = wpdm_query_var('cat_operator', 'txt');
  59.             } else if (wpdm_query_var('category', 'int') > 0) {
  60.                 $term = get_term(wpdm_query_var('category', 'int'));
  61.                 $params['categories'] = $term->slug;
  62.                 $params['operator'] = wpdm_query_var('cat_operator', 'txt');
  63.             }
  64.  
  65.             if (is_array(wpdm_query_var('tags'))) {
  66.                 $params['tax'] = 'wpdmtag';
  67.                 $params['terms'] = implode(",", wpdm_query_var('tags'));
  68.             }
  69.  
  70.             if(wpdm_query_var('from_date') !== '') {
  71.                 $params['from_date'] = wpdm_query_var('from_date');
  72.             }
  73.  
  74.             if(wpdm_query_var('to_date') !== '') {
  75.                 $params['to_date'] = wpdm_query_var('to_date');
  76.             }
  77.  
  78.             if ($params['template'] === '') $params['template'] = 'link-template-default.php';
  79.             if (!isset($params['cols'])) $params['cols'] = 1;
  80.  
  81.             $params['toolbar'] = 0;
  82.             wp_send_json($this->packages($params));
  83.             die();
  84.         }
  85.     }
  86.  
  87.     /**
  88.      * @param array $params
  89.      * @return array
  90.      */
  91.     function packages($params = array('items_per_page' => 10, 'title' => false, 'desc' => false, 'orderby' => 'date', 'order' => 'DESC', 'paging' => false, 'page_numbers' => true, 'toolbar' => 1, 'template' => '', 'cols' => 3, 'colspad' => 2, 'colsphone' => 1, 'tags' => '', 'categories' => '', 'year' => '', 'month' => '', 's' => '', 'css_class' => 'wpdm_packages', 'scid' => '', 'async' => 1, 'tax' => '', 'terms' => ''))
  92.     {
  93.         global $current_user, $post;
  94.  
  95.         static $wpdm_packages = 0;
  96.  
  97.         if (isset($params['login']) && $params['login'] == 1 && !is_user_logged_in())
  98.             return WPDM()->shortCode->loginForm($params);
  99.  
  100.         $wpdm_packages++;
  101.  
  102.         $scparams = $params;
  103.  
  104.         $defaults = array('author' => '', 'author_name' => '', 'items_per_page' => 10, 'title' => false, 'desc' => false, 'orderby' => 'date', 'order' => 'DESC', 'paging' => false, 'page_numbers' => true, 'toolbar' => 1, 'template' => 'link-template-panel', 'cols' => 3, 'colspad' => 2, 'colsphone' => 1, 'css_class' => 'wpdm_packages', 'scid' => 'wpdm_packages_' . $wpdm_packages, 'async' => 1, 'include_children' => 0);
  105.         $params = shortcode_atts($defaults, $params, 'wpdm_packages');
  106.  
  107.         if (is_array($params))
  108.             extract($params);
  109.  
  110.         if (!isset($items_per_page) || $items_per_page < 1) $items_per_page = 10;
  111.  
  112.         $cwd_class = "col-lg-" . (int)(12 / $cols);
  113.         $cwdsm_class = "col-md-" . (int)(12 / $colspad);
  114.         $cwdxs_class = "col-" . (int)(12 / $colsphone);
  115.  
  116.         if (isset($orderby) && !isset($order_field)) $order_field = $orderby;
  117.         $order_field = isset($order_field) ? $order_field : 'date';
  118.         $order_field = isset($_REQUEST['orderby']) ? esc_attr($_REQUEST['orderby']) : $order_field;
  119.         $order = isset($order) ? $order : 'desc';
  120.         $order = isset($_REQUEST['order']) ? esc_attr($_REQUEST['order']) : $order;
  121.         $cp = wpdm_query_var('cp', 'num');
  122.         if (!$cp) $cp = 1;
  123.  
  124.         $query = new Query();
  125.         $query->items_per_page(wpdm_valueof($params, 'items_per_page', 10));
  126.         $query->paged($cp);
  127.         $query->sort($order_field, $order);
  128.  
  129.         foreach ($scparams as $key => $value) {
  130.             if (method_exists($query, $key) && !in_array($key, ['categories', 'tags', 'from_date', 'to_date'])) {
  131.                 $query->$key($value);
  132.             }
  133.         }
  134.  
  135.         /**
  136.          * Process "categories" parameter
  137.          * Usually values are category slug(s), users may use id(s) too
  138.          * If users uses slugs, convert slugs into ids
  139.          **/
  140.         if(wpdm_valueof($scparams, 'categories') !== '') {
  141.             $categories = wpdm_valueof($scparams, 'categories');
  142.             $categories = explode(",", $categories);
  143.             /**
  144.              * Convert slugs to ID
  145.              */
  146.             foreach ($categories as &$cat) {
  147.                 if (!is_numeric($cat) && $cat !== '') {
  148.                     $catObj = get_term_by('slug', $cat, 'wpdmcategory');
  149.                     $cat = $catObj->term_id;
  150.                 }
  151.             }
  152.             $operator = wpdm_valueof($scparams, 'operator', ['default' => 'IN']);
  153.             if(count($categories) < 2) $operator = 'IN';
  154.             $include_children = wpdm_valueof($scparams, 'include_children', ['default' => false]);
  155.             $query->categories($categories, 'term_id', $operator, $include_children);
  156.         }
  157.  
  158.         if(wpdm_valueof($scparams, 'from_date') !== '') {
  159.             $query->from_date(wpdm_query_var('from_date'), (wpdm_query_var('date_col') === 'update'));
  160.         }
  161.  
  162.         if(wpdm_valueof($scparams, 'to_date') !== '') {
  163.             $query->to_date(wpdm_query_var('from_date'), (wpdm_query_var('date_col') === 'update'));
  164.         }
  165.  
  166.         if(wpdm_valueof($scparams, 'tags') !== '') {
  167.             $tags = wpdm_valueof($scparams, 'tags');
  168.             $operator = wpdm_valueof($scparams, 'operator', ['default' => 'IN']);
  169.             $query->tags($tags, 'slug', $operator);
  170.         }
  171.  
  172.         if (wpdm_query_var('skw', 'txt') != '') {
  173.             $query->s(wpdm_query_var('skw', 'txt'));
  174.         }
  175.  
  176.         if(wpdm_valueof($scparams, 'tax') !== '') {
  177.             $_terms = explode("|", wpdm_valueof($scparams, 'terms'));
  178.             $taxos = explode("|", wpdm_valueof($scparams, 'tax'));
  179.             foreach ($taxos as $index => $_taxo) {
  180.                 $terms = wpdm_valueof($_terms, $index);
  181.                 $terms = explode(",", $terms);
  182.                 if(count($terms) > 0) {
  183.  
  184.                     $query->taxonomy($_taxo, $terms);
  185.                 }
  186.             }
  187.         }
  188.  
  189.  
  190.         $tax_relation = wpdm_valueof($scparams, 'relation');
  191.         $tax_query = wpdm_valueof($query->params, 'tax_query');
  192.         if(isset($tax_query['relation'])) unset($tax_query['relation']);
  193.  
  194.         //If multiple taxonomy and tax relation is not mentioned, show intersect result
  195.         if(is_array($tax_query) && count($tax_query) > 1 && !$tax_relation)
  196.             $tax_relation = 'AND';
  197.         else
  198.             $tax_relation = 'OR';
  199.  
  200.         if($tax_relation)
  201.             $query->taxonomy_relation($tax_relation);
  202.  
  203.         if (get_option('_wpdm_hide_all', 0) == 1) {
  204.             $query->meta("__wpdm_access", '"guest"');
  205.  
  206.             if (is_user_logged_in()) {
  207.                 foreach ($current_user->roles as $role) {
  208.                     $query->meta("__wpdm_access", $role);
  209.                 }
  210.                 $query->meta_relation('OR');
  211.             }
  212.         }
  213.  
  214.         if (isset($scparams['year']) || isset($scparams['month']) || isset($scparams['day'])) {
  215.  
  216.             if (isset($scparams['day'])) {
  217.                 $day = ($scparams['day'] == 'today') ? date('d') : $scparams['day'];
  218.                 $query->filter('day', $day);
  219.             }
  220.  
  221.             if (isset($scparams['month'])) {
  222.                 $month = ($scparams['month'] == 'this') ? date('Ym') : $scparams['month'];
  223.                 $query->filter('m', $month);
  224.             }
  225.  
  226.             if (isset($scparams['year'])) {
  227.                 $year = ($scparams['year'] == 'this') ? date('Y') : $scparams['year'];
  228.                 $query->filter('year', $year);
  229.             }
  230.  
  231.             if (isset($scparams['week'])) {
  232.                 $week = ($scparams['week'] == 'this') ? date('W') : $scparams['week'];
  233.                 $query->filter('week', $week);
  234.             }
  235.         }
  236.         //wpdmdd($query->params);
  237.         $query->post_status('publish');
  238.         $query->process();
  239.         $total = $query->count;
  240.         $packages = $query->packages();
  241.  
  242.  
  243.         $pages = ceil($total / $items_per_page);
  244.         $page = isset($_REQUEST['cp']) ? (int)$_REQUEST['cp'] : 1;
  245.         $start = ($page - 1) * $items_per_page;
  246.  
  247.  
  248.         $html = '';
  249.  
  250.         foreach ($packages as $pack){
  251.             $pack = (array)$pack;
  252.             $repeater = "<div class='{$cwd_class} {$cwdsm_class} {$cwdxs_class}'>" . WPDM()->package->fetchTemplate(wpdm_valueof($scparams, 'template', 'link-template-default.php'), $pack) . "</div>";
  253.             $html .= $repeater;
  254.  
  255.         }
  256.  
  257.         if($html === '') $html = UI::div(UI::div(esc_attr__( 'No downloads found!', WPDM_TEXT_DOMAIN ), "alert alert-info"), "col-md-12");
  258.  
  259.         $html = "<div class='row'>{$html}</div>";
  260.  
  261.         $_scparams = Crypt::encrypt($scparams);
  262.  
  263.         /*
  264.         $pagination = "";
  265.  
  266.         global $post;
  267.  
  268.         $burl = get_permalink();
  269.         $sap = get_option('permalink_structure') ? '?' : '&';
  270.         $burl = $burl . $sap;
  271.         if (isset($_REQUEST['p']) && $_REQUEST['p'] != '') $burl .= 'p=' . esc_attr($_REQUEST['p']) . '&';
  272.         if (isset($_REQUEST['src']) && $_REQUEST['src'] != '') $burl .= 'src=' . esc_attr($_REQUEST['src']) . '&';
  273.         $orderby = isset($_REQUEST['orderby']) ? esc_attr($_REQUEST['orderby']) : 'date';
  274.         $order = ucfirst($order);
  275.  
  276.         $order_field = " " . __(ucwords(str_replace("_", " ", $order_field)), "wpdmpro");
  277.         $ttitle = __("Title", "download-manager");
  278.         $tdls = __("Downloads", "download-manager");
  279.         $tcdate = __("Publish Date", "download-manager");
  280.         $tudate = __("Update Date", "download-manager");
  281.         $tasc = __("Asc", "download-manager");
  282.         $tdsc = __("Desc", "download-manager");
  283.         $tsrc = __("Search", "download-manager");
  284.         $ord = __("Order", "download-manager");
  285.         $orderby_label = __("Order By", "download-manager");
  286.  
  287.         $css_class = isset($scparams['css_class']) ? sanitize_text_field($scparams['css_class']) : '';
  288.         $desc = isset($scparams['desc']) ? sanitize_text_field($scparams['desc']) : '';
  289.  
  290.         $title = isset($title) && $title != '' && $total > 0 ? "<h3>$title</h3>" : "";
  291.  
  292.  
  293.         $toolbar = isset($toolbar) ? $toolbar : 0;
  294.  
  295.         ob_start();
  296.         include Template::locate("shortcodes/packages.php", WPDM_TPL_FALLBACK);
  297.         $content = ob_get_clean();
  298.         $scparams['paging'] = 0;
  299.         $content = WPDM()->package->shortCodes->packages($scparams); //*/
  300.         return ['html' => $html, 'current' => $page, 'last' => $pages, 'params' => $query->params];
  301.     }
  302.  
  303.  
  304.     /**
  305.      * @usage Breadcrumb helper, set root category
  306.      */
  307.     function setRootCategory(){
  308.         $cat_id = wpdm_query_var('cat_id', '');
  309.         $result['type'] = 'failed';
  310.         if(is_numeric($cat_id)) {
  311.             $result['type'] = 'success';
  312.  
  313.             $parents = rtrim($this->getCustomCategoryParents($cat_id,'wpdmcategory',false,'>',false),'>');
  314.             $temp = explode('>', $parents);
  315.             //print_r($temp);
  316.             $count = count($temp);
  317.             $str = "";
  318.             for($i = 1; $i<=$count ; $i++){
  319.                 if($i == $count) {
  320.                     $str .= "{$temp[$i-1]}";
  321.                 }
  322.                 else {
  323.  
  324.                     $parent = get_term_by('name', $temp[$i-1], 'wpdmcategory');
  325.                     //print_r($parent);
  326.                     $link = get_term_link($parent);
  327.                     //print_r($link);
  328.                     $a = "<a class='wpdm-cat-link2' rel='{$parent->term_id}' test_rel='{$parent->term_id}' title='{$parent->description}' href='$link'>{$parent->name}</a><span class=\"bcsep\"></span>";
  329.                     $str .= $a;
  330.                 }
  331.             }
  332.             $result['parent'] = $str;
  333.  
  334.         }
  335.  
  336.         if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
  337.             $result = json_encode($result);
  338.             echo $result;
  339.         }
  340.         else {
  341.             header("Location: ".$_SERVER["HTTP_REFERER"]);
  342.         }
  343.  
  344.         die();
  345.     }
  346.  
  347.     /**
  348.      * @param $id
  349.      * @param bool|false $taxonomy
  350.      * @param bool|false $link
  351.      * @param string $separator
  352.      * @param bool|false $nicename
  353.      * @param array $visited
  354.      * @return array|mixed|null|object|string|\WP_Error
  355.      */
  356.     function getCustomCategoryParents( $id, $taxonomy = false, $link = false, $separator = '/', $nicename = false, $visited = array() ) {
  357.  
  358.         if(!($taxonomy && is_taxonomy_hierarchical( $taxonomy )))
  359.             return '';
  360.  
  361.         $chain = '';
  362.         // $parent = get_category( $id );
  363.         $parent = get_term( $id, $taxonomy);
  364.         if ( is_wp_error( $parent ) )
  365.             return $parent;
  366.  
  367.         if ( $nicename )
  368.             $name = $parent->slug;
  369.         else
  370.             $name = $parent->name;
  371.  
  372.         if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
  373.             $visited[] = $parent->parent;
  374.             // $chain .= get_category_parents( $parent->parent, $link, $separator, $nicename, $visited );
  375.             $chain .= $this->GetCustomCategoryParents( $parent->parent, $taxonomy, $link, $separator, $nicename, $visited );
  376.         }
  377.  
  378.         if ( $link ) {
  379.             // $chain .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;
  380.             $chain .= '<a href="' . esc_url( get_term_link( (int) $parent->term_id, $taxonomy ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s","wpdm-archive-page" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;
  381.         } else {
  382.             $chain .= $name.$separator;
  383.         }
  384.         return $chain;
  385.     }
  386.  
  387. }
Advertisement
Add Comment
Please, Sign In to add comment