classes
5 months ago
compatibility
1 year ago
metaboxes
5 months ago
admin-ajax.php
1 month ago
admin-hooks.php
1 year ago
admin-the-functions.php
1 year ago
index.php
2 years ago
admin-the-functions.php
483 lines
| 1 | <?php |
| 2 | // admin related functions |
| 3 | |
| 4 | // Include advanced metabox tab |
| 5 | require_once( 'metaboxes/metabox-fields-general-advanced.php' ); |
| 6 | |
| 7 | |
| 8 | /** |
| 9 | * Searchs and removes unexpected fields and sections from metabox hub models |
| 10 | * |
| 11 | * @param array $models The list of metabox models |
| 12 | * @param array $args The metabox field and sections which should be dropped |
| 13 | * @return List of models |
| 14 | */ |
| 15 | function auxin_remove_from_metabox_hub( $models, $args = array() ){ |
| 16 | |
| 17 | if( empty( $models ) ){ |
| 18 | return; |
| 19 | } |
| 20 | |
| 21 | $defaults = array( |
| 22 | 'model_ids' => array(), // the list of model IDs to be dropped |
| 23 | 'field_ids' => array() // the list of field IDs to be dropped |
| 24 | ); |
| 25 | |
| 26 | $args = wp_parse_args( $args, $defaults ); |
| 27 | |
| 28 | $args['model_ids' ] = (array) $args['model_ids']; |
| 29 | $args['field_ids' ] = (array) $args['field_ids']; |
| 30 | |
| 31 | foreach ( $models as $model_info_index => $model_info ) { |
| 32 | // if similar field id detected, drop it |
| 33 | if( in_array( $model_info['model']->id, $args['model_ids' ] ) ){ |
| 34 | unset( $models[ $model_info_index ] ); |
| 35 | continue; |
| 36 | } |
| 37 | |
| 38 | $fields = $model_info['model']->fields; |
| 39 | |
| 40 | if( ! empty( $fields ) ){ |
| 41 | foreach ( $fields as $field_index => $field ) { |
| 42 | if( empty( $field["id"] ) ){ |
| 43 | continue; |
| 44 | } |
| 45 | if( in_array( $field["id"], $args['field_ids' ] ) ){ |
| 46 | unset( $fields[ $field_index ] ); |
| 47 | $models[ $model_info_index ]['model']->fields = $fields; |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | return $models; |
| 54 | } |
| 55 | |
| 56 | |
| 57 | |
| 58 | /*----------------------------------------------------------------------------*/ |
| 59 | /* TGMPA plugin update functions |
| 60 | /*----------------------------------------------------------------------------*/ |
| 61 | |
| 62 | /** |
| 63 | * Count the number of bundled plugins having new updates |
| 64 | * |
| 65 | * @return int|bool The umber of plugins having update |
| 66 | */ |
| 67 | function auxin_count_bundled_plugins_have_update(){ |
| 68 | // Check transient |
| 69 | if ( false === ( $tgmpa_counter = auxin_get_transient( 'auxin_count_bundled_plugins_have_update' ) ) ) { |
| 70 | // Get instance |
| 71 | $tgmpa_instance = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) ); |
| 72 | // Reset Counter |
| 73 | $tgmpa_counter = 0; |
| 74 | // Check plugins list |
| 75 | foreach ( $tgmpa_instance->plugins as $slug => $plugin ) { |
| 76 | if ( $plugin['source_type'] === 'bundled' && $tgmpa_instance->is_plugin_active( $slug ) && $tgmpa_instance->does_plugin_have_update( $slug ) ) { |
| 77 | $tgmpa_counter++; |
| 78 | } |
| 79 | } |
| 80 | auxin_set_transient( 'auxin_count_bundled_plugins_have_update', $tgmpa_counter, 12 * HOUR_IN_SECONDS ); |
| 81 | } |
| 82 | |
| 83 | return $tgmpa_counter; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Get tgmpa install plugins page |
| 88 | */ |
| 89 | function auxin_get_tgmpa_plugins_page(){ |
| 90 | // Get instance |
| 91 | $tgmpa_instance = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) ); |
| 92 | return $tgmpa_instance->install_plugins_page(); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get our custom updates list |
| 97 | * |
| 98 | * @return array |
| 99 | */ |
| 100 | function auxin_get_update_list(){ |
| 101 | // General objects |
| 102 | $bulk_list = new stdClass; |
| 103 | $last_checked = new stdClass; |
| 104 | |
| 105 | // Get plugin updates info |
| 106 | $bulk_list->plugins = new stdClass; |
| 107 | $last_checked->plugins_time = 0; |
| 108 | if ( current_user_can( 'update_plugins' ) ) { |
| 109 | $update_plugins = auxin_get_transient( 'auxin_update_plugins' ); |
| 110 | if ( isset( $update_plugins->response ) && ! empty( $update_plugins->response ) ){ |
| 111 | $bulk_list->plugins = $update_plugins->response; |
| 112 | } |
| 113 | if( isset( $update_plugins->last_checked ) ){ |
| 114 | $last_checked->plugins_time = $update_plugins->last_checked; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // Get theme updates info |
| 119 | $bulk_list->themes = new stdClass; |
| 120 | $last_checked->themes_time = 0; |
| 121 | if ( current_user_can( 'update_themes' ) ) { |
| 122 | $update_themes = auxin_get_transient( 'auxin_update_themes' ); |
| 123 | if ( isset( $update_themes->response ) && ! empty( $update_themes->response ) ){ |
| 124 | $bulk_list->themes = $update_themes->response; |
| 125 | } |
| 126 | if( isset( $update_themes->last_checked ) ){ |
| 127 | $last_checked->themes_time = $update_themes->last_checked; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // Set last checked in human diff time |
| 132 | $get_last_checked_item = $last_checked->themes_time >= $last_checked->plugins_time ? $last_checked->themes_time : $last_checked->plugins_time; |
| 133 | $bulk_list->last_checked = $get_last_checked_item !== 0 ? human_time_diff( $get_last_checked_item ) : __( 'a long time', 'auxin-elements' ); |
| 134 | // Set total updates count |
| 135 | $bulk_list->total_updates = count( (array) $bulk_list->plugins ) + count( (array) $bulk_list ->themes ); |
| 136 | |
| 137 | return apply_filters( 'auxin_get_total_updates_list', $bulk_list ); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Get total updates number |
| 142 | * |
| 143 | * @return integer |
| 144 | */ |
| 145 | function auxin_get_total_updates(){ |
| 146 | $last_update = auxin_get_update_list(); |
| 147 | return isset( $last_update->total_updates ) ? $last_update->total_updates : 0; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Set The Default Category for post type |
| 152 | * |
| 153 | * @return integer |
| 154 | */ |
| 155 | function auxin_set_uncategorized_term ( $post_id, $post ) { |
| 156 | $taxonomies = get_object_taxonomies( $post->post_type ); |
| 157 | foreach ( $taxonomies as $taxonomy ) { |
| 158 | if ( $taxonomy == 'language' || $taxonomy == 'post_translations' ) { |
| 159 | continue; |
| 160 | } |
| 161 | $terms = wp_get_post_terms( $post_id, $taxonomy ); |
| 162 | if ( empty( $terms ) ) { |
| 163 | wp_set_object_terms( $post_id, 'uncategorized', $taxonomy ); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Import requested template from server |
| 170 | * |
| 171 | */ |
| 172 | if ( ! function_exists('auxin_template_importer') ) { |
| 173 | function auxin_template_importer( $template_ID = '', $template_type = '', $action = '' ) { |
| 174 | |
| 175 | if ( ! user_can( wp_get_current_user(), 'manage_options' ) ) { |
| 176 | return [ |
| 177 | 'success' => false, |
| 178 | 'data' => [ |
| 179 | 'message' => __( 'Sorry. You don\'t have sufficient permission to import a template!', 'auxin-elements') |
| 180 | ] |
| 181 | ]; |
| 182 | } |
| 183 | |
| 184 | $template_ID = absint( trim( $template_ID ) ); |
| 185 | $template_type = sanitize_text_field( trim( $template_type ) ); |
| 186 | |
| 187 | if ( empty( $template_ID ) || empty( $template_type ) ) |
| 188 | return [ |
| 189 | 'success' => false, |
| 190 | 'data' => [ |
| 191 | 'message' => __( 'Template ID or type is required.', 'auxin-elements') |
| 192 | ] |
| 193 | ]; |
| 194 | |
| 195 | $template_imported_id_key = false; |
| 196 | if ( is_numeric( $template_ID ) ) { |
| 197 | // Get All Templates data |
| 198 | $templates_data = Auxin_Welcome::get_instance()->get_demo_list('templates'); |
| 199 | |
| 200 | if ( empty( $templates_data ) ) { |
| 201 | return [ |
| 202 | 'success' => false, |
| 203 | 'data' => [ |
| 204 | 'message' => __( "An error occurred while updating templates library.", 'auxin-elements' ) |
| 205 | ] |
| 206 | ]; |
| 207 | } |
| 208 | |
| 209 | // Find data of selected template |
| 210 | $has_template = false; |
| 211 | foreach ( $templates_data['templates'] as $key => $template_info ) { |
| 212 | if ( $template_info['id'] == $template_ID && $template_info['type'] == $template_type ) { |
| 213 | $has_template = true; |
| 214 | $template = $template_info; |
| 215 | break; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | if ( ! $has_template ){ |
| 220 | return [ |
| 221 | 'success' => false, |
| 222 | 'data' => [ |
| 223 | 'message' => __( 'Template Not Found.', 'auxin-elements' ) |
| 224 | ] |
| 225 | ]; |
| 226 | } |
| 227 | |
| 228 | // Import Template |
| 229 | $template_data_key = sanitize_key( "auxin_template_kit_{$template_type}_data_for_origin_id_{$template_ID}" ); |
| 230 | $template_imported_id_key = sanitize_key( "auxin_template_kit_new_id_for_origin_{$template_ID}" ); |
| 231 | |
| 232 | $template_imported_new_id = auxin_get_transient( $template_imported_id_key ); |
| 233 | |
| 234 | // Check if the template is already imported and was not deleted |
| 235 | if ( false !== $template_imported_new_id && ! in_array( get_post_status( $template_imported_new_id ), [ false, 'trash' ] ) ) { |
| 236 | return [ |
| 237 | 'success' => true, |
| 238 | 'data' => [ |
| 239 | 'message' => __( 'Template is already imported.', 'auxin-elements' ), |
| 240 | 'data' => false, |
| 241 | 'isImported' => true, |
| 242 | ] |
| 243 | ]; |
| 244 | } |
| 245 | |
| 246 | if( false === $template_data = auxin_get_transient( $template_data_key ) ){ |
| 247 | |
| 248 | // Retrieve the template data |
| 249 | $template_data = Auxin_Demo_Importer::get_instance()->fetch_template_data( $template_ID ); |
| 250 | |
| 251 | if( ! $template_data ){ |
| 252 | return [ |
| 253 | 'success' => false, |
| 254 | 'data' => [ |
| 255 | 'message' => __( 'Connection error, please check your connection.', 'auxin-elements' ) |
| 256 | ] |
| 257 | ]; |
| 258 | } |
| 259 | |
| 260 | // Set transient for 48h |
| 261 | auxin_set_transient( $template_data_key, $template_data, WEEK_IN_SECONDS ); |
| 262 | } |
| 263 | } else { |
| 264 | return [ |
| 265 | 'success' => false, |
| 266 | 'data' => [ |
| 267 | 'message' => __( 'Template ID must be numeric or valid filepath.', 'auxin-elements') |
| 268 | ] |
| 269 | ]; |
| 270 | } |
| 271 | |
| 272 | |
| 273 | $post_type = "elementor_library"; |
| 274 | $args = [ |
| 275 | 'post_title' => wp_strip_all_tags( $template['title'] ), |
| 276 | 'post_status' => 'publish', |
| 277 | 'post_type' => $post_type |
| 278 | ]; |
| 279 | |
| 280 | // Inserting template into database |
| 281 | $post_id = wp_insert_post( $args ); |
| 282 | |
| 283 | if( ! is_wp_error( $post_id ) ){ |
| 284 | |
| 285 | // update menu name to use menu created by user |
| 286 | if ( $action == 'update_menu' ) { |
| 287 | if ( has_nav_menu('header-primary') && $template_type == 'header'){ |
| 288 | $location = 'header-primary'; |
| 289 | } else if ( has_nav_menu('footer') && $template_type == 'footer' ){ |
| 290 | $location = 'footer'; |
| 291 | } else { |
| 292 | $location = ''; |
| 293 | } |
| 294 | |
| 295 | $header_menu = ( ! empty( $location ) ) ? get_term( get_nav_menu_locations()[$location], 'nav_menu' ) : ''; |
| 296 | $template_data = ( ! empty( $header_menu ) ) ? preg_replace( '#"menu_slug":".+?(?=")"#', '"menu_slug":"'.$header_menu->name.'"', $template_data ) : $template_data; |
| 297 | } |
| 298 | |
| 299 | $json_content = json_decode( $template_data , true ); |
| 300 | $elementor_version = defined( 'ELEMENTOR_VERSION' ) ? ELEMENTOR_VERSION : '2.9.0'; |
| 301 | |
| 302 | update_post_meta( $post_id, '_elementor_edit_mode', 'builder' ); |
| 303 | update_post_meta( $post_id, '_elementor_data', $json_content['content'] ); |
| 304 | update_post_meta( $post_id, '_elementor_version', $elementor_version ); |
| 305 | |
| 306 | // Set page template |
| 307 | if( ! empty( $template['page_tmpl'] ) ){ |
| 308 | update_post_meta( $post_id, '_wp_page_template', $template['page_tmpl'] ); |
| 309 | } |
| 310 | |
| 311 | // Set template type |
| 312 | if( $post_type === 'elementor_library' ) { |
| 313 | update_post_meta( $post_id, '_elementor_template_type', $template_type ); |
| 314 | } |
| 315 | |
| 316 | if ( $template_imported_id_key ) { |
| 317 | auxin_set_transient( $template_imported_id_key, $post_id, MONTH_IN_SECONDS ); |
| 318 | } |
| 319 | |
| 320 | // Force to generate CSS file for this template |
| 321 | Auxin_Demo_Importer::get_instance()->maybe_flush_post( $post_id ); |
| 322 | |
| 323 | // NOTE: Here we can set new header or footer template az main header or footer |
| 324 | return [ |
| 325 | 'success' => true, |
| 326 | 'data' => [ |
| 327 | 'message' => __( 'Template Imported Successfully', 'auxin-elements'), |
| 328 | 'postId' => $post_id, |
| 329 | 'isImported' => false, |
| 330 | 'postTitle' => get_the_title( $post_id ) |
| 331 | ] |
| 332 | ]; |
| 333 | |
| 334 | } else { |
| 335 | |
| 336 | return [ |
| 337 | 'success' => false, |
| 338 | 'data' => [ |
| 339 | 'message' => __( 'Error while saving the template.', 'auxin-elements') |
| 340 | ] |
| 341 | ]; |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Import requested template from server |
| 348 | * |
| 349 | */ |
| 350 | if ( ! function_exists('auxin_template_importer_by_path') ) { |
| 351 | function auxin_template_importer_by_path( $template_path = '', $template_type = '', $action = '' ) { |
| 352 | |
| 353 | if ( ! user_can( wp_get_current_user(), 'manage_options' ) ) { |
| 354 | return [ |
| 355 | 'success' => false, |
| 356 | 'data' => [ |
| 357 | 'message' => __( 'Sorry. You don\'t have sufficient permission to import a template!', 'auxin-elements') |
| 358 | ] |
| 359 | ]; |
| 360 | } |
| 361 | |
| 362 | $template_path = sanitize_text_field( trim( $template_path ) ); |
| 363 | $template_type = sanitize_text_field( trim( $template_type ) ); |
| 364 | |
| 365 | if ( empty( $template_path ) || empty( $template_type ) ) |
| 366 | return [ |
| 367 | 'success' => false, |
| 368 | 'data' => [ |
| 369 | 'message' => __( 'Template path or type is required.', 'auxin-elements') |
| 370 | ] |
| 371 | ]; |
| 372 | |
| 373 | $template_imported_id_key = false; |
| 374 | |
| 375 | if ( file_exists( $template_path ) ) { |
| 376 | // make sure uploaded file has json extension |
| 377 | if( pathinfo( $template_path, PATHINFO_EXTENSION) !== 'json' ){ |
| 378 | return [ |
| 379 | 'success' => false, |
| 380 | 'data' => [ |
| 381 | 'message' => __( 'Not a valid template file extension.', 'auxin-elements' ) |
| 382 | ] |
| 383 | ]; |
| 384 | } |
| 385 | |
| 386 | // make sure uploaded file is JSON |
| 387 | $template_data = file_get_contents( $template_path ); |
| 388 | if ( ! auxin_is_json( $template_data ) ) { |
| 389 | return [ |
| 390 | 'success' => false, |
| 391 | 'data' => [ |
| 392 | 'message' => __( 'Not a valid template file type.', 'auxin-elements' ) |
| 393 | ] |
| 394 | ]; |
| 395 | } |
| 396 | |
| 397 | $template = [ |
| 398 | 'title' => basename( $template_path, '.json' ) |
| 399 | ]; |
| 400 | |
| 401 | } else { |
| 402 | return [ |
| 403 | 'success' => false, |
| 404 | 'data' => [ |
| 405 | 'message' => __( 'Template ID must be numeric or valid filepath.', 'auxin-elements') |
| 406 | ] |
| 407 | ]; |
| 408 | } |
| 409 | |
| 410 | |
| 411 | $post_type = "elementor_library"; |
| 412 | $args = [ |
| 413 | 'post_title' => wp_strip_all_tags( $template['title'] ), |
| 414 | 'post_status' => 'publish', |
| 415 | 'post_type' => $post_type |
| 416 | ]; |
| 417 | |
| 418 | // Inserting template into database |
| 419 | $post_id = wp_insert_post( $args ); |
| 420 | |
| 421 | if( ! is_wp_error( $post_id ) ){ |
| 422 | |
| 423 | // update menu name to use menu created by user |
| 424 | if ( $action == 'update_menu' ) { |
| 425 | if ( has_nav_menu('header-primary') && $template_type == 'header'){ |
| 426 | $location = 'header-primary'; |
| 427 | } else if ( has_nav_menu('footer') && $template_type == 'footer' ){ |
| 428 | $location = 'footer'; |
| 429 | } else { |
| 430 | $location = ''; |
| 431 | } |
| 432 | |
| 433 | $header_menu = ( ! empty( $location ) ) ? get_term( get_nav_menu_locations()[$location], 'nav_menu' ) : ''; |
| 434 | $template_data = ( ! empty( $header_menu ) ) ? preg_replace( '#"menu_slug":".+?(?=")"#', '"menu_slug":"'.$header_menu->name.'"', $template_data ) : $template_data; |
| 435 | } |
| 436 | |
| 437 | $json_content = json_decode( $template_data , true ); |
| 438 | $elementor_version = defined( 'ELEMENTOR_VERSION' ) ? ELEMENTOR_VERSION : '2.9.0'; |
| 439 | |
| 440 | update_post_meta( $post_id, '_elementor_edit_mode', 'builder' ); |
| 441 | update_post_meta( $post_id, '_elementor_data', $json_content['content'] ); |
| 442 | update_post_meta( $post_id, '_elementor_version', $elementor_version ); |
| 443 | |
| 444 | // Set page template |
| 445 | if( ! empty( $template['page_tmpl'] ) ){ |
| 446 | update_post_meta( $post_id, '_wp_page_template', $template['page_tmpl'] ); |
| 447 | } |
| 448 | |
| 449 | // Set template type |
| 450 | if( $post_type === 'elementor_library' ) { |
| 451 | update_post_meta( $post_id, '_elementor_template_type', $template_type ); |
| 452 | } |
| 453 | |
| 454 | if ( $template_imported_id_key ) { |
| 455 | auxin_set_transient( $template_imported_id_key, $post_id, MONTH_IN_SECONDS ); |
| 456 | } |
| 457 | |
| 458 | // Force to generate CSS file for this template |
| 459 | Auxin_Demo_Importer::get_instance()->maybe_flush_post( $post_id ); |
| 460 | |
| 461 | // NOTE: Here we can set new header or footer template az main header or footer |
| 462 | return [ |
| 463 | 'success' => true, |
| 464 | 'data' => [ |
| 465 | 'message' => __( 'Template Imported Successfully', 'auxin-elements'), |
| 466 | 'postId' => $post_id, |
| 467 | 'isImported' => false, |
| 468 | 'postTitle' => get_the_title( $post_id ) |
| 469 | ] |
| 470 | ]; |
| 471 | |
| 472 | } else { |
| 473 | |
| 474 | return [ |
| 475 | 'success' => false, |
| 476 | 'data' => [ |
| 477 | 'message' => __( 'Error while saving the template.', 'auxin-elements') |
| 478 | ] |
| 479 | ]; |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 |