views
2 years ago
Category.php
2 years ago
CategoryController.php
5 years ago
Shortcodes.php
2 years ago
Shortcodes.php
58 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace WPDM\Category; |
| 5 | |
| 6 | |
| 7 | use WPDM\__\Template; |
| 8 | use WPDM\__\Query; |
| 9 | |
| 10 | class Shortcodes |
| 11 | { |
| 12 | function __construct() |
| 13 | { |
| 14 | add_shortcode("wpdm_category", [$this, 'listPackages']); |
| 15 | add_shortcode('wpdm_category_link', [$this, 'categoryLink']); |
| 16 | } |
| 17 | |
| 18 | function listPackages($params = array('id' => '', 'operator' => 'IN', 'items_per_page' => 10, 'title' => false, 'desc' => false, 'orderby' => 'create_date', 'order' => 'desc', 'paging' => false, 'toolbar' => 1, 'template' => '', 'cols' => 3, 'colspad' => 2, 'colsphone' => 1, 'morelink' => 1)) |
| 19 | { |
| 20 | if(!isset($params['id'])) return __('No category selected!', 'download-manager'); |
| 21 | $params['categories'] = $params['id']; |
| 22 | $params['catsc'] = 1; |
| 23 | unset($params['id']); |
| 24 | |
| 25 | if(!in_array(wpdm_valueof($params, 'cat_field'), ['id', 'term_id'])) { |
| 26 | $ids = []; |
| 27 | foreach (explode(",", $params['categories']) as $slug) { |
| 28 | $term = get_term_by('slug', $slug, 'wpdmcategory'); |
| 29 | if($term) |
| 30 | $ids[] = $term->term_id; |
| 31 | } |
| 32 | if(count($ids) > 0) { |
| 33 | $params['categories'] = implode(",", $ids); |
| 34 | $params['cat_field'] = 'id'; |
| 35 | } |
| 36 | } |
| 37 | return WPDM()->package->shortCodes->packages($params); |
| 38 | |
| 39 | } |
| 40 | |
| 41 | function categoryLink($params) |
| 42 | { |
| 43 | $category = new Category((int)wpdm_valueof($params, 'id')); |
| 44 | if(!$category->ID) return ''; |
| 45 | $cat = (array)$category; |
| 46 | $_cat = []; |
| 47 | foreach ($cat as $key => $value) { |
| 48 | if(!is_array($value)) |
| 49 | $_cat["{{{$key}}}"] = $value; |
| 50 | } |
| 51 | $_cat['{{icon}}'] = $category->icon ? "<img src='{$category->icon}' alt='{$category->name}' />" : ""; |
| 52 | $template = isset($params['template']) && $params['template'] != '' ? $params['template'] : 'category-link-shortcode.php'; |
| 53 | $template = wp_basename($template); |
| 54 | return Template::output($template, $_cat, __DIR__.'/views'); |
| 55 | } |
| 56 | |
| 57 | } |
| 58 |