Repository navigation
Expand file tree
/
Copy pathmodule.php
More file actions
568 lines (486 loc) · 17.9 KB
/
Copy pathmodule.php
File metadata and controls
568 lines (486 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
<?php
namespace ElementorPro\Modules\CustomCode;
use Elementor\Core\Admin\Menu\Admin_Menu_Manager;
use Elementor\Core\Documents_Manager;
use Elementor\Icons_Manager;
use Elementor\Modules\EditorOne\Classes\Menu_Data_Provider;
use Elementor\Settings;
use Elementor\TemplateLibrary\Source_Local;
use Elementor\Utils;
use ElementorPro\Base\Editor_One_Trait;
use ElementorPro\Base\Module_Base;
use ElementorPro\License\API;
use ElementorPro\Modules\CustomCode\AdminMenuItems\Custom_Code_Menu_Item;
use ElementorPro\Modules\CustomCode\AdminMenuItems\Custom_Code_Promotion_Menu_Item;
use ElementorPro\Modules\CustomCode\EditorOneMenuItems\Editor_One_Custom_Code_Menu_Item;
use ElementorPro\Modules\CustomCode\EditorOneMenuItems\Editor_One_Custom_Code_Promotion;
use ElementorPro\Modules\CustomCode\ImportExport\Import_Export as Custom_Code_Import_Export;
use ElementorPro\Modules\CustomCode\ImportExportCustomization\Import_Export_Customization as Custom_Code_Import_Export_Customization;
use ElementorPro\Modules\ThemeBuilder\Classes\Conditions_Manager;
use ElementorPro\Modules\ThemeBuilder\Classes\Locations_Manager;
use ElementorPro\Plugin;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class Module extends Module_Base {
use Editor_One_Trait;
const CAPABILITY = 'manage_options';
const CPT = 'elementor_snippet';
const MODULE_NAME = 'custom_code';
const DOCUMENT_TYPE = 'code_snippet';
const ADDITIONAL_COLUMN_INSTANCES = 'instances';
const MENU_SLUG = 'edit.php?post_type=' . self::CPT;
const PROMOTION_MENU_SLUG = 'e-custom-code';
/**
* @var \ElementorPro\Modules\CustomCode\Custom_Code_Metabox
*/
public $meta_box;
public function __construct() {
parent::__construct();
$this->meta_box = new Custom_Code_Metabox();
$this->actions();
if ( $this->can_use_custom_code() ) {
$this->register_custom_post_type();
$this->register_metabox();
}
$this->register_import_export();
}
public function get_name() {
return 'custom-code';
}
private function actions() {
if ( $this->can_use_custom_code() ) {
add_action( 'elementor/documents/register', function ( $documents_manager ) {
return $this->register_documents( $documents_manager );
} );
add_action( 'elementor/theme/register_locations', function ( $location_manager ) {
return $this->register_location( $location_manager );
} );
add_action( 'init', function () {
$this->register_rest_meta_fields();
} );
}
add_action( 'elementor/admin/menu/register', function ( Admin_Menu_Manager $admin_menu_manager ) {
if ( $this->is_editor_one_active() ) {
return;
}
$this->add_admin_menu( $admin_menu_manager );
} );
// TODO: BC - Remove after `Admin_Menu_Manager` will be the standard.
add_action( 'admin_menu', function () {
if ( did_action( 'elementor/admin/menu/register' ) ) {
return;
}
$menu_title = esc_html__( 'Custom Code', 'elementor-pro' );
add_submenu_page(
Settings::PAGE_ID,
$menu_title,
$menu_title,
self::CAPABILITY,
static::MENU_SLUG
);
}, /* After custom icons */ 51 );
add_action( 'elementor/editor-one/menu/register', function ( Menu_Data_Provider $menu_data_provider ) {
if ( $this->can_use_custom_code() ) {
$menu_data_provider->register_menu( new Editor_One_Custom_Code_Menu_Item() );
} else {
$menu_data_provider->register_menu( new Editor_One_Custom_Code_Promotion() );
}
} );
add_action( 'current_screen', function () {
if ( ! is_admin() ) {
return;
}
$current_screen = get_current_screen();
if ( 'edit-' . self::CPT === $current_screen->id ) {
$this->admin_ui_actions();
} elseif ( self::CPT === $current_screen->id ) {
// Enqueue assets.
add_action( 'admin_enqueue_scripts', function () {
$this->enqueue_assets();
}, 0 /* elementor-app-base styles should be loaded in early stages */ );
}
} );
add_filter( 'elementor/editor-one/admin-edit-post-types', function ( array $post_types ) {
$post_types[] = self::CPT;
return $post_types;
} );
$this->frontend_actions();
}
private function admin_ui_actions() {
// Show blank 'custom code' snippets list.
add_action( 'manage_posts_extra_tablenav', function ( $which ) {
return $this->maybe_render_blank_state( $which );
} );
// Mange post columns.
add_filter( 'manage_posts_columns', function ( $columns ) {
return $this->manage_posts_columns( $columns );
} );
add_action( 'manage_posts_custom_column', function ( $column_name, $post_id ) {
return $this->manage_posts_custom_column( $column_name, $post_id );
}, 10, 2 );
}
private function frontend_actions() {
// Print snippets.
add_action( 'wp_head', function () {
$this->print_snippets( Custom_Code_Metabox::OPTION_LOCATION_HEAD );
} );
add_action( 'wp_body_open', function () {
$this->print_snippets( Custom_Code_Metabox::OPTION_LOCATION_BODY_START );
} );
add_action( 'wp_footer', function () {
$this->print_snippets( Custom_Code_Metabox::OPTION_LOCATION_BODY_END );
}, 21 /* After 'wp_print_footer_scripts' */ );
}
private function register_custom_post_type() {
$labels = [
'name' => esc_html__( 'Custom Code', 'elementor-pro' ),
'singular_name' => esc_html__( 'Custom Code', 'elementor-pro' ),
'add_new' => esc_html__( 'Add new', 'elementor-pro' ),
'add_new_item' => esc_html__( 'New code', 'elementor-pro' ),
'edit_item' => esc_html__( 'Edit code', 'elementor-pro' ),
];
register_post_type( self::CPT, [
'labels' => $labels,
'public' => false,
'rewrite' => false,
'show_ui' => true,
'show_in_menu' => false,
'show_in_nav_menus' => false,
'exclude_from_search' => true,
'show_in_rest' => true,
'capability_type' => 'post',
'hierarchical' => false,
'supports' => [ 'title', 'author', 'custom-fields' ],
'capabilities' => [
'publish_posts' => 'manage_options',
'edit_posts' => 'manage_options',
'edit_others_posts' => 'manage_options',
'delete_posts' => 'manage_options',
'delete_others_posts' => 'manage_options',
'read_private_posts' => 'manage_options',
'edit_post' => 'manage_options',
'delete_post' => 'manage_options',
'read_post' => 'manage_options',
],
] );
// Handling custom post type messages.
add_filter( 'post_updated_messages', function( $messages ) {
if ( self::CPT === get_post_type() ) {
$post = get_post();
// Thanks 'WooCommerce' for the example.
$messages[ self::CPT ] = [
'', // Not in use.
__( 'Custom code updated.', 'elementor-pro' ),
__( 'Custom field updated.', 'elementor-pro' ),
__( 'Custom field deleted.', 'elementor-pro' ),
__( 'Custom code updated.', 'elementor-pro' ),
__( 'Revision restored.', 'elementor-pro' ),
__( 'Custom code published.', 'elementor-pro' ),
__( 'Custom code saved.', 'elementor-pro' ),
__( 'Custom code submitted.', 'elementor-pro' ),
sprintf(
/* translators: %s: The scheduled date. */
__( 'Custom code scheduled for %s.', 'elementor-pro' ),
'<strong>' . date_i18n( esc_html__( 'M j, Y @ G:i', 'elementor-pro' ), strtotime( $post->post_date ) ) . '</strong>'
),
__( 'Custom code draft updated.', 'elementor-pro' ),
];
}
return $messages;
} );
add_filter( 'bulk_post_updated_messages', function ( $messages, $counters ) {
$current_screen = get_current_screen();
if ( $current_screen && self::CPT === $current_screen->post_type ) {
$messages[ self::CPT ] = [
/* translators: %s: Number of custom code items. */
'updated' => _n( '%s custom code updated.', '%s custom codes updated.', $counters['updated'], 'elementor-pro' ),
/* translators: %s: Number of custom code items. */
'locked' => _n( '%s custom code cannot be not updated, someone else is editing it.', '%s custom codes cannot be not updated, someone else is editing them.', $counters['locked'], 'elementor-pro' ),
/* translators: %s: Number of custom code items. */
'deleted' => _n( '%s custom code permanently deleted.', '%s custom codes permanently deleted.', $counters['deleted'], 'elementor-pro' ),
/* translators: %s: Number of custom code items. */
'trashed' => _n( '%s custom code moved to trash.', '%s custom codes moved to trash.', $counters['trashed'], 'elementor-pro' ),
/* translators: %s: Number of custom code items. */
'untrashed' => _n( '%s custom code restored.', '%s custom code restored.', $counters['untrashed'], 'elementor-pro' ),
];
}
return $messages;
}, 10, 12 );
}
/**
* Function register metabox.
*
* Add meta box for custom-code post.
*/
private function register_metabox() {
if ( ! is_admin() ) {
return;
}
// Remove 'author' meta_box from 'add-new.php', 'author' is required only in list ( enabled via 'supports' arg ).
add_action( 'add_meta_boxes_' . self::CPT, function () {
remove_meta_box( 'authordiv', self::CPT, 'normal' );
} );
}
private function enqueue_assets() {
wp_enqueue_style( 'elementor-app-base', $this->get_css_assets_url( 'app-base', null, 'default', true ), [
'elementor-icons',
'select2',
], ELEMENTOR_VERSION );
wp_register_style(
'select2',
$this->get_css_assets_url( 'e-select2', 'assets/lib/e-select2/css/' ),
[],
'4.0.6-rc.1'
);
wp_register_script(
'select2',
$this->get_js_assets_url( 'e-select2.full', 'assets/lib/e-select2/js/' ),
[
'jquery',
],
'4.0.6-rc.1',
true
);
wp_register_style(
'elementor-icons',
$this->get_css_assets_url( 'elementor-icons', 'assets/lib/eicons/css/' ),
[],
Icons_Manager::ELEMENTOR_ICONS_VERSION
);
wp_enqueue_script( 'react' );
wp_enqueue_script( 'react-dom' );
wp_enqueue_script(
'elementor-app-packages',
$this->get_js_assets_url( 'app-packages' ),
[
'wp-i18n',
'react',
],
ELEMENTOR_VERSION,
true
);
add_action( 'elementor-pro/metabox/render', function ( $metabox, $post_id ) {
$min_suffix = Utils::is_script_debug() ? '' : '.min';
wp_enqueue_script(
'tipsy',
ELEMENTOR_ASSETS_URL . 'lib/tipsy/tipsy' . $min_suffix . '.js',
[
'jquery',
],
'1.0.0',
true
);
wp_enqueue_style(
'custom-code',
ELEMENTOR_PRO_ASSETS_URL . 'css/modules/custom-code' . $min_suffix . '.css',
[
'elementor-app-base',
],
ELEMENTOR_PRO_VERSION
);
// Load 'admin.js` module JS entry.
wp_enqueue_script(
'custom-code-metabox',
ELEMENTOR_PRO_ASSETS_URL . 'js/custom-code' . $min_suffix . '.js',
[
'elementor-v2-ui',
'elementor-v2-icons',
'react',
'select2',
// Temporary dependency until we will have a better way to load AI app in the admin.
'elementor-ai-admin',
],
ELEMENTOR_PRO_VERSION
);
$post = [
'ID' => $post_id,
'post_status' => get_post_status( $post_id ),
];
wp_add_inline_script( 'custom-code-metabox', sprintf( 'elementorProAdmin.customCode.post = %s;', wp_json_encode( $post ) ) );
}, 10, 2 );
}
private function add_admin_menu( Admin_Menu_Manager $admin_menu_manager ) {
if ( $this->can_use_custom_code() ) {
$admin_menu_manager->register( static::MENU_SLUG, new Custom_Code_Menu_Item() );
}
}
private function can_use_custom_code() {
return ( API::is_license_active() && API::is_licence_has_feature( static::MODULE_NAME, API::BC_VALIDATION_CALLBACK ) || $this->has_custom_code_snippets() );
}
private function has_custom_code_snippets() {
$existing_snippets = get_posts( [
'posts_per_page' => 1, // Avoid fetching too much data
'post_type' => static::CPT,
] );
return ! empty( $existing_snippets );
}
private function register_documents( Documents_Manager $documents_manager ) {
$documents_manager->register_document_type( self::DOCUMENT_TYPE, Document::get_class_full_name() );
}
private function register_location( Locations_Manager $location_manager ) {
foreach ( array_keys( $this->meta_box->get_location_options() ) as $location ) {
$location_manager->register_location( $location, [
'multiple' => true,
'public' => false,
'edit_in_content' => false,
] );
}
}
private function maybe_render_blank_state( $which ) {
$counts = (array) wp_count_posts( self::CPT );
unset( $counts['auto-draft'] );
if ( ! array_sum( $counts ) ) {
/** @var Source_Local $source */
$source = Plugin::elementor()->templates_manager->get_source( 'local' );
$source->maybe_render_blank_state( $which, [
'post_type' => self::DOCUMENT_TYPE,
'cpt' => self::CPT,
'description' => esc_html__( 'Add pixels, meta tags and any other scripts to your site.', 'elementor-pro' ) . sprintf( '<br><a target="_blank" href="https://go.elementor.com/wp-dash-custom-code">%s</a>', esc_html__( 'Learn more about adding custom code', 'elementor-pro' ) ),
'href' => esc_url( admin_url( '/post-new.php?post_type=' . self::CPT ) ),
] );
}
}
private function manage_posts_columns( $columns ) {
$new = [
self::ADDITIONAL_COLUMN_INSTANCES => esc_html__( 'Instances', 'elementor-pro' ),
Custom_Code_Metabox::FIELD_LOCATION => esc_html__( 'Location', 'elementor-pro' ),
Custom_Code_Metabox::FIELD_PRIORITY => esc_html__( 'Priority', 'elementor-pro' ),
];
// Insert after 'author'.
$keys = array_keys( $columns );
$pos = array_search( 'author', $keys ) + 1;
$columns = array_merge( array_slice( $columns, 0, $pos ), $new, array_slice( $columns, $pos ) );
return $columns;
}
private function manage_posts_custom_column( $column_name, $post_id ) {
if ( in_array( $column_name, Custom_Code_Metabox::INPUT_FIELDS ) ) {
$value = get_post_meta( $post_id, '_elementor_' . $column_name, true );
if ( Custom_Code_Metabox::FIELD_LOCATION === $column_name ) {
$location_labels = $this->meta_box->get_location_labels();
if ( isset( $location_labels[ $value ] ) ) {
$value = $location_labels[ $value ];
}
}
echo esc_html( $value );
} elseif ( self::ADDITIONAL_COLUMN_INSTANCES === $column_name ) {
/** @var Conditions_Manager $conditions_manager */
$conditions_manager = Plugin::instance()->modules_manager->get_modules( 'theme-builder' )->get_conditions_manager();
echo esc_html( implode( ', ', $conditions_manager->get_document_instances( $post_id ) ) );
}
}
private function get_snippets_by_location( $location ) {
return get_posts( [
'numberposts' => -1,
'post_type' => self::CPT,
'meta_query' => [
[
'key' => '_elementor_' . Custom_Code_Metabox::FIELD_LOCATION,
'value' => $location,
],
],
'orderby' => [
'meta_value_num' => 'ASC',
'ID' => 'ASC',
],
'meta_key' => '_elementor_' . Custom_Code_Metabox::FIELD_PRIORITY,
] );
}
private function print_snippets( $location ) {
// Do not print snippets on safe mode.
if ( isset( $_REQUEST['elementor-mode'] ) && 'safe' === $_REQUEST['elementor-mode'] ) {
return;
}
$snippets = $this->get_snippets_by_location( $location );
/** @var \ElementorPro\Modules\ThemeBuilder\Module $theme_builder */
$theme_builder = Plugin::instance()->modules_manager->get_modules( 'theme-builder' );
$documents_by_conditions = $theme_builder->get_conditions_manager()->get_documents_for_location( $location );
$location_manager = $theme_builder->get_locations_manager();
foreach ( $snippets as $snippet ) {
// Add snippet to location.
// Also handling situation without conditions, bind current snippet id with current location.
if ( isset( $documents_by_conditions[ $snippet->ID ] ) || ! get_post_meta( $snippet->ID, '_elementor_conditions', true ) ) {
$location_manager->add_doc_to_location( $location, $snippet->ID );
}
}
elementor_theme_do_location( $location );
}
private function register_rest_meta_fields() {
$this->register_rest_meta_location();
$this->register_rest_meta_priority();
$this->register_rest_meta_code();
}
private function register_rest_meta_location() {
$enum = [
Custom_Code_Metabox::OPTION_LOCATION_HEAD,
Custom_Code_Metabox::OPTION_LOCATION_BODY_START,
Custom_Code_Metabox::OPTION_LOCATION_BODY_END,
];
register_meta( 'post', '_elementor_' . Custom_Code_Metabox::FIELD_LOCATION, [
'object_subtype' => self::CPT,
'show_in_rest' => [
'schema' => [
'type' => 'string',
'enum' => $enum,
],
],
'single' => true,
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
'auth_callback' => function( $allowed, $meta_key, $object_id ) {
$post = get_post( $object_id );
if ( ! $post || self::CPT !== $post->post_type ) {
return false;
}
return current_user_can( self::CAPABILITY );
},
'default' => Custom_Code_Metabox::DEFAULT_LOCATION,
] );
}
private function register_rest_meta_priority() {
register_meta( 'post', '_elementor_' . Custom_Code_Metabox::FIELD_PRIORITY, [
'object_subtype' => self::CPT,
'show_in_rest' => [
'schema' => [ 'type' => 'integer' ],
],
'single' => true,
'type' => 'integer',
'sanitize_callback' => 'absint',
'auth_callback' => function( $allowed, $meta_key, $object_id ) {
$post = get_post( $object_id );
if ( ! $post || self::CPT !== $post->post_type ) {
return false;
}
return current_user_can( self::CAPABILITY );
},
'default' => Custom_Code_Metabox::DEFAULT_PRIORITY,
] );
}
private function register_rest_meta_code() {
register_meta( 'post', '_elementor_' . Custom_Code_Metabox::FIELD_CODE, [
'object_subtype' => self::CPT,
'show_in_rest' => [
'schema' => [ 'type' => 'string' ],
],
'single' => true,
'type' => 'string',
'auth_callback' => function( $allowed, $meta_key, $object_id ) {
$post = get_post( $object_id );
if ( ! $post || self::CPT !== $post->post_type ) {
return false;
}
return current_user_can( self::CAPABILITY );
},
'sanitize_callback' => function( $value ) {
if ( ! current_user_can( 'unfiltered_html' ) ) {
return wp_kses_post( $value );
}
return $value;
},
] );
}
private function register_import_export() {
( new Custom_Code_Import_Export() )->register_hooks();
( new Custom_Code_Import_Export_Customization() )->register_hooks();
}
}