class-affiliate.php
6 years ago
class-review-box.php
6 years ago
folders.class.php
6 years ago
form.class.php
6 years ago
plugin.updates.php
6 years ago
tree.class.php
6 years ago
folders.class.php
2288 lines
| 1 | <?php |
| 2 | defined('ABSPATH') or wp_die('Nope, not accessing this'); |
| 3 | |
| 4 | class WCP_Folders |
| 5 | { |
| 6 | |
| 7 | private static $instance; |
| 8 | |
| 9 | public $license_key_data = null; |
| 10 | |
| 11 | private static $folders; |
| 12 | |
| 13 | public $total_folders = 0; |
| 14 | |
| 15 | private static $postIds; |
| 16 | |
| 17 | public function __construct() |
| 18 | { |
| 19 | spl_autoload_register(array($this, 'autoload')); |
| 20 | add_action('init', array($this, 'create_folder_terms'), 15); |
| 21 | add_action('admin_init', array($this, 'folders_register_settings')); |
| 22 | add_action('admin_menu', array($this, 'admin_menu'), 10000); |
| 23 | add_action('admin_enqueue_scripts', array($this, 'folders_admin_styles')); |
| 24 | add_action('admin_enqueue_scripts', array($this, 'folders_admin_scripts')); |
| 25 | add_filter('plugin_action_links_' . WCP_FOLDERS_PLUGIN_BASE, [$this, 'plugin_action_links']); |
| 26 | add_action('admin_footer', array($this, 'admin_footer')); |
| 27 | add_action('parse_tax_query', array($this, 'taxonomy_archive_exclude_children')); |
| 28 | |
| 29 | /* Save Data */ |
| 30 | add_action('wp_ajax_wcp_add_new_folder', array($this, 'wcp_add_new_folder')); |
| 31 | /* Update Data */ |
| 32 | add_action('wp_ajax_wcp_update_folder', array($this, 'wcp_update_folder')); |
| 33 | /* Remove Data */ |
| 34 | add_action('wp_ajax_wcp_remove_folder', array($this, 'wcp_remove_folder')); |
| 35 | /* Save State Data */ |
| 36 | add_action('wp_ajax_save_wcp_folder_state', array($this, 'save_wcp_folder_state')); |
| 37 | /* Save State Data */ |
| 38 | add_action('wp_ajax_wcp_save_parent_data', array($this, 'wcp_save_parent_data')); |
| 39 | /* Update Parent Data */ |
| 40 | add_action('wp_ajax_wcp_update_parent_information', array($this, 'wcp_update_parent_information')); |
| 41 | /* Update Parent Data */ |
| 42 | add_action('wp_ajax_wcp_save_folder_order', array($this, 'wcp_save_folder_order')); |
| 43 | /* Update Parent Data */ |
| 44 | add_action('wp_ajax_wcp_mark_un_mark_folder', array($this, 'wcp_mark_un_mark_folder')); |
| 45 | /* Update Parent Data */ |
| 46 | add_action('wp_ajax_wcp_change_post_folder', array($this, 'wcp_change_post_folder')); |
| 47 | /* Update Parent Data */ |
| 48 | add_action('wp_ajax_wcp_change_multiple_post_folder', array($this, 'wcp_change_multiple_post_folder')); |
| 49 | /* Update Parent Data */ |
| 50 | add_action('wp_ajax_wcp_remove_post_folder', array($this, 'wcp_remove_post_folder')); |
| 51 | /* Update width Data */ |
| 52 | add_action('wp_ajax_wcp_change_post_width', array($this, 'wcp_change_post_width')); |
| 53 | /* Update width Data */ |
| 54 | add_action('wp_ajax_wcp_change_folder_display_status', array($this, 'wcp_change_folder_display_status')); |
| 55 | /* Update width Data */ |
| 56 | add_action('wp_ajax_wcp_change_all_status', array($this, 'wcp_change_all_status')); |
| 57 | self::$folders = 10; |
| 58 | /* Send message on plugin deactivate */ |
| 59 | add_action( 'wp_ajax_folder_plugin_deactivate', array( $this, 'folder_plugin_deactivate' ) ); |
| 60 | /* Send message on owner */ |
| 61 | add_action( 'wp_ajax_wcp_folder_send_message_to_owner', array( $this, 'wcp_folder_send_message_to_owner' ) ); |
| 62 | /* Get default list */ |
| 63 | add_action( 'wp_ajax_wcp_get_default_list', array( $this, 'wcp_get_default_list' ) ); |
| 64 | /* Get default list */ |
| 65 | add_action( 'wp_ajax_get_folders_default_list', array( $this, 'get_folders_default_list' ) ); |
| 66 | /* Auto select folder for new page, post */ |
| 67 | add_action('new_to_auto-draft', array($this, 'new_to_auto_draft'), 10); |
| 68 | /* for media */ |
| 69 | add_action('restrict_manage_posts', array($this, 'output_list_table_filters'), 10, 2); |
| 70 | add_filter('pre_get_posts', array($this, 'filter_attachments_list')); |
| 71 | add_action('wp_enqueue_media', array($this, 'output_backbone_view_filters')); |
| 72 | add_filter('ajax_query_attachments_args', array($this, 'filter_attachments_grid')); |
| 73 | add_filter('add_attachment', array($this, 'save_media_terms')); |
| 74 | |
| 75 | /* to filter un assigned items*/ |
| 76 | add_filter('pre_get_posts', array($this, 'filter_record_list')); |
| 77 | add_filter('pre-upload-ui', array($this, 'show_dropdown_on_media_screen')); |
| 78 | add_action('add_attachment', array($this, 'add_attachment_category')); |
| 79 | |
| 80 | /* check for default folders */ |
| 81 | add_filter('pre_get_posts', array($this, 'check_for_default_folders')); |
| 82 | |
| 83 | $options = get_option("folders_settings"); |
| 84 | |
| 85 | $options = is_array($options)?$options:array(); |
| 86 | |
| 87 | if (in_array("post", $options)) { |
| 88 | add_filter('manage_posts_columns', array($this, 'wcp_manage_columns_head')); |
| 89 | add_action('manage_posts_custom_column', array($this, 'wcp_manage_columns_content'), 10, 2); |
| 90 | } |
| 91 | |
| 92 | if (in_array("page", $options)) { |
| 93 | add_filter('manage_page_posts_columns', array($this, 'wcp_manage_columns_head')); |
| 94 | add_action('manage_page_posts_custom_column', array($this, 'wcp_manage_columns_content'), 10, 2); |
| 95 | } |
| 96 | |
| 97 | if (in_array("attachment", $options)) { |
| 98 | add_filter('manage_media_columns', array($this, 'wcp_manage_columns_head')); |
| 99 | add_action('manage_media_custom_column', array($this, 'wcp_manage_columns_content'), 10, 2); |
| 100 | } |
| 101 | |
| 102 | foreach ($options as $option) { |
| 103 | if ($option != "post" && $option != "page" && $option != "attachment") { |
| 104 | add_filter('manage_edit-'.$option.'_columns', array($this, 'wcp_manage_columns_head'), 99999); |
| 105 | add_action('manage_'.$option.'_posts_custom_column', array($this, 'wcp_manage_columns_content'), 2, 2); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | add_action("wp_ajax_folder_update_status", array($this, 'folder_update_status')); |
| 110 | } |
| 111 | |
| 112 | public function check_for_default_folders() { |
| 113 | global $typenow, $current_screen; |
| 114 | $isAjax = (defined('DOING_AJAX') && DOING_AJAX)?1:0; |
| 115 | $options = get_option('folders_settings'); |
| 116 | $options = (empty($options) || !is_array($options))?array():$options; |
| 117 | if(!$isAjax && in_array($typenow, $options) && (isset($current_screen->base) && ($current_screen->base == "edit" || ($current_screen->base == "upload")))) { |
| 118 | $default_folders = get_option('default_folders'); |
| 119 | $default_folders = (empty($default_folders) || !is_array($default_folders))?array():$default_folders; |
| 120 | if ($typenow == "attachment") { |
| 121 | $admin_url = admin_url("upload.php?post_type=attachment&media_folder="); |
| 122 | if(!isset($_REQUEST['media_folder'])) { |
| 123 | if(isset($default_folders[$typenow]) && !empty($default_folders[$typenow])) { |
| 124 | $admin_url .= $default_folders[$typenow]; |
| 125 | wp_redirect($admin_url); |
| 126 | exit; |
| 127 | } |
| 128 | } |
| 129 | } else { |
| 130 | $admin_url = admin_url("edit.php?post_type=" . $typenow); |
| 131 | if (isset($_GET['s']) && !empty($_GET['s'])) { |
| 132 | $admin_url .= "&s=" . $_GET['s']; |
| 133 | } |
| 134 | $post_type = self::get_custom_post_type($typenow); |
| 135 | $admin_url .= "&{$post_type}="; |
| 136 | if(!isset($_REQUEST[$post_type])) { |
| 137 | if(isset($default_folders[$typenow]) && !empty($default_folders[$typenow])) { |
| 138 | $admin_url .= $default_folders[$typenow]; |
| 139 | wp_redirect($admin_url); |
| 140 | exit; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | // echo "2121"; die; |
| 146 | } |
| 147 | |
| 148 | public function folder_update_status() { |
| 149 | if(!empty($_REQUEST['nonce']) && wp_verify_nonce($_REQUEST['nonce'], 'folder_update_status')) { |
| 150 | $status = filter_input(INPUT_POST, 'status', FILTER_SANITIZE_STRING); |
| 151 | $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_STRING); |
| 152 | update_option("folder_update_message", 2); |
| 153 | if($status == 1) { |
| 154 | $url = 'https://go.premio.io/api/update.php?email='.$email.'&plugin=folders'; |
| 155 | $handle = curl_init(); |
| 156 | curl_setopt($handle, CURLOPT_URL, $url); |
| 157 | curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); |
| 158 | $response = curl_exec($handle); |
| 159 | curl_close($handle); |
| 160 | } |
| 161 | } |
| 162 | echo "1"; |
| 163 | die; |
| 164 | } |
| 165 | |
| 166 | public function add_attachment_category($post_ID) |
| 167 | { |
| 168 | if(self::is_for_this_post_type('attachment') || self::is_for_this_post_type('media')) { |
| 169 | $folder_id = isset($_REQUEST["folder_for_media"]) ? $_REQUEST["folder_for_media"] : null; |
| 170 | if (!is_null($folder_id)) { |
| 171 | $folder_id = (int)$folder_id; |
| 172 | $folder_id = self::sanitize_options($folder_id, "int"); |
| 173 | if ($folder_id > 0) { |
| 174 | $post_type = self::get_custom_post_type("attachment"); |
| 175 | $term = get_term($folder_id); |
| 176 | if(!empty($term) && isset($term->slug)) { |
| 177 | wp_set_object_terms($post_ID, $term->slug, $post_type ); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | public function show_dropdown_on_media_screen() { |
| 185 | if(self::is_for_this_post_type('attachment')) { |
| 186 | $post_type = self::get_custom_post_type('attachment'); |
| 187 | if(!class_exists('WCP_Tree')) { |
| 188 | $files = array( |
| 189 | 'WCP_Tree' => WCP_DS . "includes" . WCP_DS . "tree.class.php" |
| 190 | ); |
| 191 | |
| 192 | foreach ($files as $file) { |
| 193 | if (file_exists(dirname(dirname(__FILE__)) . $file)) { |
| 194 | include_once dirname(dirname(__FILE__)) . $file; |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | $options = WCP_Tree::get_folder_option_data($post_type); |
| 199 | ?><p class="attachments-category"><?php esc_html_e("Select a folder (Optional)", WCP_FOLDER) ?><br/></p> |
| 200 | <p> |
| 201 | <select name="folder_for_media" class="folder_for_media"><option value="-1">- <?php esc_html_e('Uncategorized', WCP_FOLDER) ?></option><?php echo $options ?></select> |
| 202 | </p> |
| 203 | <?php |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | public function wcp_hide_folders() |
| 208 | { |
| 209 | $response = array(); |
| 210 | $response['status'] = 0; |
| 211 | $response['error'] = 0; |
| 212 | $response['data'] = array(); |
| 213 | $response['message'] = ""; |
| 214 | $postData = filter_input_array(INPUT_POST); |
| 215 | $errorCounter = 0; |
| 216 | if (!isset($postData['status']) || empty($postData['status'])) { |
| 217 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 218 | $errorCounter++; |
| 219 | } else if (!isset($postData['type']) || empty($postData['type'])) { |
| 220 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 221 | $errorCounter++; |
| 222 | } else if (!isset($postData['nonce']) || empty($postData['nonce'])) { |
| 223 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 224 | $errorCounter++; |
| 225 | } else if ($postData['type'] == "page" && !current_user_can("edit_pages")) { |
| 226 | $response['message'] = esc_html__("You have not permission to update width", WCP_FOLDER); |
| 227 | $errorCounter++; |
| 228 | } else if ($postData['type'] != "page" && !current_user_can("edit_posts")) { |
| 229 | $response['message'] = esc_html__("You have not permission to update width", WCP_FOLDER); |
| 230 | $errorCounter++; |
| 231 | } else { |
| 232 | $type = self::sanitize_options($postData['type']); |
| 233 | $nonce = self::sanitize_options($postData['nonce']); |
| 234 | if(!wp_verify_nonce($nonce, 'wcp_folder_nonce_'.$type)) { |
| 235 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 236 | $errorCounter++; |
| 237 | } |
| 238 | } |
| 239 | if ($errorCounter == 0) { |
| 240 | $type = self::sanitize_options($postData['type']); |
| 241 | $status = self::sanitize_options($postData['status']); |
| 242 | $optionName = "wcp_folder_display_status_" . $type; |
| 243 | update_option($optionName, $status); |
| 244 | $response['status'] = 1; |
| 245 | } |
| 246 | echo json_encode($response); |
| 247 | die; |
| 248 | } |
| 249 | |
| 250 | public function wcp_change_folder_display_status() |
| 251 | { |
| 252 | $response = array(); |
| 253 | $response['status'] = 0; |
| 254 | $response['error'] = 0; |
| 255 | $response['data'] = array(); |
| 256 | $response['message'] = ""; |
| 257 | $postData = filter_input_array(INPUT_POST); |
| 258 | $errorCounter = 0; |
| 259 | if (!isset($postData['status']) || empty($postData['status'])) { |
| 260 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 261 | $errorCounter++; |
| 262 | } else if (!isset($postData['type']) || empty($postData['type'])) { |
| 263 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 264 | $errorCounter++; |
| 265 | } else if (!isset($postData['nonce']) || empty($postData['nonce'])) { |
| 266 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 267 | $errorCounter++; |
| 268 | } else if ($postData['type'] == "page" && !current_user_can("edit_pages")) { |
| 269 | $response['message'] = esc_html__("You have not permission to update width", WCP_FOLDER); |
| 270 | $errorCounter++; |
| 271 | } else if ($postData['type'] != "page" && !current_user_can("edit_posts")) { |
| 272 | $response['message'] = esc_html__("You have not permission to update width", WCP_FOLDER); |
| 273 | $errorCounter++; |
| 274 | } else { |
| 275 | $type = self::sanitize_options($postData['type']); |
| 276 | $nonce = self::sanitize_options($postData['nonce']); |
| 277 | if(!wp_verify_nonce($nonce, 'wcp_folder_nonce_'.$type)) { |
| 278 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 279 | $errorCounter++; |
| 280 | } |
| 281 | } |
| 282 | if ($errorCounter == 0) { |
| 283 | $type = self::sanitize_options($postData['type']); |
| 284 | $width = self::sanitize_options($postData['status']); |
| 285 | $optionName = "wcp_dynamic_display_status_" . $type; |
| 286 | update_option($optionName, $width); |
| 287 | $response['status'] = 1; |
| 288 | } |
| 289 | echo json_encode($response); |
| 290 | die; |
| 291 | } |
| 292 | |
| 293 | public function wcp_remove_post_folder() { |
| 294 | $response = array(); |
| 295 | $response['status'] = 0; |
| 296 | $response['error'] = 0; |
| 297 | $response['data'] = array(); |
| 298 | $response['message'] = ""; |
| 299 | $postData = filter_input_array(INPUT_POST); |
| 300 | $errorCounter = 0; |
| 301 | if (!isset($postData['post_id']) || empty($postData['post_id'])) { |
| 302 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 303 | $errorCounter++; |
| 304 | } else if (!isset($postData['type']) || empty($postData['type'])) { |
| 305 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 306 | $errorCounter++; |
| 307 | } else if (!isset($postData['nonce']) || empty($postData['nonce'])) { |
| 308 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 309 | $errorCounter++; |
| 310 | } else if(!wp_verify_nonce($postData['nonce'], 'wcp_folder_nonce_'.$postData['type'])) { |
| 311 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 312 | $errorCounter++; |
| 313 | } |
| 314 | if ($errorCounter == 0) { |
| 315 | $type = self::sanitize_options($postData['type']); |
| 316 | $post_id = self::sanitize_options($postData['post_id']); |
| 317 | |
| 318 | $post_id = explode(",", $post_id); |
| 319 | |
| 320 | $taxonomy = self::get_custom_post_type($type); |
| 321 | |
| 322 | foreach($post_id as $id) { |
| 323 | if(!empty($id) && is_numeric($id) && $id > 0) { |
| 324 | wp_delete_object_term_relationships($id, $taxonomy); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | $response['status'] = 1; |
| 329 | } |
| 330 | echo json_encode($response); |
| 331 | die; |
| 332 | // wp_delete_object_term_relationships |
| 333 | } |
| 334 | |
| 335 | public function wcp_get_default_list() { |
| 336 | $post_type = 'attachment'; |
| 337 | |
| 338 | $total_posts = wp_count_posts($post_type)->inherit;; |
| 339 | |
| 340 | $empty_items = self::get_total_empty_posts('attachment'); |
| 341 | |
| 342 | $post_type = self::get_custom_post_type($post_type); |
| 343 | $terms_data = WCP_Tree::get_full_tree_data($post_type); |
| 344 | $taxonomies = self::get_terms_hierarchical('media_folder'); |
| 345 | |
| 346 | $response = array( |
| 347 | 'status' => 1, |
| 348 | 'data' => $terms_data, |
| 349 | 'total_items' => $total_posts, |
| 350 | 'taxonomies' => $taxonomies, |
| 351 | 'empty_items' => $empty_items |
| 352 | ); |
| 353 | echo json_encode($response); |
| 354 | die; |
| 355 | } |
| 356 | |
| 357 | function get_folders_default_list() { |
| 358 | $postData = filter_input_array(INPUT_POST); |
| 359 | |
| 360 | $post_type = $postData['type']; |
| 361 | |
| 362 | if($post_type != 'attachment') { |
| 363 | $total_posts = self::get_total_posts($post_type); |
| 364 | } else { |
| 365 | $total_posts = wp_count_posts($post_type)->inherit; |
| 366 | } |
| 367 | |
| 368 | $empty_items = self::get_total_empty_posts($post_type); |
| 369 | |
| 370 | $post_type = self::get_custom_post_type($post_type); |
| 371 | |
| 372 | $terms_data = WCP_Tree::get_full_tree_data($post_type); |
| 373 | |
| 374 | $response = array( |
| 375 | 'status' => 1, |
| 376 | 'data' => $terms_data, |
| 377 | 'total_items' => $total_posts, |
| 378 | 'empty_items' => $empty_items |
| 379 | ); |
| 380 | echo json_encode($response); |
| 381 | die; |
| 382 | |
| 383 | } |
| 384 | |
| 385 | function save_media_terms( $post_id ) { |
| 386 | if ( wp_is_post_revision( $post_id ) ) { |
| 387 | return; |
| 388 | } |
| 389 | $post = get_post($post_id); |
| 390 | if($post->post_type !== 'attachment') { |
| 391 | return; |
| 392 | } |
| 393 | $post_type = self::get_custom_post_type('attachment'); |
| 394 | $selected_folder = get_option("selected_{$post_type}_folder"); |
| 395 | if($selected_folder != null && !empty($selected_folder)) { |
| 396 | $terms = get_term($selected_folder); |
| 397 | if(!empty($terms) && isset($terms->term_id)) { |
| 398 | wp_set_post_terms($post_id, $terms->term_id, $post_type, false); |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | public function filter_attachments_grid( $args ) { |
| 404 | $taxonomy = 'media_folder'; |
| 405 | if ( ! isset( $args[ $taxonomy ] ) ) { |
| 406 | return $args; |
| 407 | } |
| 408 | $term = sanitize_text_field( $args[ $taxonomy ] ); |
| 409 | if ( $term != "-1" ) { |
| 410 | return $args; |
| 411 | } |
| 412 | unset( $args[ $taxonomy ] ); |
| 413 | $args['tax_query'] = array( |
| 414 | array( |
| 415 | 'taxonomy' => $taxonomy, |
| 416 | 'operator' => 'NOT EXISTS', |
| 417 | ), |
| 418 | ); |
| 419 | $args = apply_filters( 'media_library_organizer_media_filter_attachments_grid', $args ); |
| 420 | return $args; |
| 421 | } |
| 422 | |
| 423 | public function filter_record_list($query) { |
| 424 | global $typenow; |
| 425 | |
| 426 | if($typenow == "attachment") { |
| 427 | return; |
| 428 | } |
| 429 | |
| 430 | if(!self::is_for_this_post_type($typenow)) { |
| 431 | return $query; |
| 432 | } |
| 433 | |
| 434 | $taxonomy = self::get_custom_post_type($typenow); |
| 435 | |
| 436 | if ( ! isset( $query->query['post_type'] ) ) { |
| 437 | return $query; |
| 438 | } |
| 439 | |
| 440 | if ( ! isset( $_REQUEST[$taxonomy] ) ) { |
| 441 | return $query; |
| 442 | } |
| 443 | |
| 444 | $term = sanitize_text_field(wp_unslash($_REQUEST[$taxonomy])); |
| 445 | if ( $term != "-1" ) { |
| 446 | return $query; |
| 447 | } |
| 448 | |
| 449 | unset( $query->query_vars[$taxonomy] ); |
| 450 | |
| 451 | $tax_query = array( |
| 452 | 'taxonomy' => $taxonomy, |
| 453 | 'operator' => 'NOT EXISTS', |
| 454 | ); |
| 455 | |
| 456 | $query->set( 'tax_query', array( $tax_query ) ); |
| 457 | $query->tax_query = new WP_Tax_Query( array( $tax_query ) ); |
| 458 | |
| 459 | return $query; |
| 460 | } |
| 461 | |
| 462 | public function output_backbone_view_filters() { |
| 463 | wp_enqueue_script( 'folders-media', WCP_FOLDER_URL.'assets/js/media.js', array( 'media-editor', 'media-views' ), WCP_FOLDER_VERSION, true ); |
| 464 | wp_localize_script( 'folders-media', 'folders_media_options', array( |
| 465 | 'terms' => self::get_terms_hierarchical('media_folder'), |
| 466 | 'taxonomy' => get_taxonomy('media_folder') |
| 467 | )); |
| 468 | wp_enqueue_style( 'folders-media', WCP_FOLDER_URL . 'assets/css/media.css' , array(), WCP_FOLDER_VERSION); |
| 469 | } |
| 470 | |
| 471 | public function get_terms_hierarchical( $taxonomy ) { |
| 472 | // $terms = get_terms( array( |
| 473 | // 'taxonomy' => $taxonomy, |
| 474 | // 'hide_empty' => false, |
| 475 | // 'parent' => 0, |
| 476 | // 'orderby' => 'meta_value_num', |
| 477 | // 'order' => 'ASC', |
| 478 | // 'update_count_callback' => '_update_generic_term_count', |
| 479 | // 'meta_query' => [[ |
| 480 | // 'key' => 'wcp_custom_order', |
| 481 | // 'type' => 'NUMERIC', |
| 482 | // ]] |
| 483 | // ) ); |
| 484 | // |
| 485 | // if ( empty( $terms ) ) { |
| 486 | // return false; |
| 487 | // } |
| 488 | // |
| 489 | // $hierarchy = _get_term_hierarchy( $taxonomy ); |
| 490 | // |
| 491 | // $hierarchical_terms = array(); |
| 492 | // if(!empty($terms)) { |
| 493 | // foreach ($terms as $term) { |
| 494 | // if(isset($term->term_id)) { |
| 495 | // $hierarchical_terms[] = $term; |
| 496 | // $hierarchical_terms = self::add_child_terms_recursive($taxonomy, $hierarchical_terms, $hierarchy, $term->term_id, 1); |
| 497 | // } |
| 498 | // } |
| 499 | // } |
| 500 | // |
| 501 | // return $hierarchical_terms; |
| 502 | $terms = get_terms( array( |
| 503 | 'taxonomy' => $taxonomy, |
| 504 | 'hide_empty' => false, |
| 505 | 'parent' => 0, |
| 506 | 'orderby' => 'meta_value_num', |
| 507 | 'order' => 'ASC', |
| 508 | 'hierarchical' => false, |
| 509 | 'update_count_callback' => '_update_generic_term_count', |
| 510 | 'meta_query' => [[ |
| 511 | 'key' => 'wcp_custom_order', |
| 512 | 'type' => 'NUMERIC', |
| 513 | ]] |
| 514 | )); |
| 515 | $hierarchical_terms = array(); |
| 516 | foreach ($terms as $term) { |
| 517 | $hierarchical_terms[] = $term; |
| 518 | $hierarchical_terms = self::get_child_terms($taxonomy, $hierarchical_terms, $term->term_id, "-"); |
| 519 | } |
| 520 | return $hierarchical_terms; |
| 521 | } |
| 522 | |
| 523 | public static function get_child_terms($taxonomy, $hierarchical_terms, $term_id, $separator = "-") { |
| 524 | $terms = get_terms( array( |
| 525 | 'taxonomy' => $taxonomy, |
| 526 | 'hide_empty' => false, |
| 527 | 'parent' => $term_id, |
| 528 | 'orderby' => 'meta_value_num', |
| 529 | 'order' => 'ASC', |
| 530 | 'hierarchical' => false, |
| 531 | 'update_count_callback' => '_update_generic_term_count', |
| 532 | 'meta_query' => [[ |
| 533 | 'key' => 'wcp_custom_order', |
| 534 | 'type' => 'NUMERIC', |
| 535 | ]] |
| 536 | )); |
| 537 | if(!empty($terms)) { |
| 538 | foreach ($terms as $term) { |
| 539 | if(isset($term->name)) { |
| 540 | $term->name = $separator . " " . $term->name; |
| 541 | $hierarchical_terms[] = $term; |
| 542 | $hierarchical_terms = self::get_child_terms($taxonomy, $hierarchical_terms, $term->term_id, $separator . "-"); |
| 543 | } |
| 544 | } |
| 545 | } |
| 546 | return $hierarchical_terms; |
| 547 | } |
| 548 | |
| 549 | private function add_child_terms_recursive( $taxonomy, $hierarchical_terms, $hierarchy, $current_term_id, $current_depth ) { |
| 550 | |
| 551 | if ( ! isset( $hierarchy[ $current_term_id ] ) ) { |
| 552 | return $hierarchical_terms; |
| 553 | } |
| 554 | |
| 555 | foreach ( $hierarchy[ $current_term_id ] as $child_term_id ) { |
| 556 | |
| 557 | $child_term = get_term( $child_term_id, $taxonomy ); |
| 558 | |
| 559 | $child_term->name = str_pad( '', $current_depth, '-', STR_PAD_LEFT ) . ' ' . $child_term->name; |
| 560 | |
| 561 | $hierarchical_terms[] = $child_term; |
| 562 | |
| 563 | $hierarchical_terms = self::add_child_terms_recursive( $taxonomy, $hierarchical_terms, $hierarchy, $child_term_id, ( $current_depth + 1 ) ); |
| 564 | } |
| 565 | |
| 566 | return $hierarchical_terms; |
| 567 | } |
| 568 | |
| 569 | public function filter_attachments_list( $query ) { |
| 570 | |
| 571 | if ( ! isset( $query->query['post_type'] ) ) { |
| 572 | return $query; |
| 573 | } |
| 574 | |
| 575 | if ( is_array( $query->query['post_type'] ) && ! in_array( 'attachment', $query->query['post_type'] ) ) { |
| 576 | return $query; |
| 577 | } |
| 578 | if ( ! is_array( $query->query['post_type'] ) && strpos( $query->query['post_type'], 'attachment' ) === false ) { |
| 579 | return $query; |
| 580 | } |
| 581 | |
| 582 | if ( ! isset( $_REQUEST['media_folder'] ) ) { |
| 583 | return $query; |
| 584 | } |
| 585 | |
| 586 | $term = sanitize_text_field( $_REQUEST['media_folder'] ); |
| 587 | if ( $term != "-1" ) { |
| 588 | return $query; |
| 589 | } |
| 590 | |
| 591 | unset( $query->query_vars['media_folder'] ); |
| 592 | |
| 593 | $tax_query = array( |
| 594 | 'taxonomy' => 'media_folder', |
| 595 | 'operator' => 'NOT EXISTS', |
| 596 | ); |
| 597 | |
| 598 | $query->set( 'tax_query', array( $tax_query ) ); |
| 599 | $query->tax_query = new WP_Tax_Query( array( $tax_query ) ); |
| 600 | |
| 601 | $query = apply_filters( 'media_library_organizer_media_filter_attachments', $query, $_REQUEST ); |
| 602 | |
| 603 | return $query; |
| 604 | |
| 605 | } |
| 606 | |
| 607 | public function output_list_table_filters( $post_type, $view_name ) |
| 608 | { |
| 609 | if ($post_type != 'attachment') { |
| 610 | return; |
| 611 | } |
| 612 | |
| 613 | if ($view_name != 'bar') { |
| 614 | return; |
| 615 | } |
| 616 | |
| 617 | $current_term = false; |
| 618 | if ( isset( $_REQUEST['media_folder'] ) ) { |
| 619 | $current_term = sanitize_text_field(wp_unslash($_REQUEST['media_folder'])); |
| 620 | } |
| 621 | |
| 622 | wp_dropdown_categories( array( |
| 623 | 'show_option_all' => esc_html__('All Folders', WCP_FOLDER ), |
| 624 | 'show_option_none' => esc_html__('(Unassigned)', WCP_FOLDER ), |
| 625 | 'option_none_value' => -1, |
| 626 | 'orderby' => 'meta_value_num', |
| 627 | 'order' => 'ASC', |
| 628 | 'show_count' => true, |
| 629 | 'hide_empty' => false, |
| 630 | 'echo' => true, |
| 631 | 'selected' => $current_term, |
| 632 | 'hierarchical' => true, |
| 633 | 'name' => 'media_folder', |
| 634 | 'id' => '', |
| 635 | 'class' => '', |
| 636 | 'taxonomy' => 'media_folder', |
| 637 | 'value_field' => 'slug', |
| 638 | 'meta_query' => [[ |
| 639 | 'key' => 'wcp_custom_order', |
| 640 | 'type' => 'NUMERIC', |
| 641 | ]] |
| 642 | ) ); |
| 643 | |
| 644 | } |
| 645 | |
| 646 | |
| 647 | function new_to_auto_draft($post) { |
| 648 | |
| 649 | $post_type = $post->post_type; |
| 650 | |
| 651 | if(self::is_for_this_post_type($post_type)) { |
| 652 | |
| 653 | $post_type = self::get_custom_post_type($post_type); |
| 654 | $selected_folder = get_option("selected_{$post_type}_folder"); |
| 655 | |
| 656 | if($selected_folder != null && !empty($selected_folder)) { |
| 657 | $terms = get_term($selected_folder); |
| 658 | if(!empty($terms) && isset($terms->slug)) { |
| 659 | wp_set_object_terms($post->ID, $terms->slug, $post_type ); |
| 660 | } |
| 661 | |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | public function wcp_folder_send_message_to_owner() { |
| 667 | $response = array(); |
| 668 | $response['status'] = 0; |
| 669 | $response['error'] = 0; |
| 670 | $response['errors'] = array(); |
| 671 | $response['message'] = ""; |
| 672 | $errorArray = []; |
| 673 | $errorMessage = esc_html__("%s is required", WCP_FOLDER); |
| 674 | $postData = filter_input_array(INPUT_POST); |
| 675 | if(!isset($postData['textarea_text']) || trim($postData['textarea_text']) == "") { |
| 676 | $error = array( |
| 677 | "key" => "textarea_text", |
| 678 | "message" => esc_html__("Please enter your message",WCP_FOLDER) |
| 679 | ); |
| 680 | $errorArray[] = $error; |
| 681 | } |
| 682 | if(!isset($postData['user_email']) || trim($postData['user_email']) == "") { |
| 683 | $error = array( |
| 684 | "key" => "user_email", |
| 685 | "message" => sprintf($errorMessage,__("Email",WCP_FOLDER)) |
| 686 | ); |
| 687 | $errorArray[] = $error; |
| 688 | } else if(!filter_var($postData['user_email'], FILTER_VALIDATE_EMAIL)) { |
| 689 | $error = array( |
| 690 | 'key' => "user_email", |
| 691 | "message" => "Email is not valid" |
| 692 | ); |
| 693 | $errorArray[] = $error; |
| 694 | } |
| 695 | if(empty($errorArray)) { |
| 696 | if(!isset($postData['folder_help_nonce']) || trim($postData['folder_help_nonce']) == "") { |
| 697 | $error = array( |
| 698 | "key" => "nonce", |
| 699 | "message" => esc_html__("Your request is not valid", WCP_FOLDER) |
| 700 | ); |
| 701 | $errorArray[] = $error; |
| 702 | } else { |
| 703 | if(!wp_verify_nonce($postData['folder_help_nonce'], 'wcp_folder_help_nonce')) { |
| 704 | $error = array( |
| 705 | "key" => "nonce", |
| 706 | "message" => esc_html__("Your request is not valid", WCP_FOLDER) |
| 707 | ); |
| 708 | $errorArray[] = $error; |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | if(empty($errorArray)) { |
| 713 | global $current_user; |
| 714 | $text_message = self::sanitize_options($postData['textarea_text']); |
| 715 | $email = self::sanitize_options($postData['user_email'],"email"); |
| 716 | $domain = site_url(); |
| 717 | $user_name = $current_user->first_name." ".$current_user->last_name; |
| 718 | $subject = "Folder request: ".$domain; |
| 719 | $headers = "MIME-Version: 1.0\r\n"; |
| 720 | $headers .= "Content-Type: text/html; charset=UTF-8\r\n"; |
| 721 | $headers .= 'From: '.$user_name.' <'.$email.'>'.PHP_EOL ; |
| 722 | $headers .= 'Reply-To: '.$user_name.' <'.$email.'>'.PHP_EOL ; |
| 723 | $headers .= 'X-Mailer: PHP/' . phpversion(); |
| 724 | ob_start(); |
| 725 | ?> |
| 726 | <table border="0" cellspacing="0" cellpadding="5"> |
| 727 | <tr> |
| 728 | <th>Domain</th> |
| 729 | <td><?php echo esc_attr($domain) ?></td> |
| 730 | </tr> |
| 731 | <tr> |
| 732 | <th>Email</th> |
| 733 | <td><?php echo esc_attr($email) ?></td> |
| 734 | </tr> |
| 735 | <tr> |
| 736 | <th>Message</th> |
| 737 | <td><?php echo esc_attr(nl2br($text_message)) ?></td> |
| 738 | </tr> |
| 739 | </table> |
| 740 | <?php |
| 741 | $message = ob_get_clean(); |
| 742 | $email_id = "gal@premio.io"; |
| 743 | $status = wp_mail($email_id, $subject, $message, $headers); |
| 744 | if($status) { |
| 745 | $response['status'] = 1; |
| 746 | } else { |
| 747 | $response['status'] = 0; |
| 748 | $response['message'] = "Not able to send mail"; |
| 749 | } |
| 750 | } else { |
| 751 | $response['error'] = 1; |
| 752 | $response['errors'] = $errorArray; |
| 753 | } |
| 754 | echo json_encode($response); |
| 755 | } |
| 756 | |
| 757 | public function folder_plugin_deactivate() { |
| 758 | global $current_user; |
| 759 | $postData = filter_input_array(INPUT_POST); |
| 760 | $errorCounter = 0; |
| 761 | $response = array(); |
| 762 | $response['status'] = 0; |
| 763 | $response['message'] = ""; |
| 764 | $response['valid'] = 1; |
| 765 | if(!isset($postData['reason']) || empty($postData['reason'])) { |
| 766 | $errorCounter++; |
| 767 | $response['message'] = "Please provide reason"; |
| 768 | } else if (!isset($postData['nonce']) || empty($postData['nonce'])) { |
| 769 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 770 | $errorCounter++; |
| 771 | $response['valid'] = 0; |
| 772 | } else { |
| 773 | $nonce = self::sanitize_options($postData['nonce']); |
| 774 | if(!wp_verify_nonce($nonce, 'wcp_folder_deactivate_nonce')) { |
| 775 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 776 | $errorCounter++; |
| 777 | $response['valid'] = 0; |
| 778 | } |
| 779 | } |
| 780 | if($errorCounter == 0) { |
| 781 | $reason = $postData['reason']; |
| 782 | $email = "none@none.none"; |
| 783 | if (isset($postData['email_id']) && !empty($postData['email_id']) && filter_var($postData['email_id'], FILTER_VALIDATE_EMAIL)) { |
| 784 | $email = $postData['email_id']; |
| 785 | } |
| 786 | $domain = site_url(); |
| 787 | $user_name = $current_user->first_name." ".$current_user->last_name; |
| 788 | $subject = "Folders was removed from {$domain}"; |
| 789 | $headers = "MIME-Version: 1.0\r\n"; |
| 790 | $headers .= "Content-Type: text/html; charset=UTF-8\r\n"; |
| 791 | $headers .= 'From: '.$user_name.' <'.$email.'>'.PHP_EOL ; |
| 792 | $headers .= 'Reply-To: '.$user_name.' <'.$email.'>'.PHP_EOL ; |
| 793 | $headers .= 'X-Mailer: PHP/' . phpversion(); |
| 794 | ob_start(); |
| 795 | ?> |
| 796 | <table border="0" cellspacing="0" cellpadding="5"> |
| 797 | <tr> |
| 798 | <th>Plugin</th> |
| 799 | <td>Folders</td> |
| 800 | </tr> |
| 801 | <tr> |
| 802 | <th>Plugin Version</th> |
| 803 | <td><?php echo esc_attr(WCP_FOLDER_VERSION) ?></td> |
| 804 | </tr> |
| 805 | <tr> |
| 806 | <th>Domain</th> |
| 807 | <td><?php echo esc_attr($domain) ?></td> |
| 808 | </tr> |
| 809 | <tr> |
| 810 | <th>Email</th> |
| 811 | <td><?php echo esc_attr($email) ?></td> |
| 812 | </tr> |
| 813 | <tr> |
| 814 | <th>Comment</th> |
| 815 | <td><?php echo esc_attr(nl2br($reason)) ?></td> |
| 816 | </tr> |
| 817 | <tr> |
| 818 | <th>WordPress Version</th> |
| 819 | <td><?php echo esc_attr(get_bloginfo('version')) ?></td> |
| 820 | </tr> |
| 821 | <tr> |
| 822 | <th>PHP Version</th> |
| 823 | <td><?php echo esc_attr(PHP_VERSION) ?></td> |
| 824 | </tr> |
| 825 | </table> |
| 826 | <?php |
| 827 | $content = ob_get_clean(); |
| 828 | $email_id = "gal@premio.io"; |
| 829 | wp_mail($email_id, $subject, $content, $headers); |
| 830 | $response['status'] = 1; |
| 831 | } |
| 832 | echo json_encode($response); |
| 833 | die; |
| 834 | } |
| 835 | |
| 836 | public function check_has_valid_key() |
| 837 | { |
| 838 | return 0; |
| 839 | } |
| 840 | |
| 841 | public static function total_term_folders() |
| 842 | { |
| 843 | $post_types = get_option(WCP_FOLDER_VAR); |
| 844 | $post_types = is_array($post_types)?$post_types:array(); |
| 845 | $total = 0; |
| 846 | foreach ($post_types as $post_type) { |
| 847 | $post_type = self::get_custom_post_type($post_type); |
| 848 | $total += wp_count_terms($post_type); |
| 849 | } |
| 850 | return $total; |
| 851 | } |
| 852 | |
| 853 | public function get_license_key_information($licenseKey) |
| 854 | { |
| 855 | return array(); |
| 856 | } |
| 857 | |
| 858 | public function get_license_key_data($licenseKey = '') |
| 859 | { |
| 860 | return array(); |
| 861 | } |
| 862 | |
| 863 | public function check_for_license_key() |
| 864 | { |
| 865 | return false; |
| 866 | } |
| 867 | |
| 868 | public function wcp_remove_post_item() |
| 869 | { |
| 870 | $response = array(); |
| 871 | $response['status'] = 0; |
| 872 | $response['error'] = 0; |
| 873 | $response['data'] = array(); |
| 874 | $response['message'] = ""; |
| 875 | $postData = filter_input_array(INPUT_POST); |
| 876 | if (isset($postData['post_id']) && !empty($postData['post_id'])) { |
| 877 | wp_delete_post($postData['post_id']); |
| 878 | $response['status'] = 1; |
| 879 | } |
| 880 | echo json_encode($response); |
| 881 | die; |
| 882 | } |
| 883 | |
| 884 | public function wcp_change_all_status() |
| 885 | { |
| 886 | $response = array(); |
| 887 | $response['status'] = 0; |
| 888 | $response['error'] = 0; |
| 889 | $response['data'] = array(); |
| 890 | $response['message'] = ""; |
| 891 | $postData = filter_input_array(INPUT_POST); |
| 892 | $errorCounter = 0; |
| 893 | if (!isset($postData['type']) || empty($postData['type'])) { |
| 894 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 895 | $errorCounter++; |
| 896 | } else if (!isset($postData['nonce']) || empty($postData['nonce'])) { |
| 897 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 898 | $errorCounter++; |
| 899 | } else if (!current_user_can("manage_categories") || ($postData['type'] == "page" && !current_user_can("edit_pages"))) { |
| 900 | $response['message'] = esc_html__("You have not permission to update width", WCP_FOLDER); |
| 901 | $errorCounter++; |
| 902 | } else if (!current_user_can("manage_categories") || ($postData['type'] != "page" && !current_user_can("edit_posts"))) { |
| 903 | $response['message'] = esc_html__("You have not permission to update width", WCP_FOLDER); |
| 904 | $errorCounter++; |
| 905 | } else { |
| 906 | $type = self::sanitize_options($postData['type']); |
| 907 | $nonce = self::sanitize_options($postData['nonce']); |
| 908 | if(!wp_verify_nonce($nonce, 'wcp_folder_nonce_'.$type)) { |
| 909 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 910 | $errorCounter++; |
| 911 | } |
| 912 | } |
| 913 | if ($errorCounter == 0) { |
| 914 | if (isset($postData['folders']) || !empty($postData['folders'])) { |
| 915 | $status = isset($postData['status']) ? $postData['status'] : 0; |
| 916 | $status = self::sanitize_options($status); |
| 917 | $folders = self::sanitize_options($postData['folders']); |
| 918 | $folders = trim($folders, ","); |
| 919 | $folders = explode(",", $folders); |
| 920 | foreach ($folders as $folder) { |
| 921 | update_term_meta($folder, "is_active", $status); |
| 922 | } |
| 923 | } |
| 924 | $response['status'] = 1; |
| 925 | } |
| 926 | echo json_encode($response); |
| 927 | die; |
| 928 | } |
| 929 | |
| 930 | public function wcp_change_post_width() |
| 931 | { |
| 932 | $response = array(); |
| 933 | $response['status'] = 0; |
| 934 | $response['error'] = 0; |
| 935 | $response['data'] = array(); |
| 936 | $response['message'] = ""; |
| 937 | $postData = filter_input_array(INPUT_POST); |
| 938 | $errorCounter = 0; |
| 939 | if (!isset($postData['width']) || empty($postData['width'])) { |
| 940 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 941 | $errorCounter++; |
| 942 | } else if (!isset($postData['type']) || empty($postData['type'])) { |
| 943 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 944 | $errorCounter++; |
| 945 | } else if (!isset($postData['nonce']) || empty($postData['nonce'])) { |
| 946 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 947 | $errorCounter++; |
| 948 | } else if ($postData['type'] == "page" && !current_user_can("edit_pages")) { |
| 949 | $response['message'] = esc_html__("You have not permission to update width", WCP_FOLDER); |
| 950 | $errorCounter++; |
| 951 | } else if ($postData['type'] != "page" && !current_user_can("edit_posts")) { |
| 952 | $response['message'] = esc_html__("You have not permission to update width", WCP_FOLDER); |
| 953 | $errorCounter++; |
| 954 | } else { |
| 955 | $type = self::sanitize_options($postData['type']); |
| 956 | $nonce = self::sanitize_options($postData['nonce']); |
| 957 | if(!wp_verify_nonce($nonce, 'wcp_folder_nonce_'.$type)) { |
| 958 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 959 | $errorCounter++; |
| 960 | } |
| 961 | } |
| 962 | if ($errorCounter == 0) { |
| 963 | $type = self::sanitize_options($postData['type']); |
| 964 | $width = self::sanitize_options($postData['width'], "int"); |
| 965 | $optionName = "wcp_dynamic_width_for_" . $type; |
| 966 | update_option($optionName, $width); |
| 967 | $response['status'] = 1; |
| 968 | } |
| 969 | echo json_encode($response); |
| 970 | die; |
| 971 | } |
| 972 | |
| 973 | public function wcp_change_multiple_post_folder() |
| 974 | { |
| 975 | $response = array(); |
| 976 | $response['status'] = 0; |
| 977 | $response['error'] = 0; |
| 978 | $response['data'] = array(); |
| 979 | $response['message'] = ""; |
| 980 | $postData = filter_input_array(INPUT_POST); |
| 981 | $errorCounter = 0; |
| 982 | if (!isset($postData['post_ids']) || empty($postData['post_ids'])) { |
| 983 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 984 | $errorCounter++; |
| 985 | } else if (!isset($postData['folder_id']) || empty($postData['folder_id'])) { |
| 986 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 987 | $errorCounter++; |
| 988 | } else if (!isset($postData['type']) || empty($postData['type'])) { |
| 989 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 990 | $errorCounter++; |
| 991 | } else if (!isset($postData['nonce']) || empty($postData['nonce'])) { |
| 992 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 993 | $errorCounter++; |
| 994 | } else if ($postData['type'] == "page" && !current_user_can("edit_pages")) { |
| 995 | $response['message'] = esc_html__("You have not permission to update folder", WCP_FOLDER); |
| 996 | $errorCounter++; |
| 997 | } else if ($postData['type'] != "page" && !current_user_can("edit_posts")) { |
| 998 | $response['message'] = esc_html__("You have not permission to update folder", WCP_FOLDER); |
| 999 | $errorCounter++; |
| 1000 | } else { |
| 1001 | $folder_id = self::sanitize_options($postData['folder_id']); |
| 1002 | if(!wp_verify_nonce($postData['nonce'], 'wcp_folder_term_'.$folder_id)) { |
| 1003 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 1004 | $errorCounter++; |
| 1005 | } |
| 1006 | } |
| 1007 | if ($errorCounter == 0) { |
| 1008 | $postID = self::sanitize_options($postData['post_ids']); |
| 1009 | $postID = trim($postID, ","); |
| 1010 | $folderID = self::sanitize_options($postData['folder_id']); |
| 1011 | $type = self::sanitize_options($postData['type']); |
| 1012 | $postArray = explode(",", $postID); |
| 1013 | $status = 0; |
| 1014 | if(isset($postData['status'])) { |
| 1015 | $status = self::sanitize_options($postData['status']); |
| 1016 | } |
| 1017 | $status = true; |
| 1018 | |
| 1019 | $taxonomy = ""; |
| 1020 | if(isset($postData['taxonomy'])) { |
| 1021 | $taxonomy = self::sanitize_options($postData['taxonomy']); |
| 1022 | } |
| 1023 | if (is_array($postArray)) { |
| 1024 | $post_type = self::get_custom_post_type($type); |
| 1025 | foreach ($postArray as $post) { |
| 1026 | $terms = get_the_terms($post, $post_type); |
| 1027 | if (!empty($terms)) { |
| 1028 | foreach ($terms as $term) { |
| 1029 | if(!empty($taxonomy) && ($term->term_id == $taxonomy || $term->slug == $taxonomy)) { |
| 1030 | wp_remove_object_terms($post, $term->term_id, $post_type); |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | wp_set_post_terms($post, $folderID, $post_type, $status); |
| 1035 | } |
| 1036 | } |
| 1037 | $response['status'] = 1; |
| 1038 | } |
| 1039 | echo json_encode($response); |
| 1040 | die; |
| 1041 | } |
| 1042 | |
| 1043 | public function wcp_change_post_folder() |
| 1044 | { |
| 1045 | $response = array(); |
| 1046 | $response['status'] = 0; |
| 1047 | $response['error'] = 0; |
| 1048 | $response['data'] = array(); |
| 1049 | $response['message'] = ""; |
| 1050 | $postData = filter_input_array(INPUT_POST); |
| 1051 | $errorCounter = 0; |
| 1052 | if (!isset($postData['post_id']) || empty($postData['post_id'])) { |
| 1053 | $errorCounter++; |
| 1054 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 1055 | } else if (!isset($postData['folder_id']) || empty($postData['folder_id'])) { |
| 1056 | $errorCounter++; |
| 1057 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 1058 | } else if (!isset($postData['type']) || empty($postData['type'])) { |
| 1059 | $errorCounter++; |
| 1060 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 1061 | } else if (!isset($postData['nonce']) || empty($postData['nonce'])) { |
| 1062 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 1063 | $errorCounter++; |
| 1064 | } else if ($postData['type'] == "page" && !current_user_can("edit_pages")) { |
| 1065 | $response['message'] = esc_html__("You have not permission to update folder", WCP_FOLDER); |
| 1066 | $errorCounter++; |
| 1067 | } else if ($postData['type'] != "page" && !current_user_can("edit_posts")) { |
| 1068 | $response['message'] = esc_html__("You have not permission to update folder", WCP_FOLDER); |
| 1069 | $errorCounter++; |
| 1070 | } else { |
| 1071 | $term_id = self::sanitize_options($postData['folder_id']); |
| 1072 | if(!wp_verify_nonce($postData['nonce'], 'wcp_folder_term_'.$term_id)) { |
| 1073 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 1074 | $errorCounter++; |
| 1075 | } |
| 1076 | } |
| 1077 | if ($errorCounter == 0) { |
| 1078 | $postID = self::sanitize_options($postData['post_id']); |
| 1079 | $folderID = self::sanitize_options($postData['folder_id']); |
| 1080 | $type = self::sanitize_options($postData['type']); |
| 1081 | $folder_post_type = self::get_custom_post_type($type); |
| 1082 | $status = 0; |
| 1083 | if(isset($postData['status'])) { |
| 1084 | $status = self::sanitize_options($postData['status']); |
| 1085 | } |
| 1086 | $status = ($status == 1)?true:false; |
| 1087 | $taxonomy = ""; |
| 1088 | if(isset($postData['taxonomy'])) { |
| 1089 | $taxonomy = self::sanitize_options($postData['taxonomy']); |
| 1090 | } |
| 1091 | $terms = get_the_terms($postID, $folder_post_type); |
| 1092 | if (!empty($terms)) { |
| 1093 | foreach ($terms as $term) { |
| 1094 | if(!empty($taxonomy) && ($term->term_id == $taxonomy || $term->slug == $taxonomy)) { |
| 1095 | wp_remove_object_terms($postID, $term->term_id, $folder_post_type); |
| 1096 | } |
| 1097 | } |
| 1098 | } |
| 1099 | wp_set_post_terms($postID, $folderID, $folder_post_type, true); |
| 1100 | $response['status'] = 1; |
| 1101 | } |
| 1102 | echo json_encode($response); |
| 1103 | die; |
| 1104 | } |
| 1105 | |
| 1106 | public function wcp_mark_un_mark_folder() |
| 1107 | { |
| 1108 | $response = array(); |
| 1109 | $response['status'] = 0; |
| 1110 | $response['error'] = 0; |
| 1111 | $response['data'] = array(); |
| 1112 | $response['message'] = ""; |
| 1113 | $postData = filter_input_array(INPUT_POST); |
| 1114 | $errorCounter = 0; |
| 1115 | if (!current_user_can("manage_categories")) { |
| 1116 | $response['message'] = esc_html__("You have not permission to update folder", WCP_FOLDER); |
| 1117 | $errorCounter++; |
| 1118 | } else if (!isset($postData['term_id']) || empty($postData['term_id'])) { |
| 1119 | $errorCounter++; |
| 1120 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 1121 | } else if (!isset($postData['nonce']) || empty($postData['nonce'])) { |
| 1122 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 1123 | $errorCounter++; |
| 1124 | } else { |
| 1125 | $term_id = self::sanitize_options($postData['term_id']); |
| 1126 | if(!wp_verify_nonce($postData['nonce'], 'wcp_folder_highlight_term_'.$term_id)) { |
| 1127 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 1128 | $errorCounter++; |
| 1129 | } |
| 1130 | } |
| 1131 | if ($errorCounter == 0) { |
| 1132 | $term_id = self::sanitize_options($postData['term_id']); |
| 1133 | $status = get_term_meta($term_id, "is_highlighted", true); |
| 1134 | if ($status == 1) { |
| 1135 | update_term_meta($term_id, "is_highlighted", 0); |
| 1136 | $status = 0; |
| 1137 | } else { |
| 1138 | update_term_meta($term_id, "is_highlighted", 1); |
| 1139 | $status = 1; |
| 1140 | } |
| 1141 | $response['marked'] = $status; |
| 1142 | $response['id'] = $postData['term_id']; |
| 1143 | $response['status'] = 1; |
| 1144 | } |
| 1145 | echo json_encode($response); |
| 1146 | die; |
| 1147 | } |
| 1148 | |
| 1149 | public function wcp_save_folder_order() |
| 1150 | { |
| 1151 | $response = array(); |
| 1152 | $response['status'] = 0; |
| 1153 | $response['error'] = 0; |
| 1154 | $response['data'] = array(); |
| 1155 | $response['message'] = ""; |
| 1156 | $postData = filter_input_array(INPUT_POST); |
| 1157 | $errorCounter = 0; |
| 1158 | if (!current_user_can("manage_categories")) { |
| 1159 | $response['message'] = esc_html__("You have not permission to update folder order", WCP_FOLDER); |
| 1160 | $errorCounter++; |
| 1161 | } else if (!isset($postData['term_ids']) || empty($postData['term_ids'])) { |
| 1162 | $errorCounter++; |
| 1163 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 1164 | } else if (!isset($postData['type']) || empty($postData['type'])) { |
| 1165 | $errorCounter++; |
| 1166 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 1167 | } else if (!isset($postData['nonce']) || empty($postData['nonce'])) { |
| 1168 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 1169 | $errorCounter++; |
| 1170 | } else { |
| 1171 | $type = self::sanitize_options($postData['type']); |
| 1172 | if(!wp_verify_nonce($postData['nonce'], 'wcp_folder_nonce_'.$type)) { |
| 1173 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 1174 | $errorCounter++; |
| 1175 | } |
| 1176 | } |
| 1177 | if ($errorCounter == 0) { |
| 1178 | $termIds = self::sanitize_options(($postData['term_ids'])); |
| 1179 | $type = self::sanitize_options($postData['type']); |
| 1180 | $termIds = trim($termIds, ","); |
| 1181 | $termArray = explode(",", $termIds); |
| 1182 | $order = 1; |
| 1183 | foreach ($termArray as $term) { |
| 1184 | if (!empty($term)) { |
| 1185 | update_term_meta($term, "wcp_custom_order", $order); |
| 1186 | $order++; |
| 1187 | } |
| 1188 | } |
| 1189 | $response['status'] = 1; |
| 1190 | $folder_type = self::get_custom_post_type($type); |
| 1191 | $response['options'] = WCP_Tree::get_option_data_for_select($folder_type); |
| 1192 | |
| 1193 | } |
| 1194 | echo json_encode($response); |
| 1195 | die; |
| 1196 | } |
| 1197 | |
| 1198 | public function save_wcp_folder_state() |
| 1199 | { |
| 1200 | $response = array(); |
| 1201 | $response['status'] = 0; |
| 1202 | $response['error'] = 0; |
| 1203 | $response['data'] = array(); |
| 1204 | $response['message'] = ""; |
| 1205 | $postData = filter_input_array(INPUT_POST); |
| 1206 | $errorCounter = 0; |
| 1207 | if (!current_user_can("manage_categories")) { |
| 1208 | $response['message'] = esc_html__("You have not permission to update folder", WCP_FOLDER); |
| 1209 | $errorCounter++; |
| 1210 | } else if (!isset($postData['term_id']) || empty($postData['term_id'])) { |
| 1211 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 1212 | $errorCounter++; |
| 1213 | } else if (!isset($postData['nonce']) || empty($postData['nonce'])) { |
| 1214 | $response['message'] = "Unable to create folder, Your request is not valid"; |
| 1215 | $errorCounter++; |
| 1216 | } else { |
| 1217 | $term_id = self::sanitize_options($postData['term_id']); |
| 1218 | if(!wp_verify_nonce($postData['nonce'], 'wcp_folder_term_'.$term_id)) { |
| 1219 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 1220 | $errorCounter++; |
| 1221 | } |
| 1222 | } |
| 1223 | if ($errorCounter == 0) { |
| 1224 | $response['status'] = 1; |
| 1225 | $term_id = self::sanitize_options($postData['term_id']); |
| 1226 | $is_active = isset($postData['is_active'])?$postData['is_active']:0; |
| 1227 | $is_active = self::sanitize_options($is_active); |
| 1228 | if ($is_active == 1) { |
| 1229 | update_term_meta($term_id, "is_active", 1); |
| 1230 | } else { |
| 1231 | update_term_meta($term_id, "is_active", 0); |
| 1232 | } |
| 1233 | } |
| 1234 | echo json_encode($response); |
| 1235 | die; |
| 1236 | } |
| 1237 | |
| 1238 | public function wcp_update_parent_information() |
| 1239 | { |
| 1240 | $response = array(); |
| 1241 | $response['status'] = 0; |
| 1242 | $response['error'] = 0; |
| 1243 | $response['data'] = array(); |
| 1244 | $response['message'] = ""; |
| 1245 | $postData = filter_input_array(INPUT_POST); |
| 1246 | $errorCounter = 0; |
| 1247 | if (!current_user_can("manage_categories")) { |
| 1248 | $response['message'] = esc_html__("You have not permission to update folder", WCP_FOLDER); |
| 1249 | $errorCounter++; |
| 1250 | } else if (!isset($postData['term_id']) || empty($postData['term_id'])) { |
| 1251 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 1252 | $errorCounter++; |
| 1253 | } else if (!isset($postData['type']) || empty($postData['type'])) { |
| 1254 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 1255 | $errorCounter++; |
| 1256 | } else if (!isset($postData['parent_id'])) { |
| 1257 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 1258 | $errorCounter++; |
| 1259 | } else if (!isset($postData['nonce']) || empty($postData['nonce'])) { |
| 1260 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 1261 | $errorCounter++; |
| 1262 | } else { |
| 1263 | $term_id = self::sanitize_options($postData['term_id']); |
| 1264 | if(!wp_verify_nonce($postData['nonce'], 'wcp_folder_term_'.$term_id)) { |
| 1265 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 1266 | $errorCounter++; |
| 1267 | } |
| 1268 | } |
| 1269 | if($errorCounter == 0) { |
| 1270 | $term_id = self::sanitize_options($postData['term_id']); |
| 1271 | $parent_id = self::sanitize_options($postData['parent_id']); |
| 1272 | $type = self::sanitize_options($postData['type']); |
| 1273 | $folder_type = self::get_custom_post_type($type); |
| 1274 | wp_update_term($term_id, $folder_type, array( |
| 1275 | 'parent' => $parent_id |
| 1276 | )); |
| 1277 | update_term_meta($parent_id, "is_active", 1); |
| 1278 | $response['status'] = 1; |
| 1279 | } |
| 1280 | echo json_encode($response); |
| 1281 | die; |
| 1282 | } |
| 1283 | |
| 1284 | public function wcp_save_parent_data() |
| 1285 | { |
| 1286 | $response = array(); |
| 1287 | $response['status'] = 0; |
| 1288 | $response['error'] = 0; |
| 1289 | $response['data'] = array(); |
| 1290 | $response['message'] = ""; |
| 1291 | $postData = filter_input_array(INPUT_POST); |
| 1292 | $errorCounter = 0; |
| 1293 | if (!isset($postData['type']) || empty($postData['type'])) { |
| 1294 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 1295 | $errorCounter++; |
| 1296 | } else if (!isset($postData['nonce']) || empty($postData['nonce'])) { |
| 1297 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 1298 | $errorCounter++; |
| 1299 | } else { |
| 1300 | $type = self::sanitize_options($postData['type']); |
| 1301 | if(!wp_verify_nonce($postData['nonce'], 'wcp_folder_nonce_'.$type)) { |
| 1302 | $response['message'] = esc_html__("Your request is not valid", WCP_FOLDER); |
| 1303 | $errorCounter++; |
| 1304 | } |
| 1305 | } |
| 1306 | if ($errorCounter == 0) { |
| 1307 | $type = self::sanitize_options($postData['type']); |
| 1308 | $optionName = $type . "_parent_status"; |
| 1309 | $response['status'] = 1; |
| 1310 | $is_active = isset($postData['is_active'])?$postData['is_active']:0; |
| 1311 | $is_active = self::sanitize_options($is_active); |
| 1312 | if ($is_active == 1) { |
| 1313 | update_option($optionName, 1); |
| 1314 | } else { |
| 1315 | update_option($optionName, 0); |
| 1316 | } |
| 1317 | } |
| 1318 | echo json_encode($response); |
| 1319 | die; |
| 1320 | } |
| 1321 | |
| 1322 | public function wcp_remove_folder() |
| 1323 | { |
| 1324 | $response = array(); |
| 1325 | $response['status'] = 0; |
| 1326 | $response['error'] = 0; |
| 1327 | $response['data'] = array(); |
| 1328 | $response['message'] = ""; |
| 1329 | $postData = filter_input_array(INPUT_POST); |
| 1330 | $errorCounter = 0; |
| 1331 | if (!current_user_can("manage_categories")) { |
| 1332 | $error = esc_html__("You have not permission to remove folder", WCP_FOLDER); |
| 1333 | $errorCounter++; |
| 1334 | } else if (!isset($postData['term_id']) || empty($postData['term_id'])) { |
| 1335 | $error = esc_html__("Your request is not valid", WCP_FOLDER); |
| 1336 | $errorCounter++; |
| 1337 | } else if (!isset($postData['type']) || empty($postData['type'])) { |
| 1338 | $error = esc_html__("Your request is not valid", WCP_FOLDER); |
| 1339 | $errorCounter++; |
| 1340 | } else if (!isset($postData['nonce']) || empty($postData['nonce'])) { |
| 1341 | $error = "Unable to delete folder, Your request is not valid"; |
| 1342 | $errorCounter++; |
| 1343 | } else { |
| 1344 | $term_id = self::sanitize_options($postData['term_id']); |
| 1345 | if(!wp_verify_nonce($postData['nonce'], 'wcp_folder_delete_term_'.$term_id)) { |
| 1346 | $error = "Unable to delete folder, Your request is not valid"; |
| 1347 | $errorCounter++; |
| 1348 | } |
| 1349 | } |
| 1350 | if ($errorCounter == 0) { |
| 1351 | $term_id = self::sanitize_options($postData['term_id']); |
| 1352 | $type = self::sanitize_options($postData['type']); |
| 1353 | self::remove_folder_child_items($term_id, $type); |
| 1354 | $response['status'] = 1; |
| 1355 | $is_active = 1; |
| 1356 | $folders = -1; |
| 1357 | if (!self::check_has_valid_key()) { |
| 1358 | $is_active = 0; |
| 1359 | $folders = self::total_term_folders(); |
| 1360 | } |
| 1361 | $response['folders'] = $folders; |
| 1362 | $response['is_key_active'] = $is_active; |
| 1363 | } else { |
| 1364 | $response['error'] = 1; |
| 1365 | $response['message'] = $error; |
| 1366 | } |
| 1367 | echo json_encode($response); |
| 1368 | die; |
| 1369 | } |
| 1370 | |
| 1371 | public function remove_folder_child_items($term_id, $post_type) |
| 1372 | { |
| 1373 | $folder_type = self::get_custom_post_type($post_type); |
| 1374 | $terms = get_terms($folder_type, array( |
| 1375 | 'hide_empty' => false, |
| 1376 | 'parent' => $term_id |
| 1377 | )); |
| 1378 | |
| 1379 | if (!empty($terms)) { |
| 1380 | foreach ($terms as $term) { |
| 1381 | self::remove_folder_child_items($term->term_id, $post_type); |
| 1382 | } |
| 1383 | wp_delete_term($term_id, $folder_type); |
| 1384 | } else { |
| 1385 | wp_delete_term($term_id, $folder_type); |
| 1386 | } |
| 1387 | } |
| 1388 | |
| 1389 | public function wcp_update_folder() |
| 1390 | { |
| 1391 | $response = array(); |
| 1392 | $response['status'] = 0; |
| 1393 | $response['error'] = 0; |
| 1394 | $response['data'] = array(); |
| 1395 | $response['message'] = ""; |
| 1396 | $postData = $_REQUEST; |
| 1397 | $errorCounter = 0; |
| 1398 | if (!current_user_can("manage_categories")) { |
| 1399 | $error = esc_html__("You have not permission to update folder", WCP_FOLDER); |
| 1400 | $errorCounter++; |
| 1401 | } else if (!isset($postData['term_id']) || empty($postData['term_id'])) { |
| 1402 | $error = esc_html__("Unable to rename folder, Your request is not valid", WCP_FOLDER); |
| 1403 | $errorCounter++; |
| 1404 | } else if (!isset($postData['name']) || empty($postData['name'])) { |
| 1405 | $error = esc_html__("Folder name can no be empty", WCP_FOLDER); |
| 1406 | $errorCounter++; |
| 1407 | } else if (!isset($postData['type']) || empty($postData['type'])) { |
| 1408 | $error = esc_html__("Your request is not valid", WCP_FOLDER); |
| 1409 | $errorCounter++; |
| 1410 | } else if (!isset($postData['nonce']) || empty($postData['nonce'])) { |
| 1411 | $error = esc_html__("Unable to rename folder, Your request is not valid", WCP_FOLDER); |
| 1412 | $errorCounter++; |
| 1413 | } else { |
| 1414 | $term_id = self::sanitize_options($postData['term_id']); |
| 1415 | if(!wp_verify_nonce($postData['nonce'], 'wcp_folder_rename_term_'.$term_id)) { |
| 1416 | $error = _("Unable to rename folder, Your request is not valid", WCP_FOLDER); |
| 1417 | $errorCounter++; |
| 1418 | } |
| 1419 | } |
| 1420 | if ($errorCounter == 0) { |
| 1421 | $type = self::sanitize_options($postData['type']); |
| 1422 | $folder_type = self::get_custom_post_type($type); |
| 1423 | $name = self::sanitize_options($postData['name']); |
| 1424 | $term_id = self::sanitize_options($postData['term_id']); |
| 1425 | $result = wp_update_term( |
| 1426 | $term_id, |
| 1427 | $folder_type, |
| 1428 | array( |
| 1429 | 'name' => $name, |
| 1430 | ) |
| 1431 | ); |
| 1432 | if (!empty($result)) { |
| 1433 | $response['id'] = $result['term_id']; |
| 1434 | $response['status'] = 1; |
| 1435 | $response['term_title'] = $postData['name']; |
| 1436 | } else { |
| 1437 | $response['message'] = esc_html__("Unable to rename folder", WCP_FOLDER); |
| 1438 | } |
| 1439 | } else { |
| 1440 | $response['error'] = 1; |
| 1441 | $response['message'] = $error; |
| 1442 | } |
| 1443 | echo json_encode($response); |
| 1444 | die; |
| 1445 | } |
| 1446 | |
| 1447 | public function create_slug_from_string($str) |
| 1448 | { |
| 1449 | $a = array('À', 'Á', 'Â', 'Ã', 'Ä', '� |
| 1450 | ', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'A', 'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'Ð', 'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', '?', '?', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', '?', '?', 'L', 'l', 'N', 'n', 'N', 'n', 'N', 'n', '?', 'O', 'o', 'O', 'o', 'O', 'o', 'Œ', 'œ', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'Š', 'š', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Ÿ', 'Z', 'z', 'Z', 'z', 'Ž', 'ž', '?', 'ƒ', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', '?', '?', '?', '?', '?', '?'); |
| 1451 | $b = array('A', 'A', 'A', 'A', 'A', 'A', 'AE', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I', 'I', 'D', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y', 's', 'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'A', 'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'D', 'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o'); |
| 1452 | return strtolower(preg_replace(array('/[^a-zA-Z0-9 -]/', '/[ -]+/', '/^-|-$/'), array('', '-', ''), str_replace($a, $b, $str))); |
| 1453 | } |
| 1454 | |
| 1455 | public static function sanitize_options($value, $type = "") { |
| 1456 | $value = stripslashes($value); |
| 1457 | if($type == "int") { |
| 1458 | $value = filter_var($value, FILTER_SANITIZE_NUMBER_INT); |
| 1459 | } else if($type == "email") { |
| 1460 | $value = sanitize_email($value); |
| 1461 | } else { |
| 1462 | $value = sanitize_text_field($value); |
| 1463 | } |
| 1464 | return $value; |
| 1465 | } |
| 1466 | |
| 1467 | public function wcp_add_new_folder() |
| 1468 | { |
| 1469 | $response = array(); |
| 1470 | $response['status'] = 0; |
| 1471 | $response['error'] = 0; |
| 1472 | $response['login'] = 1; |
| 1473 | $response['data'] = array(); |
| 1474 | $response['message'] = ""; |
| 1475 | $postData = $_REQUEST; |
| 1476 | $errorCounter = 0; |
| 1477 | if (!current_user_can("manage_categories")) { |
| 1478 | $error = esc_html__("You have not permission to add folder", WCP_FOLDER); |
| 1479 | $errorCounter++; |
| 1480 | } else if (!isset($postData['name']) || empty($postData['name'])) { |
| 1481 | $error = esc_html__("Folder name can no be empty", WCP_FOLDER); |
| 1482 | $errorCounter++; |
| 1483 | } else if (!isset($postData['type']) || empty($postData['type'])) { |
| 1484 | $error = esc_html__("Your request is not valid", WCP_FOLDER); |
| 1485 | $errorCounter++; |
| 1486 | } else if (!isset($postData['nonce']) || empty($postData['nonce'])) { |
| 1487 | $response['login'] = 0; |
| 1488 | $error = esc_html__("Unable to create folder, Your request is not valid", WCP_FOLDER); |
| 1489 | $errorCounter++; |
| 1490 | } else { |
| 1491 | $type = self::sanitize_options($postData['type']); |
| 1492 | if(!wp_verify_nonce($postData['nonce'], 'wcp_folder_nonce_'.$type)) { |
| 1493 | $response['login'] = 0; |
| 1494 | $error = esc_html__("Unable to create folder, Your request is not valid", WCP_FOLDER); |
| 1495 | $errorCounter++; |
| 1496 | } |
| 1497 | } |
| 1498 | if ($errorCounter == 0) { |
| 1499 | $parent = isset($postData['parent_id']) && !empty($postData['parent_id']) ? $postData['parent_id'] : 0; |
| 1500 | $parent = self::sanitize_options($parent); |
| 1501 | $type = self::sanitize_options($postData['type']); |
| 1502 | $folder_type = self::get_custom_post_type($type); |
| 1503 | $term_name = self::sanitize_options($postData['name']); |
| 1504 | $term = term_exists($term_name, $folder_type, $parent); |
| 1505 | if (!(0 !== $term && null !== $term)) { |
| 1506 | $slug = self::create_slug_from_string($postData['name']) . "-" . time(); |
| 1507 | $result = wp_insert_term( |
| 1508 | $postData['name'], // the term |
| 1509 | $folder_type, // the taxonomy |
| 1510 | array( |
| 1511 | 'parent' => $parent, |
| 1512 | 'slug' => $slug |
| 1513 | ) |
| 1514 | ); |
| 1515 | if (!empty($result)) { |
| 1516 | $response['id'] = $result['term_id']; |
| 1517 | $response['status'] = 1; |
| 1518 | $order = isset($postData['order']) ? $postData['order'] : 0; |
| 1519 | $order = self::sanitize_options($order); |
| 1520 | update_term_meta($result['term_id'], "wcp_custom_order", $order); |
| 1521 | if ($parent != 0) { |
| 1522 | update_term_meta($parent, "is_active", 1); |
| 1523 | } |
| 1524 | $delete_nonce = wp_create_nonce('wcp_folder_delete_term_'.$result['term_id']); |
| 1525 | $rename_nonce = wp_create_nonce('wcp_folder_rename_term_'.$result['term_id']); |
| 1526 | $highlight_nonce = wp_create_nonce('wcp_folder_highlight_term_'.$result['term_id']); |
| 1527 | $term_nonce = wp_create_nonce('wcp_folder_term_'.$result['term_id']); |
| 1528 | $string = "<li data-nonce='{$term_nonce}' data-star='{$highlight_nonce}' data-rename='{$rename_nonce}' data-delete='{$delete_nonce}' data-slug='{$result['term_id']}' class='ui-state-default route new-folders' id='wcp_folder_{$result['term_id']}' data-folder-id='{$result['term_id']}'><h3 class='title is-new-item' id='title_{$result['term_id']}'><span class='nav-icon'><i class='wcp-icon folder-icon-arrow_right'></i><img src='".esc_url(WCP_FOLDER_URL."assets/images/move-option.png")."' class='move-folder-icon' ></span><span class='title-text'>{$postData['name']}</span> <span class='update-inline-record'></span><span class='star-icon'></span> </h3><span class='ui-icon'><i class='wcp-icon folder-icon-folder'></i></span> <ul class='space' id='space_{$result['term_id']}'>"; |
| 1529 | $string .= "</ul></li>"; |
| 1530 | $response['term_data'] = $string; |
| 1531 | $response['parent_id'] = $parent; |
| 1532 | $response['term_id'] = $result['term_id']; |
| 1533 | |
| 1534 | $is_active = 1; |
| 1535 | $folders = -1; |
| 1536 | if (!self::check_has_valid_key()) { |
| 1537 | $is_active = 0; |
| 1538 | $folders = self::total_term_folders(); |
| 1539 | } |
| 1540 | $response['is_key_active'] = $is_active; |
| 1541 | $response['folders'] = $folders; |
| 1542 | } else { |
| 1543 | $response['message'] = esc_html__("Error during server request", WCP_FOLDER); |
| 1544 | } |
| 1545 | } else { |
| 1546 | $response['error'] = 1; |
| 1547 | $response['message'] = esc_html__("Folder name is already exists", WCP_FOLDER); |
| 1548 | } |
| 1549 | } else { |
| 1550 | $response['error'] = 1; |
| 1551 | $response['message'] = $error; |
| 1552 | } |
| 1553 | echo json_encode($response); |
| 1554 | die; |
| 1555 | } |
| 1556 | |
| 1557 | public function is_for_this_post_type($post_type) |
| 1558 | { |
| 1559 | $post_types = get_option(WCP_FOLDER_VAR); |
| 1560 | $post_types = is_array($post_types)?$post_types:array(); |
| 1561 | return in_array($post_type, $post_types); |
| 1562 | } |
| 1563 | |
| 1564 | public function is_active_for_screen() |
| 1565 | { |
| 1566 | |
| 1567 | global $typenow, $current_screen; |
| 1568 | |
| 1569 | $postData = filter_input_array(INPUT_POST); |
| 1570 | |
| 1571 | if ((isset($postData['action']) && $postData['action'] == 'inline-save') && (isset($postData['post_type']) && self::is_for_this_post_type($postData['post_type']))) { |
| 1572 | return true; |
| 1573 | } |
| 1574 | global $current_screen; |
| 1575 | |
| 1576 | if (self::is_for_this_post_type($typenow) && ('edit' == $current_screen->base || 'upload' == $current_screen->base)) { |
| 1577 | return true; |
| 1578 | } |
| 1579 | |
| 1580 | $post_types = get_option(WCP_FOLDER_VAR); |
| 1581 | $post_types = is_array($post_types)?$post_types:array(); |
| 1582 | |
| 1583 | if(empty($typenow) && 'upload' == $current_screen->base ) { |
| 1584 | $typenow = "attachment"; |
| 1585 | if (self::is_for_this_post_type($typenow)) { |
| 1586 | return true; |
| 1587 | } |
| 1588 | } |
| 1589 | return false; |
| 1590 | } |
| 1591 | |
| 1592 | public function is_add_update_screen() |
| 1593 | { |
| 1594 | global $current_screen; |
| 1595 | $current_type = $current_screen->base; |
| 1596 | $action = $current_screen->action; |
| 1597 | $post_types = get_option(WCP_FOLDER_VAR); |
| 1598 | $post_types = is_array($post_types)?$post_types:array(); |
| 1599 | global $typenow; |
| 1600 | if (in_array($current_type, $post_types) && in_array($action, array("add", ""))) { |
| 1601 | $license_data = self::get_license_key_data(); |
| 1602 | |
| 1603 | $is_active = 1; |
| 1604 | $folders = -1; |
| 1605 | if (!self::check_has_valid_key()) { |
| 1606 | $is_active = 0; |
| 1607 | $folders = self::total_term_folders(); |
| 1608 | } |
| 1609 | $response['folders'] = $folders; |
| 1610 | $response['is_key_active'] = $is_active; |
| 1611 | } |
| 1612 | } |
| 1613 | |
| 1614 | public static function get_custom_post_type($post_type) |
| 1615 | { |
| 1616 | if ($post_type == "post") { |
| 1617 | return "post_folder"; |
| 1618 | } else if ($post_type == "page") { |
| 1619 | return "folder"; |
| 1620 | } else if ($post_type == "attachment") { |
| 1621 | return "media_folder"; |
| 1622 | } |
| 1623 | return $post_type . '_folder'; |
| 1624 | } |
| 1625 | |
| 1626 | public function admin_footer() |
| 1627 | { |
| 1628 | |
| 1629 | if (self::is_active_for_screen()) { |
| 1630 | global $typenow; |
| 1631 | |
| 1632 | $total_posts = self::get_total_posts($typenow); |
| 1633 | |
| 1634 | $total_empty = self::get_total_empty_posts($typenow); |
| 1635 | |
| 1636 | $folder_type = self::get_custom_post_type($typenow); |
| 1637 | $terms_data = WCP_Tree::get_full_tree_data($folder_type); |
| 1638 | $terms_html = WCP_Tree::get_option_data_for_select($folder_type); |
| 1639 | $form_html = WCP_Forms::get_form_html($terms_html); |
| 1640 | include_once dirname(dirname(__FILE__)) . WCP_DS . "/templates" . WCP_DS . "admin" . WCP_DS . "admin-content.php"; |
| 1641 | } |
| 1642 | |
| 1643 | global $pagenow; |
| 1644 | if ( 'plugins.php' !== $pagenow ) { |
| 1645 | |
| 1646 | } else { |
| 1647 | include_once dirname(dirname(__FILE__)) . WCP_DS . "/templates" . WCP_DS . "admin" . WCP_DS . "folder-deactivate-form.php"; |
| 1648 | } |
| 1649 | } |
| 1650 | |
| 1651 | public function get_total_posts($post_type = "") |
| 1652 | { |
| 1653 | global $typenow; |
| 1654 | if ($post_type == "") { |
| 1655 | $post_type = $typenow; |
| 1656 | } |
| 1657 | if ($typenow == "attachment") { |
| 1658 | return wp_count_posts($post_type)->inherit; |
| 1659 | } else { |
| 1660 | return wp_count_posts($post_type)->publish + wp_count_posts($post_type)->draft + wp_count_posts($post_type)->future + wp_count_posts($post_type)->private; |
| 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | public function get_total_empty_posts($post_type = "") |
| 1665 | { |
| 1666 | $taxonomy = self::get_custom_post_type($post_type); |
| 1667 | $args = array( |
| 1668 | 'posts_per_page' => -1, |
| 1669 | 'post_type' => $post_type, |
| 1670 | 'post_status' => 'inherit' |
| 1671 | ); |
| 1672 | if ($post_type != "attachment") { |
| 1673 | $args['post_status'] = array('publish', 'draft', 'future', 'private'); |
| 1674 | } |
| 1675 | $args['tax_query'] = array( |
| 1676 | array( |
| 1677 | 'taxonomy' => $taxonomy, |
| 1678 | 'operator' => 'NOT EXISTS', |
| 1679 | ), |
| 1680 | ); |
| 1681 | $result = get_posts($args); |
| 1682 | |
| 1683 | return (count($result)); |
| 1684 | } |
| 1685 | |
| 1686 | public function autoload() |
| 1687 | { |
| 1688 | $files = array( |
| 1689 | 'WCP_Tree_View' => WCP_DS . "includes" . WCP_DS . "tree.class.php", |
| 1690 | 'WCP_Form_View' => WCP_DS . "includes" . WCP_DS . "form.class.php", |
| 1691 | ); |
| 1692 | |
| 1693 | foreach ($files as $file) { |
| 1694 | if (file_exists(dirname(dirname(__FILE__)) . $file)) { |
| 1695 | include_once dirname(dirname(__FILE__)) . $file; |
| 1696 | } |
| 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | public function create_folder_terms() |
| 1701 | { |
| 1702 | $options = get_option(WCP_FOLDER_VAR); |
| 1703 | $options = is_array($options)?$options:array(); |
| 1704 | $old_plugin_status = 0; |
| 1705 | $posts = array(); |
| 1706 | if (!empty($options)) { |
| 1707 | foreach ($options as $option) { |
| 1708 | if (!(strpos($option, 'folder4') === false) && $old_plugin_status == 0) { |
| 1709 | $old_plugin_status = 1; |
| 1710 | } |
| 1711 | if (in_array($option, array("page", "post", "attachment"))) { |
| 1712 | $posts[] = str_replace("folder4", "", $option); |
| 1713 | } else { |
| 1714 | $posts[] = $option; |
| 1715 | } |
| 1716 | } |
| 1717 | if(!empty($posts)) { |
| 1718 | update_option(WCP_FOLDER_VAR, $posts); |
| 1719 | } |
| 1720 | } |
| 1721 | if ($old_plugin_status == 1) { |
| 1722 | update_option("folders_show_in_menu", "on"); |
| 1723 | $old_plugin_var = get_option("folder_old_plugin_status"); |
| 1724 | if (empty($old_plugin_var) || $old_plugin_var == null) { |
| 1725 | update_option("folder_old_plugin_status", "1"); |
| 1726 | } |
| 1727 | } |
| 1728 | $posts = get_option(WCP_FOLDER_VAR); |
| 1729 | if (!empty($posts)) { |
| 1730 | foreach ($posts as $post_type) { |
| 1731 | $labels = array( |
| 1732 | 'name' => esc_html__('Folders', WCP_FOLDER), |
| 1733 | 'singular_name' => esc_html__('Folder', WCP_FOLDER), |
| 1734 | 'all_items' => esc_html__('All Folders', WCP_FOLDER), |
| 1735 | 'edit_item' => esc_html__('Edit Folder', WCP_FOLDER), |
| 1736 | 'update_item' => esc_html__('Update Folder', WCP_FOLDER), |
| 1737 | 'add_new_item' => esc_html__('Add New Folder', WCP_FOLDER), |
| 1738 | 'new_item_name' => esc_html__('Add Folder Name', WCP_FOLDER), |
| 1739 | 'menu_name' => esc_html__('Folders', WCP_FOLDER), |
| 1740 | 'search_items' => esc_html__('Search Folders', WCP_FOLDER), |
| 1741 | 'parent_item' => esc_html__('Parent Folder', WCP_FOLDER), |
| 1742 | ); |
| 1743 | |
| 1744 | $args = array( |
| 1745 | 'label' => esc_html__('Folder', WCP_FOLDER), |
| 1746 | 'labels' => $labels, |
| 1747 | 'show_tagcloud' => false, |
| 1748 | 'hierarchical' => true, |
| 1749 | 'public' => false, |
| 1750 | 'show_ui' => true, |
| 1751 | 'show_in_menu' => false, |
| 1752 | 'show_in_rest' => true, |
| 1753 | 'show_admin_column' => true, |
| 1754 | 'update_count_callback' => '_update_generic_term_count', |
| 1755 | 'query_var' => true, |
| 1756 | 'rewrite' => false, |
| 1757 | ); |
| 1758 | |
| 1759 | $folder_post_type = self::get_custom_post_type($post_type); |
| 1760 | |
| 1761 | register_taxonomy( |
| 1762 | $folder_post_type, |
| 1763 | $post_type, |
| 1764 | $args |
| 1765 | ); |
| 1766 | } |
| 1767 | } |
| 1768 | |
| 1769 | $postData = filter_input_array(INPUT_POST); |
| 1770 | |
| 1771 | if(current_user_can("manage_categories")) { |
| 1772 | if (isset($postData['folders_show_in_menu']) && !empty($postData['folders_show_in_menu'])) { |
| 1773 | $show_menu = "off"; |
| 1774 | if ($postData['folders_show_in_menu'] == "on") { |
| 1775 | $show_menu = "on"; |
| 1776 | } |
| 1777 | update_option("folders_show_in_menu", $show_menu); |
| 1778 | } |
| 1779 | |
| 1780 | if (isset($postData['folders_settings1'])) { |
| 1781 | $posts = array(); |
| 1782 | if (isset($postData['folders_settings']) && is_array($postData['folders_settings'])) { |
| 1783 | foreach ($postData['folders_settings'] as $key => $val) { |
| 1784 | $posts[] = $val; |
| 1785 | } |
| 1786 | } |
| 1787 | update_option("folders_settings", $posts); |
| 1788 | } |
| 1789 | } |
| 1790 | |
| 1791 | $old_version = get_option("folder_old_plugin_status"); |
| 1792 | if($old_version !== false && $old_version == 1) { |
| 1793 | $total_folders = get_option("folder_old_plugin_folder_status"); |
| 1794 | if($total_folders === false) { |
| 1795 | $total = self::total_term_folders(); |
| 1796 | if($total <= 10) { |
| 1797 | $total = 10; |
| 1798 | }; |
| 1799 | update_option("folder_old_plugin_folder_status", $total); |
| 1800 | self::$folders = $total; |
| 1801 | } else { |
| 1802 | self::$folders = $total_folders; |
| 1803 | } |
| 1804 | } |
| 1805 | |
| 1806 | $total_folders = get_option("folder_old_plugin_folder_status"); |
| 1807 | if($total_folders === false) { |
| 1808 | self::$folders = 10; |
| 1809 | } else { |
| 1810 | self::$folders = $total_folders; |
| 1811 | } |
| 1812 | } |
| 1813 | |
| 1814 | function searchForId($id, $menu) |
| 1815 | { |
| 1816 | if ($menu) { |
| 1817 | foreach ($menu as $key => $val) { |
| 1818 | if (array_key_exists(2, $val)) { |
| 1819 | $stripVal = explode('=', $val[2]); |
| 1820 | } |
| 1821 | if (array_key_exists(1, $stripVal)) { |
| 1822 | $stripVal = $stripVal[1]; |
| 1823 | } |
| 1824 | if ($stripVal === $id) { |
| 1825 | return $key; |
| 1826 | } |
| 1827 | } |
| 1828 | } |
| 1829 | } |
| 1830 | |
| 1831 | function create_menu_for_folders() |
| 1832 | { |
| 1833 | global $menu; |
| 1834 | self::check_and_set_post_type(); |
| 1835 | |
| 1836 | $folder_types = get_option("folders_settings"); |
| 1837 | if (empty($folder_types)) { |
| 1838 | return; |
| 1839 | } |
| 1840 | |
| 1841 | foreach ($folder_types as $type) { |
| 1842 | $itemKey = self::searchForId($type, $menu); |
| 1843 | switch (true) { |
| 1844 | case ($type == 'attachment'): |
| 1845 | $itemKey = 10; |
| 1846 | $edit = 'upload.php'; |
| 1847 | break; |
| 1848 | case ($type === 'post'): |
| 1849 | $edit = 'edit.php'; |
| 1850 | $itemKey = 5; |
| 1851 | break; |
| 1852 | default: |
| 1853 | $edit = 'edit.php'; |
| 1854 | break; |
| 1855 | } |
| 1856 | |
| 1857 | $folder = $type == 'attachment' ? 'media' : $type; |
| 1858 | $upper = $type == 'attachment' ? 'Media' : ucwords(str_replace(array('-', '_'), ' ', $type)); |
| 1859 | if ($type == 'page') { |
| 1860 | $tax_slug = 'folder'; |
| 1861 | } else { |
| 1862 | $tax_slug = $folder . '_folder'; |
| 1863 | } |
| 1864 | |
| 1865 | |
| 1866 | if ($type == 'attachment') { |
| 1867 | add_menu_page('Media Folders', 'Media Folders', 'publish_pages', "{$edit}?type=folder", false, 'dashicons-portfolio', "{$itemKey}.5"); |
| 1868 | } else { |
| 1869 | add_menu_page($upper . ' Folders', "{$upper} Folders", 'publish_pages', "{$edit}?post_type={$type}&type=folder", false, 'dashicons-portfolio', "{$itemKey}.5"); |
| 1870 | } |
| 1871 | $terms = get_terms($tax_slug, array( |
| 1872 | 'hide_empty' => true, |
| 1873 | 'parent' => 0, |
| 1874 | 'orderby' => 'meta_value_num', |
| 1875 | 'order' => 'ASC', |
| 1876 | 'hierarchical' => false, |
| 1877 | 'meta_query' => [[ |
| 1878 | 'key' => 'wcp_custom_order', |
| 1879 | 'type' => 'NUMERIC', |
| 1880 | ]] |
| 1881 | ) |
| 1882 | ); |
| 1883 | |
| 1884 | if ($terms) { |
| 1885 | foreach ($terms as $term) { |
| 1886 | if ($type == 'attachment') { |
| 1887 | add_submenu_page("{$edit}?type=folder", $term->name, $term->name, 'publish_pages', "{$edit}?post_type=attachment&media_folder={$term->slug}", false); |
| 1888 | } else { |
| 1889 | add_submenu_page("{$edit}?post_type={$type}&type=folder", $term->name, $term->name, 'publish_pages', "{$edit}?post_type={$type}&{$tax_slug}={$term->slug}", false); |
| 1890 | } |
| 1891 | } |
| 1892 | } |
| 1893 | } |
| 1894 | } |
| 1895 | |
| 1896 | function folders_admin_styles() |
| 1897 | { |
| 1898 | if (self::is_active_for_screen()) { |
| 1899 | wp_register_style('wcp-folders-fa', plugin_dir_url(dirname(__FILE__)) . 'assets/css/folder-icon.css', array(), WCP_FOLDER_VERSION); |
| 1900 | wp_enqueue_style('wcp-folders-fa'); |
| 1901 | wp_register_style('wcp-folders-admin', plugin_dir_url(dirname(__FILE__)) . 'assets/css/design.css', array(), WCP_FOLDER_VERSION); |
| 1902 | wp_enqueue_style('wcp-folders-admin'); |
| 1903 | } |
| 1904 | wp_register_style('wcp-css-handle', false); |
| 1905 | wp_enqueue_style('wcp-css-handle'); |
| 1906 | $css = " |
| 1907 | .wcp-folder-upgrade-button {color: #FF5983; font-weight: bold;} |
| 1908 | "; |
| 1909 | if (self::is_active_for_screen()) { |
| 1910 | global $typenow; |
| 1911 | $width = get_option("wcp_dynamic_width_for_" . $typenow); |
| 1912 | $width = esc_attr($width); |
| 1913 | $display_status = "wcp_dynamic_display_status_" . $typenow; |
| 1914 | $display_status = get_option($display_status); |
| 1915 | if($display_status != "hide") { |
| 1916 | if (!empty($width) && is_numeric($width)) { |
| 1917 | $css .= ".wcp-content{width:{$width}px}"; |
| 1918 | if (function_exists('is_rtl') && is_rtl()) { |
| 1919 | $css .= "html[dir='rtl'] body.wp-admin #wpcontent {padding-right:" . ($width + 20) . "px}"; |
| 1920 | $css .= "html[dir='rtl'] body.wp-admin #wpcontent {padding-left:0px}"; |
| 1921 | } else { |
| 1922 | $css .= "body.wp-admin #wpcontent {padding-left:" . ($width + 20) . "px}"; |
| 1923 | } |
| 1924 | } |
| 1925 | } else { |
| 1926 | if (function_exists('is_rtl') && is_rtl()) { |
| 1927 | $css .= "html[dir='rtl'] body.wp-admin #wpcontent {padding-right:20px}"; |
| 1928 | $css .= "html[dir='rtl'] body.wp-admin #wpcontent {padding-left:0px}"; |
| 1929 | } else { |
| 1930 | $css .= "body.wp-admin #wpcontent {padding-left:20px}"; |
| 1931 | } |
| 1932 | } |
| 1933 | } |
| 1934 | wp_add_inline_style('wcp-css-handle', $css); |
| 1935 | |
| 1936 | if (self::is_active_for_screen()) { |
| 1937 | global $typenow; |
| 1938 | add_filter('views_edit-' . $typenow, array($this, 'wcp_check_for_child_folders')); |
| 1939 | } |
| 1940 | |
| 1941 | } |
| 1942 | |
| 1943 | function wcp_check_for_child_folders($content) |
| 1944 | { |
| 1945 | $termId = 0; |
| 1946 | global $typenow; |
| 1947 | $post_type = self::get_custom_post_type($typenow); |
| 1948 | if (isset($_GET[$post_type]) && !empty($_GET[$post_type])) { |
| 1949 | $term = $_GET[$post_type]; |
| 1950 | $term = get_term_by("slug", $term, $post_type); |
| 1951 | if (!empty($term)) { |
| 1952 | $termId = $term->term_id; |
| 1953 | } |
| 1954 | } |
| 1955 | $terms = get_terms($post_type, array( |
| 1956 | 'hide_empty' => false, |
| 1957 | 'parent' => $termId, |
| 1958 | 'orderby' => 'meta_value_num', |
| 1959 | 'order' => 'ASC', |
| 1960 | 'hierarchical' => false, |
| 1961 | 'update_count_callback' => '_update_generic_term_count', |
| 1962 | 'meta_query' => [[ |
| 1963 | 'key' => 'wcp_custom_order', |
| 1964 | 'type' => 'NUMERIC', |
| 1965 | ]] |
| 1966 | )); |
| 1967 | echo '<div class="tree-structure" id="list-folder-' . $termId . '" data-id="' . $termId . '">'; |
| 1968 | echo '<ul>'; |
| 1969 | foreach ($terms as $term) { |
| 1970 | $status = get_term_meta($term->term_id, "is_highlighted", true); |
| 1971 | ?> |
| 1972 | <li class="grid-view" data-id="<?php echo $term->term_id ?>" id="folder_<?php echo $term->term_id ?>"> |
| 1973 | <div class="folder-item is-folder" data-id="<?php echo $term->term_id ?>"> |
| 1974 | <a title='<?php echo $term->name ?>' id="folder_view_<?php echo $term->term_id ?>" |
| 1975 | class="folder-view <?php echo ($status == 1) ? "is-high" : "" ?>" |
| 1976 | data-id="<?php echo $term->term_id ?>"> |
| 1977 | <span class="folder item-name"><span id="wcp_folder_text_<?php echo $term->term_id ?>" |
| 1978 | class="folder-title"><?php echo $term->name ?></span></span> |
| 1979 | <!--<span class="folder-option"></span>--> |
| 1980 | </a> |
| 1981 | </div> |
| 1982 | </li> |
| 1983 | <?php |
| 1984 | } |
| 1985 | echo '</ul>'; |
| 1986 | echo '<div class="clear clearfix"></div>'; |
| 1987 | echo '</div>'; |
| 1988 | if(!empty($content) && is_array($content)) { |
| 1989 | echo '<ul class="subsubsub">'; |
| 1990 | foreach($content as $k=>$v) { |
| 1991 | echo "<li class='{$k}'>{$v}</li>"; |
| 1992 | } |
| 1993 | echo '</ul>'; |
| 1994 | } |
| 1995 | } |
| 1996 | |
| 1997 | function folders_admin_scripts() |
| 1998 | { |
| 1999 | if (self::is_active_for_screen()) { |
| 2000 | global $typenow; |
| 2001 | wp_register_script('wcp-folders-alert', plugin_dir_url(dirname(__FILE__)) . 'assets/js/livequery.min.js', array(), WCP_FOLDER_VERSION); |
| 2002 | wp_register_script('wcp-folders-custom', plugin_dir_url(dirname(__FILE__)) . 'assets/js/custom.js', array('jquery', 'jquery-ui-resizable', 'jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-sortable', 'backbone'), WCP_FOLDER_VERSION); |
| 2003 | |
| 2004 | if ($typenow == "attachment") { |
| 2005 | $admin_url = admin_url("upload.php?post_type=attachment&media_folder="); |
| 2006 | } else { |
| 2007 | $admin_url = admin_url("edit.php?post_type=" . $typenow); |
| 2008 | if (isset($_GET['s']) && !empty($_GET['s'])) { |
| 2009 | $admin_url .= "&s=" . $_GET['s']; |
| 2010 | } |
| 2011 | $post_type = self::get_custom_post_type($typenow); |
| 2012 | $admin_url .= "&{$post_type}="; |
| 2013 | } |
| 2014 | |
| 2015 | $is_active = 1; |
| 2016 | $folders = -1; |
| 2017 | if (!self::check_has_valid_key()) { |
| 2018 | $is_active = 0; |
| 2019 | $folders = self::total_term_folders(); |
| 2020 | } |
| 2021 | $register_url = admin_url("admin.php?page=wcp_folders_upgrade"); |
| 2022 | |
| 2023 | $is_rtl = 0; |
| 2024 | if ( function_exists( 'is_rtl' ) && is_rtl() ) { |
| 2025 | $is_rtl = 1; |
| 2026 | } |
| 2027 | |
| 2028 | $can_manage_folder = current_user_can("manage_categories")?1:0; |
| 2029 | $width = get_option("wcp_dynamic_width_for_" . $typenow); |
| 2030 | $width = empty($width)||!is_numeric($width)?310:$width; |
| 2031 | $post_type = self::get_custom_post_type($typenow); |
| 2032 | $taxonomy_status = 0; |
| 2033 | $selected_taxonomy = ""; |
| 2034 | if(!isset($_GET[$post_type]) || empty($_GET[$post_type])) { |
| 2035 | $taxonomy_status = 1; |
| 2036 | } else if(isset($_GET[$post_type]) && !empty($_GET[$post_type])) { |
| 2037 | $selected_taxonomy = $_GET[$post_type]; |
| 2038 | } |
| 2039 | wp_localize_script('wcp-folders-custom', 'wcp_settings', array( |
| 2040 | 'ajax_url' => admin_url('admin-ajax.php'), |
| 2041 | 'post_type' => $typenow, |
| 2042 | 'page_url' => $admin_url, |
| 2043 | 'ajax_image' => plugin_dir_url(dirname(__FILE__)) . "assets/images/ajax-loader.gif", |
| 2044 | 'is_key_active' => $is_active, |
| 2045 | 'folders' => $folders, |
| 2046 | 'register_url' => $register_url, |
| 2047 | 'isRTL' => $is_rtl, |
| 2048 | 'nonce' => wp_create_nonce('wcp_folder_nonce_'.$typenow), |
| 2049 | 'can_manage_folder' => $can_manage_folder, |
| 2050 | 'folder_width' => $width, |
| 2051 | 'taxonomy_status' => $taxonomy_status, |
| 2052 | 'selected_taxonomy' => $selected_taxonomy |
| 2053 | )); |
| 2054 | |
| 2055 | wp_enqueue_script('wcp-folders-alert'); |
| 2056 | wp_enqueue_script('wcp-folders-custom'); |
| 2057 | |
| 2058 | } else { |
| 2059 | self::is_add_update_screen(); |
| 2060 | } |
| 2061 | } |
| 2062 | |
| 2063 | public function plugin_action_links($links) |
| 2064 | { |
| 2065 | array_unshift($links, '<a href="' . admin_url("admin.php?page=wcp_folders_settings") . '" >' . esc_html__('Settings', WCP_FOLDER) . '</a>'); |
| 2066 | $links['need_help'] = '<a target="_blank" href="https://premio.io/help/folders/?utm_source=pluginspage" >'.__( 'Need help?', WCP_FOLDER ).'</a>'; |
| 2067 | $links['pro'] = '<a class="wcp-folder-upgrade-button" href="'.admin_url("admin.php?page=wcp_folders_upgrade").'" >'.__( 'Upgrade', WCP_FOLDER ).'</a>'; |
| 2068 | return $links; |
| 2069 | } |
| 2070 | |
| 2071 | public static function get_instance() |
| 2072 | { |
| 2073 | if (empty(self::$instance)) { |
| 2074 | self::$instance = new WCP_Folders(); |
| 2075 | } |
| 2076 | return self::$instance; |
| 2077 | } |
| 2078 | |
| 2079 | public function check_and_set_post_type() { |
| 2080 | $options = get_option(WCP_FOLDER_VAR); |
| 2081 | $options = is_array($options)?$options:array(); |
| 2082 | $old_plugin_status = 0; |
| 2083 | $post_array = array(); |
| 2084 | if (!empty($options)) { |
| 2085 | foreach ($options as $key=>$val) { |
| 2086 | if (!(strpos($key, 'folders4') === false) && $old_plugin_status == 0) { |
| 2087 | $old_plugin_status = 1; |
| 2088 | } |
| 2089 | if (in_array($key, array("folders4page", "folders4post", "folders4attachment"))) { |
| 2090 | $post_array[] = str_replace("folders4", "", $key); |
| 2091 | } |
| 2092 | } |
| 2093 | } else { |
| 2094 | $post_array = array("page", "post", "attachment"); |
| 2095 | } |
| 2096 | if ($old_plugin_status == 1) { |
| 2097 | update_option("folders_show_in_menu", "on"); |
| 2098 | $old_plugin_var = get_option("folder_old_plugin_status"); |
| 2099 | if (empty($old_plugin_var) || $old_plugin_var == null) { |
| 2100 | update_option("folder_old_plugin_status", "1"); |
| 2101 | } |
| 2102 | update_option(WCP_FOLDER_VAR, $post_array); |
| 2103 | self::set_default_values_if_not_exists(); |
| 2104 | } |
| 2105 | if (!empty($post_array) && get_option(WCP_FOLDER_VAR) === false) { |
| 2106 | update_option(WCP_FOLDER_VAR, $post_array); |
| 2107 | update_option("folders_show_in_menu", "off"); |
| 2108 | } |
| 2109 | } |
| 2110 | |
| 2111 | public static function activate() |
| 2112 | { |
| 2113 | update_option("folders_show_in_menu", "off"); |
| 2114 | update_option("folder_redirect_status", 1); |
| 2115 | } |
| 2116 | |
| 2117 | public static function get_total_term_folders() |
| 2118 | { |
| 2119 | return self::total_term_folders(); |
| 2120 | } |
| 2121 | |
| 2122 | function folders_register_settings() |
| 2123 | { |
| 2124 | register_setting('folders_settings', 'folders_settings1', 'folders_settings_validate'); |
| 2125 | register_setting('default_folders', 'default_folders'); |
| 2126 | |
| 2127 | self::check_and_set_post_type(); |
| 2128 | |
| 2129 | |
| 2130 | |
| 2131 | $option = get_option("folder_redirect_status", true); |
| 2132 | if ($option == 1) { |
| 2133 | update_option("folder_redirect_status", 0); |
| 2134 | wp_redirect(admin_url("admin.php?page=wcp_folders_settings")); |
| 2135 | exit; |
| 2136 | } |
| 2137 | } |
| 2138 | |
| 2139 | function wcp_manage_columns_head($defaults, $d = "") |
| 2140 | { |
| 2141 | global $typenow; |
| 2142 | $type = $typenow; |
| 2143 | if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'inline-save') { |
| 2144 | $type = self::sanitize_options($_REQUEST['post_type']); |
| 2145 | } |
| 2146 | |
| 2147 | $options = get_option("folders_settings"); |
| 2148 | |
| 2149 | // echo "<pre>"; print_r($defaults); |
| 2150 | if (is_array($options) && in_array($type, $options)) { |
| 2151 | $columns = array( |
| 2152 | 'wcp_move' => '<div class="wcp-move-multiple wcp-col" title="' . esc_html__('Move selected items', WCP_FOLDER) . '"><span class="dashicons dashicons-move"></span><div class="wcp-items"></div></div>', |
| 2153 | ) + $defaults; |
| 2154 | return $columns; |
| 2155 | } |
| 2156 | return $defaults; |
| 2157 | } |
| 2158 | |
| 2159 | function wcp_manage_columns_content($column_name, $post_ID) |
| 2160 | { |
| 2161 | $postIDs = self::$postIds; |
| 2162 | if(!is_array($postIDs)) { |
| 2163 | $postIDs = array(); |
| 2164 | } |
| 2165 | if(!in_array($post_ID, $postIDs)) { |
| 2166 | $postIDs[] = $post_ID; |
| 2167 | self::$postIds = $postIDs; |
| 2168 | global $typenow; |
| 2169 | $type = $typenow; |
| 2170 | if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'inline-save') { |
| 2171 | $type = self::sanitize_options($_REQUEST['post_type']); |
| 2172 | } |
| 2173 | |
| 2174 | $options = get_option("folders_settings"); |
| 2175 | if (is_array($options) && in_array($type, $options)) { |
| 2176 | if ($column_name == 'wcp_move') { |
| 2177 | $title = get_the_title(); |
| 2178 | if (strlen($title) > 20) { |
| 2179 | $title = substr($title, 0, 20) . "..."; |
| 2180 | } |
| 2181 | echo "<div class='wcp-move-file' data-id='{$post_ID}'><span class='wcp-move dashicons dashicons-move' data-id='{$post_ID}'></span><span class='wcp-item' data-object-id='{$post_ID}'>" . $title . "</span></div>"; |
| 2182 | } |
| 2183 | } |
| 2184 | } |
| 2185 | } |
| 2186 | |
| 2187 | function taxonomy_archive_exclude_children($query) |
| 2188 | { |
| 2189 | $options = get_option("folders_settings"); |
| 2190 | if (!empty($options)) { |
| 2191 | $taxonomy_slugs = array(); |
| 2192 | foreach ($options as $option) { |
| 2193 | $taxonomy_slugs[] = self::get_custom_post_type($option); |
| 2194 | } |
| 2195 | if (!empty($taxonomy_slugs)) { |
| 2196 | $i = 0; |
| 2197 | foreach ($query->tax_query->queries as $tax_query_item) { |
| 2198 | if (empty($taxonomy_slugs) || (isset($tax_query_item['taxonomy']) && in_array($tax_query_item['taxonomy'], $taxonomy_slugs))) { |
| 2199 | $query->tax_query->queries[$i]['include_children'] = 0; |
| 2200 | } |
| 2201 | } |
| 2202 | } |
| 2203 | } |
| 2204 | } |
| 2205 | |
| 2206 | public function admin_menu() |
| 2207 | { |
| 2208 | // Add menu item for settings page |
| 2209 | $page_title = esc_html__('Folders', WCP_FOLDER); |
| 2210 | $menu_title = esc_html__('Folders Settings', WCP_FOLDER); |
| 2211 | $capability = 'manage_options'; |
| 2212 | $menu_slug = 'wcp_folders_settings'; |
| 2213 | $callback = array($this, "wcp_folders_settings"); |
| 2214 | $icon_url = 'dashicons-category'; |
| 2215 | $position = 99; |
| 2216 | |
| 2217 | add_menu_page($page_title, $menu_title, $capability, $menu_slug, $callback, $icon_url, $position); |
| 2218 | |
| 2219 | add_submenu_page( |
| 2220 | $menu_slug, |
| 2221 | esc_html__('Upgrade to Pro', WCP_FOLDER), |
| 2222 | esc_html__('Upgrade to Pro', WCP_FOLDER), |
| 2223 | 'manage_options', |
| 2224 | 'wcp_folders_upgrade', |
| 2225 | array($this, 'wcp_folders_upgrade') |
| 2226 | ); |
| 2227 | |
| 2228 | self::check_and_set_post_type(); |
| 2229 | |
| 2230 | $show_menu = get_option("folders_show_in_menu", true); |
| 2231 | if ($show_menu == "on") { |
| 2232 | self::create_menu_for_folders(); |
| 2233 | } |
| 2234 | } |
| 2235 | |
| 2236 | public function wcp_folders_upgrade() |
| 2237 | { |
| 2238 | self::set_default_values_if_not_exists(); |
| 2239 | include_once dirname(dirname(__FILE__)) . "/templates/admin/upgrade-to-pro.php"; |
| 2240 | } |
| 2241 | |
| 2242 | public function wcp_folders_settings() |
| 2243 | { |
| 2244 | self::set_default_values_if_not_exists(); |
| 2245 | $is_shown = get_option("folder_update_message"); |
| 2246 | if($is_shown === false) { |
| 2247 | include_once dirname(dirname(__FILE__)) . "/templates/admin/update.php"; |
| 2248 | } else { |
| 2249 | $options = get_option('folders_settings'); |
| 2250 | $options = (empty($options) || !is_array($options)) ?array():$options; |
| 2251 | $post_types = get_post_types( array( 'public' => true ), 'objects' ); |
| 2252 | $terms_data = array(); |
| 2253 | foreach ($post_types as $post_type) { |
| 2254 | if(in_array($post_type->name, $options)) { |
| 2255 | $term = $post_type->name; |
| 2256 | $term = self::get_custom_post_type($term); |
| 2257 | $categories = self::get_terms_hierarchical($term); |
| 2258 | $terms_data[$post_type->name] = $categories; |
| 2259 | } else { |
| 2260 | $terms_data[$post_type->name] = array(); |
| 2261 | } |
| 2262 | } |
| 2263 | |
| 2264 | include_once dirname(dirname(__FILE__)) . "/templates/admin/general-settings.php"; |
| 2265 | } |
| 2266 | |
| 2267 | } |
| 2268 | |
| 2269 | public function set_default_values_if_not_exists() |
| 2270 | { |
| 2271 | $options = get_option(WCP_FOLDER_VAR); |
| 2272 | $options = is_array($options)?$options:array(); |
| 2273 | foreach ($options as $option) { |
| 2274 | $post_type = self::get_custom_post_type($option); |
| 2275 | $terms = get_terms($post_type, array( |
| 2276 | 'hide_empty' => false, |
| 2277 | ) |
| 2278 | ); |
| 2279 | foreach ($terms as $term) { |
| 2280 | $order = get_term_meta($term->term_id, "wcp_custom_order", true); |
| 2281 | if (empty($order) || $order == null) { |
| 2282 | update_term_meta($term->term_id, "wcp_custom_order", "1"); |
| 2283 | } |
| 2284 | |
| 2285 | } |
| 2286 | } |
| 2287 | } |
| 2288 | } |