}
}
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 );
} 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,
],
],
// 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 );
}
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();
}
}