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 ); $direction_suffix = is_rtl() ? '-rtl' : ''; wp_enqueue_style( 'custom-code', ELEMENTOR_PRO_ASSETS_URL . 'css/modules/custom-code' . $direction_suffix . $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', [ 'react', 'select2', ], 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() ); } else { $admin_menu_manager->register( static::PROMOTION_MENU_SLUG, new Custom_Code_Promotion_Menu_Item() ); } } private function can_use_custom_code() { return API::is_license_active() || $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( '
%s', 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 ); } else if ( 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, ], ], // Order. 'order' => 'ASC', 'orderby' => 'meta_value_num', '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 ); } }