class-affiliate.php
1 year ago
class-email-signup.php
1 year ago
class-help.php
1 year ago
class-polylang.php
1 year ago
class-review-box.php
1 year ago
class-upgrade-box.php
1 year ago
class-wpml.php
1 year ago
folders.class.php
1 year ago
form.class.php
1 year ago
form.fields.php
1 year ago
import.export.class.php
1 year ago
media.replace.php
1 year ago
notifications.class.php
1 year ago
plugins.class.php
1 year ago
svg.class.php
1 year ago
tree.class.php
1 year ago
import.export.class.php
365 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class Folders Plugins import/export |
| 4 | * |
| 5 | * @author : Premio <contact@premio.io> |
| 6 | * @license : GPL2 |
| 7 | * */ |
| 8 | |
| 9 | if (! defined('ABSPATH')) { |
| 10 | exit; |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | * Class Folders_Import_Export |
| 15 | * |
| 16 | * This class is responsible for managing import/export related to folders. |
| 17 | */ |
| 18 | class Folders_Import_Export |
| 19 | { |
| 20 | /** |
| 21 | * Class constructor. |
| 22 | * |
| 23 | * This method is called when an instance of the class is created. It is used to initialize the object. |
| 24 | * |
| 25 | * @return void |
| 26 | */ |
| 27 | |
| 28 | public $default_settings = null; |
| 29 | |
| 30 | |
| 31 | public function __construct() { |
| 32 | |
| 33 | //Export folders |
| 34 | add_action('wp_ajax_folders_export', [$this, 'folders_export']); |
| 35 | add_action('wp_ajax_folders_import', [$this, 'folders_import']); |
| 36 | |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Export folders data |
| 41 | * |
| 42 | * @since 1.0.0 |
| 43 | * @access public |
| 44 | * @return $response |
| 45 | */ |
| 46 | public function folders_export() |
| 47 | { |
| 48 | $response = []; |
| 49 | $response['status'] = 0; |
| 50 | $response['error'] = 0; |
| 51 | $response['data'] = []; |
| 52 | $response['message'] = ""; |
| 53 | $postData = filter_input_array(INPUT_POST); |
| 54 | $errorCounter = 0; |
| 55 | |
| 56 | if (!isset($postData['nonce']) || empty($postData['nonce'])) { |
| 57 | $response['message'] = esc_html__("Your request is not valid", 'folders'); |
| 58 | $errorCounter++; |
| 59 | } else { |
| 60 | $nonce = WCP_Folders::sanitize_options($postData['nonce']); |
| 61 | if (!wp_verify_nonce($nonce, 'wcp_folders_export')) { |
| 62 | $response['message'] = esc_html__("Your request is not valid", 'folders'); |
| 63 | $errorCounter++; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | if ($errorCounter == 0) { |
| 68 | $filename = 'export_form' . time() . '.csv'; |
| 69 | $data_rows = array(); |
| 70 | $header_row = array( |
| 71 | 'id', |
| 72 | 'Post type', |
| 73 | 'Title', |
| 74 | 'parent', |
| 75 | 'children' |
| 76 | ); |
| 77 | |
| 78 | $post_setting = apply_filters("check_for_folders_post_args", ["show_in_menu" => 1]); |
| 79 | $posts =[]; |
| 80 | $post_types = get_post_types( $post_setting, 'objects' ); |
| 81 | foreach($post_types as $post =>$value) { |
| 82 | $folder_type = WCP_Folders::get_custom_post_type($post); |
| 83 | $post_folders = self::get_terms_hierarchical_download($folder_type); |
| 84 | if(in_array($post, ['page', 'post', 'media']) || count($post_folders) > 0) { |
| 85 | $posts[] = [ |
| 86 | 'post_type' => $post, |
| 87 | 'post_title' => $value->label, |
| 88 | 'folders' => $post_folders |
| 89 | ]; |
| 90 | } |
| 91 | } |
| 92 | $response['data'] = $posts; |
| 93 | } |
| 94 | echo wp_json_encode($response); |
| 95 | die; |
| 96 | |
| 97 | }//end folders_export() |
| 98 | |
| 99 | /** |
| 100 | * Get folders by hierarchy |
| 101 | * |
| 102 | * @since 1.0.0 |
| 103 | * @access public |
| 104 | */ |
| 105 | public static function get_terms_hierarchical_download($taxonomy) |
| 106 | { |
| 107 | $customize_folders = get_option('customize_folders'); |
| 108 | $foldersByUser = isset($customize_folders['folders_by_users']) && $customize_folders['folders_by_users'] == "on" ? true : false; |
| 109 | $folder_by_user = 0; |
| 110 | if ($foldersByUser) { |
| 111 | $user_id = get_current_user_id(); |
| 112 | $folder_by_user = $user_id; |
| 113 | if (function_exists("wp_get_current_user")) { |
| 114 | $user = wp_get_current_user(); |
| 115 | $user_roles = (array) $user->roles; |
| 116 | $user_roles = !is_array($user_roles) ? [] : $user_roles; |
| 117 | if (in_array("administrator", $user_roles)) { |
| 118 | $folder_by_user = 0; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | $args = [ |
| 124 | 'taxonomy' => $taxonomy, |
| 125 | 'hide_empty' => false, |
| 126 | 'parent' => 0, |
| 127 | 'orderby' => 'meta_value_num', |
| 128 | 'order' => 'ASC', |
| 129 | 'hierarchical' => false, |
| 130 | 'update_count_callback' => '_update_generic_term_count', |
| 131 | ]; |
| 132 | |
| 133 | if ($folder_by_user) { |
| 134 | $args['meta_query'] = [ |
| 135 | [ |
| 136 | 'key' => 'wcp_custom_order', |
| 137 | 'type' => 'NUMERIC', |
| 138 | ], |
| 139 | [ |
| 140 | 'key' => 'created_by', |
| 141 | 'type' => '=', |
| 142 | 'value' => $folder_by_user, |
| 143 | ], |
| 144 | ]; |
| 145 | } else { |
| 146 | $args['meta_query'] = [ |
| 147 | [ |
| 148 | 'key' => 'wcp_custom_order', |
| 149 | 'type' => 'NUMERIC', |
| 150 | ], |
| 151 | ]; |
| 152 | } |
| 153 | |
| 154 | $terms = get_terms($args); |
| 155 | $hierarchical_terms = []; |
| 156 | if (!empty($terms)) { |
| 157 | foreach ($terms as $term) { |
| 158 | if (!empty($term) && isset($term->term_id)) { |
| 159 | $folder_info = get_term_meta($term->term_id, "folder_info", true); |
| 160 | $folder_info = shortcode_atts([ |
| 161 | 'is_sticky' => 0, |
| 162 | 'is_high' => 0, |
| 163 | 'is_locked' => 0, |
| 164 | 'is_active' => 0, |
| 165 | 'has_color' => '' |
| 166 | ], $folder_info); |
| 167 | |
| 168 | $main_term = ['name' => $term->name,'properties' => $folder_info , 'children' => []]; |
| 169 | $main_term['children'] = self::get_child_terms_download($taxonomy,$main_term['children'], $term->term_id, "-", $folder_by_user); |
| 170 | array_push($hierarchical_terms,$main_term); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | return $hierarchical_terms; |
| 175 | |
| 176 | }//end get_terms_hierarchical() |
| 177 | |
| 178 | /** |
| 179 | * Get child folders |
| 180 | * |
| 181 | * @since 1.0.0 |
| 182 | * @access public |
| 183 | */ |
| 184 | public static function get_child_terms_download($taxonomy,$main_term, $term_id, $separator="-", $folder_by_user=0) |
| 185 | { |
| 186 | $args = [ |
| 187 | 'taxonomy' => $taxonomy, |
| 188 | 'hide_empty' => false, |
| 189 | 'parent' => $term_id, |
| 190 | 'orderby' => 'meta_value_num', |
| 191 | 'order' => 'ASC', |
| 192 | 'hierarchical' => false, |
| 193 | 'update_count_callback' => '_update_generic_term_count', |
| 194 | ]; |
| 195 | if ($folder_by_user) { |
| 196 | $args['meta_query'] = [ |
| 197 | [ |
| 198 | 'key' => 'wcp_custom_order', |
| 199 | 'type' => 'NUMERIC', |
| 200 | ], |
| 201 | [ |
| 202 | 'key' => 'created_by', |
| 203 | 'type' => '=', |
| 204 | 'value' => $folder_by_user, |
| 205 | ], |
| 206 | ]; |
| 207 | } else { |
| 208 | $args['meta_query'] = [ |
| 209 | [ |
| 210 | 'key' => 'wcp_custom_order', |
| 211 | 'type' => 'NUMERIC', |
| 212 | ], |
| 213 | ]; |
| 214 | } |
| 215 | |
| 216 | |
| 217 | $terms = get_terms($args); |
| 218 | $hierarchical_terms_1 = []; |
| 219 | if (!empty($terms)) { |
| 220 | foreach ($terms as $term) { |
| 221 | if (isset($term->name)) { |
| 222 | $main_term['name'] = $term->name; |
| 223 | $folder_info = get_term_meta($term->term_id, "folder_info", true); |
| 224 | $folder_info = shortcode_atts([ |
| 225 | 'is_sticky' => 0, |
| 226 | 'is_high' => 0, |
| 227 | 'is_locked' => 0, |
| 228 | 'is_active' => 0, |
| 229 | 'has_color' => '' |
| 230 | ], $folder_info); |
| 231 | $main_term['properties'] = $folder_info; |
| 232 | $main_term['children'] = []; |
| 233 | $main_term['children'] = self::get_child_terms_download($taxonomy,$main_term['children'],$term->term_id, $separator."-"); |
| 234 | array_push($hierarchical_terms_1,$main_term); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | return $hierarchical_terms_1; |
| 240 | |
| 241 | }//end get_child_terms() |
| 242 | |
| 243 | /** |
| 244 | * Import folders data |
| 245 | * |
| 246 | * @since 1.0.0 |
| 247 | * @access public |
| 248 | * @return $response |
| 249 | */ |
| 250 | public function folders_import() |
| 251 | { |
| 252 | $response = []; |
| 253 | $response['status'] = 0; |
| 254 | $response['error'] = 0; |
| 255 | $response['data'] = []; |
| 256 | $response['message'] = ""; |
| 257 | $postData = filter_input_array(INPUT_POST); |
| 258 | $errorCounter = 0; |
| 259 | |
| 260 | if (!isset($postData['nonce']) || empty($postData['nonce'])) { |
| 261 | $response['message'] = esc_html__("Your request is not valid", 'folders'); |
| 262 | $errorCounter++; |
| 263 | } else { |
| 264 | $nonce = WCP_Folders::sanitize_options($postData['nonce']); |
| 265 | if (!wp_verify_nonce($nonce, 'wcp_folders_import')) { |
| 266 | $response['message'] = esc_html__("Your request is not valid", 'folders'); |
| 267 | $errorCounter++; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | $success = true; |
| 272 | if ($errorCounter == 0) { |
| 273 | $fileContent = WCP_Folders::sanitize_options($postData['uploaded_data']); |
| 274 | $fileContent = json_decode($fileContent,true); |
| 275 | foreach($fileContent as $content) { |
| 276 | $post_type = $content['post_type']; |
| 277 | $folders = $content['folders']; |
| 278 | $folder_type = WCP_Folders::get_custom_post_type($post_type); |
| 279 | $success = self::insert_imported_folders($folders,$folder_type); |
| 280 | } |
| 281 | } |
| 282 | if($success) { |
| 283 | $response['status'] = 1; |
| 284 | $response['error'] = 0; |
| 285 | $response['message'] = "Data imported successfully"; |
| 286 | } |
| 287 | echo wp_json_encode($response); |
| 288 | die; |
| 289 | }//end folders_import() |
| 290 | |
| 291 | public function insert_imported_folders($folders,$folder_type) { |
| 292 | $success = true; |
| 293 | foreach($folders as $folder) { |
| 294 | $folder_name = trim($folder['name']); |
| 295 | |
| 296 | $term_id = term_exists($folder_name, $folder_type, 0); |
| 297 | if(empty($term_id)) { |
| 298 | $slug = WCP_Folders::create_slug_from_string($folder_name) . "-" . time(); |
| 299 | $result = wp_insert_term( |
| 300 | $folder_name, |
| 301 | $folder_type, |
| 302 | [ |
| 303 | 'parent' => 0, |
| 304 | 'slug' => $slug, |
| 305 | ] |
| 306 | ); |
| 307 | if(!is_wp_error($result)) { |
| 308 | if(isset($result['term_id']) && isset($folder['properties']) && is_array($folder['properties']) && !empty($folder['properties'])) { |
| 309 | add_term_meta($result['term_id'], "folder_info", $folder['properties']); |
| 310 | } |
| 311 | if(isset($folder['children']) && !empty($folder['children'])) { |
| 312 | $success = self::insert_imported_folders_child($folder['children'],$folder_type,0); |
| 313 | } |
| 314 | } |
| 315 | } else { |
| 316 | if($term_id && isset($folder['properties']) && is_array($folder['properties']) && !empty($folder['properties'])) { |
| 317 | add_term_meta($term_id, "folder_info", $folder['properties']); |
| 318 | } |
| 319 | if(isset($folder['children']) && !empty($folder['children'])) { |
| 320 | $success = self::insert_imported_folders_child($folder['children'],$folder_type,0); |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | return 1; |
| 325 | } |
| 326 | |
| 327 | public function insert_imported_folders_child($folders,$folder_type,$parent_id) { |
| 328 | foreach($folders as $folder) { |
| 329 | $folder_name = trim($folder['name']); |
| 330 | $term_id = term_exists($folder_name, $folder_type, 0); |
| 331 | if(empty($term_id)) { |
| 332 | $slug = WCP_Folders::create_slug_from_string($folder_name) . "-" . time(); |
| 333 | $result = wp_insert_term( |
| 334 | $folder_name, |
| 335 | $folder_type, |
| 336 | [ |
| 337 | 'parent' => $parent_id, |
| 338 | 'slug' => $slug, |
| 339 | ] |
| 340 | ); |
| 341 | if(!is_wp_error($result)) { |
| 342 | if(isset($result['term_id']) && isset($folder['properties']) && is_array($folder['properties']) && !empty($folder['properties'])) { |
| 343 | add_term_meta($result['term_id'], "folder_info", $folder['properties']); |
| 344 | } |
| 345 | if(isset($folder['children']) && !empty($folder['children'])) { |
| 346 | $success = self::insert_imported_folders_child($folder['children'],$folder_type,0); |
| 347 | } |
| 348 | } |
| 349 | } else { |
| 350 | if($term_id && isset($folder['properties']) && is_array($folder['properties']) && !empty($folder['properties'])) { |
| 351 | add_term_meta($result['term_id'], "folder_info", $folder['properties']); |
| 352 | } |
| 353 | if(isset($folder['children']) && !empty($folder['children'])) { |
| 354 | $success = self::insert_imported_folders_child($folder['children'],$folder_type,0); |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | return 1; |
| 359 | } |
| 360 | |
| 361 | } |
| 362 | if(class_exists("Folders_Import_Export")) { |
| 363 | $Folders_Import_Export = new Folders_Import_Export(); |
| 364 | } |
| 365 |