class-affiliate.php
3 years ago
class-polylang.php
2 years ago
class-review-box.php
2 years ago
class-upgrade-box.php
3 years ago
class-wpml.php
3 years ago
folders.class.php
2 years ago
form.class.php
3 years ago
media.replace.php
2 years ago
plugins.class.php
3 years ago
tree.class.php
2 years ago
class-polylang.php
231 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class Folders PolyLang |
| 4 | * |
| 5 | * @author : Premio <contact@premio.io> |
| 6 | * @license : GPL2 |
| 7 | * */ |
| 8 | |
| 9 | if (! defined('ABSPATH')) { |
| 10 | exit; |
| 11 | } |
| 12 | |
| 13 | class WCP_Folder_PolyLang |
| 14 | { |
| 15 | |
| 16 | /** |
| 17 | * The Name of this plugin. |
| 18 | * |
| 19 | * @var string $active Checking for Plugin is active or not |
| 20 | * @since 1.0.0 |
| 21 | * @access public |
| 22 | */ |
| 23 | private $active; |
| 24 | |
| 25 | /** |
| 26 | * The Name of this plugin. |
| 27 | * |
| 28 | * @var string $poly_lang_term_taxonomy_id Poly Lang taxonomy id |
| 29 | * @since 1.0.0 |
| 30 | * @access public |
| 31 | */ |
| 32 | private $poly_lang_term_taxonomy_id; |
| 33 | |
| 34 | /** |
| 35 | * The Name of this plugin. |
| 36 | * |
| 37 | * @var string $total total posts in taxonomy |
| 38 | * @since 1.0.0 |
| 39 | * @access public |
| 40 | */ |
| 41 | private $total; |
| 42 | |
| 43 | |
| 44 | /** |
| 45 | * Define the core functionality of the plugin. |
| 46 | * |
| 47 | * Set the plugin name and the plugin version that can be used throughout the plugin. |
| 48 | * Load the dependencies, define the locale, and set the hooks for the admin area and |
| 49 | * the public-facing side of the site. |
| 50 | * |
| 51 | * @since 1.0.0 |
| 52 | */ |
| 53 | public function __construct() |
| 54 | { |
| 55 | $this->active = false; |
| 56 | $this->total = 0; |
| 57 | add_action("admin_init", [$this, 'init']); |
| 58 | |
| 59 | }//end __construct() |
| 60 | |
| 61 | |
| 62 | /** |
| 63 | * Filters the taxonomy data |
| 64 | * |
| 65 | * @since 1.0.0 |
| 66 | * @access public |
| 67 | * @return |
| 68 | */ |
| 69 | public function init() |
| 70 | { |
| 71 | global $wpdb, $polylang, $typenow; |
| 72 | $this->active = function_exists("pll_get_post_translations") && function_exists("pll_is_translated_post_type"); |
| 73 | |
| 74 | if ($this->active) { |
| 75 | if (isset($polylang->curlang) && is_object($polylang->curlang)) { |
| 76 | $this->poly_lang_term_taxonomy_id = $polylang->curlang->term_taxonomy_id; |
| 77 | |
| 78 | add_filter('premio_folder_item_in_taxonomy', [$this, 'items_in_taxonomy'], 10, 2); |
| 79 | add_filter('premio_folder_un_categorized_items', [$this, 'un_categorized_items'], 10, 2); |
| 80 | add_filter('premio_folder_all_categorized_items', [$this, 'all_categorized_items'], 10, 2); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | }//end init() |
| 85 | |
| 86 | |
| 87 | /** |
| 88 | * Get total number on taxonomies used in Polylang |
| 89 | * |
| 90 | * @since 1.0.0 |
| 91 | * @access public |
| 92 | * @return $total |
| 93 | */ |
| 94 | public function set_total($post_type) |
| 95 | { |
| 96 | if ($this->active) { |
| 97 | $where = "posts.post_status = 'inherit' OR posts.post_status = 'private'"; |
| 98 | if ($post_type != 'attachment') { |
| 99 | $where = "post_status != 'trash'"; |
| 100 | } |
| 101 | |
| 102 | global $wpdb; |
| 103 | $query = "SELECT COUNT(tmp.ID) FROM |
| 104 | ( |
| 105 | SELECT posts.ID |
| 106 | FROM {$wpdb->posts} AS posts |
| 107 | LEFT JOIN {$wpdb->term_relationships} AS trs |
| 108 | ON posts.ID = trs.object_id |
| 109 | LEFT JOIN {$wpdb->postmeta} AS postmeta |
| 110 | ON (posts.ID = postmeta.post_id AND postmeta.meta_key = '_wp_attached_file') |
| 111 | WHERE posts.post_type = '%s' |
| 112 | AND trs.term_taxonomy_id IN (%s) |
| 113 | AND ({$where}) |
| 114 | GROUP BY posts.ID |
| 115 | ) as tmp"; |
| 116 | $query = $wpdb->prepare($query, [$post_type, $this->poly_lang_term_taxonomy_id]); |
| 117 | $this->total = (int) $wpdb->get_var($query); |
| 118 | }//end if |
| 119 | |
| 120 | }//end set_total() |
| 121 | |
| 122 | |
| 123 | /** |
| 124 | * Check the items in taxonomies |
| 125 | * |
| 126 | * @since 1.0.0 |
| 127 | * @access public |
| 128 | * @return $counter |
| 129 | */ |
| 130 | public function items_in_taxonomy($term_id, $arg=[]) |
| 131 | { |
| 132 | if ($this->active) { |
| 133 | $post_type = isset($arg['post_type']) ? $arg['post_type'] : ""; |
| 134 | $taxonomy = isset($arg['taxonomy']) ? $arg['taxonomy'] : ""; |
| 135 | $where = "posts.post_status = 'inherit' OR posts.post_status = 'private'"; |
| 136 | if ($post_type != 'attachment') { |
| 137 | $where = "post_status != 'trash'"; |
| 138 | } |
| 139 | |
| 140 | global $wpdb; |
| 141 | $term_taxonomy_id = get_term_by('id', (int) $term_id, $taxonomy, OBJECT)->term_taxonomy_id; |
| 142 | $query = "SELECT COUNT(tmp.ID) FROM |
| 143 | ( |
| 144 | SELECT posts.ID FROM {$wpdb->posts} AS posts |
| 145 | LEFT JOIN {$wpdb->term_relationships} AS tr1 |
| 146 | ON (posts.ID = tr1.object_id) |
| 147 | INNER JOIN {$wpdb->term_relationships} AS tr2 |
| 148 | ON (posts.ID = tr2.object_id and tr2.term_taxonomy_id IN (%s)) |
| 149 | LEFT JOIN {$wpdb->postmeta} AS postmeta ON ( posts.ID = postmeta.post_id AND postmeta.meta_key = '_wp_attached_file' ) |
| 150 | WHERE (tr1.term_taxonomy_id IN (%s)) |
| 151 | AND posts.post_type = '%s' |
| 152 | AND (({$where})) |
| 153 | GROUP BY posts.ID |
| 154 | ) as tmp"; |
| 155 | $query = $wpdb->prepare($query, [$term_taxonomy_id, $this->poly_lang_term_taxonomy_id, $post_type]); |
| 156 | $counter = (int) $wpdb->get_var($query); |
| 157 | return $counter ? $counter : 0; |
| 158 | }//end if |
| 159 | |
| 160 | return null; |
| 161 | |
| 162 | }//end items_in_taxonomy() |
| 163 | |
| 164 | |
| 165 | /** |
| 166 | * Check the items in uncategorized taxonomies |
| 167 | * |
| 168 | * @since 1.0.0 |
| 169 | * @access public |
| 170 | * @return $counter |
| 171 | */ |
| 172 | public function un_categorized_items($post_type, $taxonomy) |
| 173 | { |
| 174 | if ($this->active) { |
| 175 | global $wpdb; |
| 176 | $where = "posts.post_status = 'inherit' OR posts.post_status = 'private'"; |
| 177 | if ($post_type != 'attachment') { |
| 178 | $where = "post_status != 'trash'"; |
| 179 | } |
| 180 | |
| 181 | $query = "SELECT COUNT(tmp.ID) FROM |
| 182 | ( |
| 183 | SELECT posts.ID |
| 184 | FROM {$wpdb->posts} AS posts |
| 185 | INNER JOIN {$wpdb->term_relationships} AS tr1 |
| 186 | ON posts.ID = tr1.object_id AND tr1.term_taxonomy_id IN (%s) |
| 187 | INNER JOIN {$wpdb->term_relationships} AS tr2 |
| 188 | ON (tr2.object_id = posts.ID) |
| 189 | JOIN {$wpdb->term_taxonomy} as tx |
| 190 | ON tx.term_taxonomy_id = tr2.term_taxonomy_id AND tx.taxonomy = '%s' |
| 191 | WHERE posts.post_type = '%s' |
| 192 | AND ({$where}) |
| 193 | GROUP BY posts.ID |
| 194 | ) as tmp"; |
| 195 | $query = $wpdb->prepare($query, [$this->poly_lang_term_taxonomy_id, $taxonomy, $post_type]); |
| 196 | $fileInFolder = (int) $wpdb->get_var($query); |
| 197 | $fileInFolder = !($fileInFolder) ? 0 : $fileInFolder; |
| 198 | $this->set_total($post_type); |
| 199 | return ($this->total - $fileInFolder); |
| 200 | }//end if |
| 201 | |
| 202 | return null; |
| 203 | |
| 204 | }//end un_categorized_items() |
| 205 | |
| 206 | |
| 207 | /** |
| 208 | * Check the items in taxonomies |
| 209 | * |
| 210 | * @since 1.0.0 |
| 211 | * @access public |
| 212 | * @return $counter |
| 213 | */ |
| 214 | public function all_categorized_items($post_type) |
| 215 | { |
| 216 | if ($this->active) { |
| 217 | $this->set_total($post_type); |
| 218 | return $this->total; |
| 219 | } |
| 220 | |
| 221 | return null; |
| 222 | |
| 223 | }//end all_categorized_items() |
| 224 | |
| 225 | |
| 226 | }//end class |
| 227 | |
| 228 | if (class_exists('WCP_Folder_PolyLang')) { |
| 229 | $WCP_Folder_PolyLang = new WCP_Folder_PolyLang(); |
| 230 | } |
| 231 |