is_block_editor ) ) { $element_type = 'block'; } } } return $element_type; } /** * Add a body class if we're using the Header element type. * We do this so we can hide the Template Tags metabox for other types. * * @since 1.7 * * @param string $classes Existing classes. * @return string */ public function body_class( $classes ) { if ( 'gp_elements' === get_post_type() ) { $element_type = self::get_element_type(); if ( $element_type ) { $classes .= ' ' . $element_type . '-element-type'; } else { $classes .= ' no-element-type'; } if ( 'header' === $element_type && get_post_meta( get_the_ID(), '_generate_element_content', true ) ) { $classes .= ' element-has-page-hero'; } $block_type = get_post_meta( get_the_ID(), '_generate_block_type', true ); if ( $block_type ) { if ( 'content-template' === $block_type && get_post_meta( get_the_ID(), '_generate_use_theme_post_container', true ) ) { $classes .= ' using-theme-post-container'; } if ( ( 'archive-navigation-template' === $block_type || 'post-navigation-template' === $block_type ) && get_post_meta( get_the_ID(), '_generate_use_archive_navigation_container', true ) ) { $classes .= ' using-theme-pagination-container'; } $classes .= ' ' . $block_type . '-block-type'; } } return $classes; } /** * Enqueue any necessary scripts and styles. * * @since 1.7 * * @param string $hook The current page hook. */ public function scripts( $hook ) { if ( in_array( $hook, array( 'post.php', 'post-new.php' ) ) ) { if ( 'gp_elements' === get_post_type() ) { $element_type = get_post_meta( get_the_ID(), '_generate_element_type', true ); // Remove autosave when dealing with non-content Elements. // phpcs:ignore -- No data processing happening. if ( 'block' !== $element_type || ( isset( $_GET['element_type'] ) && 'block' !== $_GET['element_type'] ) ) { wp_dequeue_script( 'autosave' ); } $deps = array( 'jquery' ); if ( function_exists( 'wp_enqueue_code_editor' ) ) { $settings = wp_enqueue_code_editor( array( 'type' => 'application/x-httpd-php', 'codemirror' => array( 'indentUnit' => 2, 'tabSize' => 2, ), ) ); $deps[] = 'code-editor'; } else { $settings = false; } $element_type = self::get_element_type(); wp_enqueue_script( 'generate-elements-metabox', plugin_dir_url( __FILE__ ) . 'assets/admin/metabox.js', $deps, GP_PREMIUM_VERSION, true ); wp_localize_script( 'generate-elements-metabox', 'elements', array( 'nonce' => wp_create_nonce( 'generate-elements-location' ), 'settings' => $settings ? wp_json_encode( $settings ) : false, 'type' => $element_type, 'custom_image' => __( 'Custom Image', 'gp-premium' ), 'fallback_image' => __( 'Fallback Image', 'gp-premium' ), 'choose' => __( 'Choose...', 'gp-premium' ), 'showID' => apply_filters( 'generate_elements_show_object_ids', false ), ) ); wp_enqueue_style( 'generate-elements-metabox', plugin_dir_url( __FILE__ ) . 'assets/admin/metabox.css', array(), GP_PREMIUM_VERSION ); wp_enqueue_style( 'generate-elements-balloon', plugin_dir_url( __FILE__ ) . 'assets/admin/balloon.css', array(), GP_PREMIUM_VERSION ); wp_enqueue_media(); wp_enqueue_script( 'wp-color-picker' ); wp_enqueue_style( 'wp-color-picker' ); wp_enqueue_script( 'wp-color-picker-alpha', GP_LIBRARY_DIRECTORY_URL . 'alpha-color-picker/wp-color-picker-alpha.min.js', array( 'wp-color-picker' ), '3.0.0', true ); wp_add_inline_script( 'wp-color-picker-alpha', 'jQuery( function() { jQuery( ".color-picker" ).wpColorPicker(); } );' ); if ( function_exists( 'wp_add_inline_script' ) && function_exists( 'generate_get_default_color_palettes' ) ) { // Grab our palette array and turn it into JS. $palettes = wp_json_encode( generate_get_default_color_palettes() ); // Add our custom palettes. // json_encode takes care of escaping. wp_add_inline_script( 'wp-color-picker', 'jQuery.wp.wpColorPicker.prototype.options.palettes = ' . $palettes . ';' ); } wp_enqueue_style( 'select2', GP_LIBRARY_DIRECTORY_URL . 'select2/select2.min.css', array(), '4.0.13' ); wp_enqueue_script( 'select2', GP_LIBRARY_DIRECTORY_URL . 'select2/select2.full.min.js', array( 'jquery', 'generate-elements-metabox' ), '4.0.13', true ); $css = ''; if ( function_exists( 'generate_get_option' ) && 'separate-containers' === generate_get_option( 'content_layout_setting' ) ) { $css .= 'body.left-sidebar-block-type .block-editor-block-list__layout.is-root-container, body.right-sidebar-block-type .block-editor-block-list__layout.is-root-container {background: ' . generate_get_option( 'background_color' ) . ';}'; $css .= 'body.content-template-block-type:not(.using-theme-post-container) .block-editor-block-list__layout.is-root-container, body.archive-navigation-template-block-type:not(.using-theme-pagination-container) .block-editor-block-list__layout.is-root-container {background: ' . generate_get_option( 'background_color' ) . ';}'; } wp_add_inline_style( 'generate-elements-metabox', $css ); } } } /** * Register our metabox. * * @since 1.7 */ public function register_metabox() { // Title not translated on purpose. add_meta_box( 'generate_premium_elements', __( 'Display Rules', 'gp-premium' ), array( $this, 'element_fields' ), 'gp_elements', 'normal', 'high' ); add_meta_box( 'generate_page_hero_template_tags', __( 'Template Tags', 'gp-premium' ), array( $this, 'template_tags' ), 'gp_elements', 'side', 'low' ); remove_meta_box( 'slugdiv', 'gp_elements', 'normal' ); } /** * Output all of our metabox fields. * * @since 1.7 * * @param object $post Our post object. */ public function element_fields( $post ) { wp_nonce_field( basename( __FILE__ ), 'generate_elements_nonce' ); $type = self::get_element_type(); $page_hero_class = ''; $merge = get_post_meta( get_the_ID(), '_generate_site_header_merge', true ); $conditions_set = get_post_meta( get_the_ID(), '_generate_element_display_conditions', true ); $post_status = get_post_status( get_the_ID() ); if ( 'header' === $type && get_post_meta( get_the_ID(), '_generate_element_content', true ) ) { $page_hero_class = ' has-page-hero'; } ?>

> > >
/>
/>
DISALLOW_FILE_EDIT' ); ?> />
> > > > > > >
?
/>
/>
? />
? />
> > > > > > > > > > >
?
?
px
'; } ?>
/>
'; } ?>
/>
'; } ?>
/>
'; } ?>
/>
/>
/>
/>
/>
/>
/>
/>
/>
/>
px
>
? $value ) { $type = explode( ':', $value['rule'] ); if ( in_array( 'post', $type ) || in_array( 'taxonomy', $type ) ) { $show = true; } else { $show = false; } ?>
? $value ) { $type = explode( ':', $value['rule'] ); if ( in_array( 'post', $type ) || in_array( 'taxonomy', $type ) ) { $show = true; } else { $show = false; } ?>
? $value ) { ?>
? />
'text', '_generate_custom_hook' => 'text', '_generate_hook_priority' => 'number', ); if ( 'hook' === $type_value ) { $hook_values['_generate_hook_disable_site_header'] = 'key'; $hook_values['_generate_hook_disable_site_footer'] = 'key'; $hook_values['_generate_hook_execute_shortcodes'] = 'key'; $hook_values['_generate_hook_execute_php'] = 'key'; } // We don't want people to be able to use these hooks. $blacklist = array( 'muplugins_loaded', 'registered_taxonomy', 'plugins_loaded', 'setup_theme', 'after_setup_theme', 'init', 'widgets_init', 'wp_loaded', 'pre_get_posts', 'wp', 'template_redirect', 'get_header', 'wp_enqueue_scripts', 'the_post', 'dynamic_sidebar', 'get_footer', 'get_sidebar', 'wp_print_footer_scripts', 'shutdown', ); foreach ( $hook_values as $key => $type ) { $value = false; if ( isset( $_POST[ $key ] ) ) { // Bail if we're using a blacklisted hook. if ( '_generate_custom_hook' === $key ) { if ( in_array( $_POST[ $key ], $blacklist ) ) { continue; } } if ( 'number' === $type ) { $value = absint( $_POST[ $key ] ); } elseif ( 'key' === $type ) { $value = sanitize_key( $_POST[ $key ] ); } else { $value = sanitize_text_field( $_POST[ $key ] ); } // Need to temporarily change the $value so it returns true. if ( '_generate_hook_priority' === $key ) { if ( '0' === $_POST[ $key ] ) { $value = 'zero'; } } } if ( $value ) { if ( 'zero' === $value ) { $value = '0'; } update_post_meta( $post_id, $key, $value ); } else { delete_post_meta( $post_id, $key ); } } } // Page Header type. if ( 'header' === $type_value ) { $hero_values = array( '_generate_hero_custom_classes' => 'attribute', '_generate_hero_container' => 'text', '_generate_hero_inner_container' => 'text', '_generate_hero_horizontal_alignment' => 'text', '_generate_hero_full_screen' => 'key', '_generate_hero_vertical_alignment' => 'text', '_generate_hero_padding_top' => 'number', '_generate_hero_padding_top_unit' => 'text', '_generate_hero_padding_right' => 'number', '_generate_hero_padding_right_unit' => 'text', '_generate_hero_padding_bottom' => 'number', '_generate_hero_padding_bottom_unit' => 'text', '_generate_hero_padding_left' => 'number', '_generate_hero_padding_left_unit' => 'text', '_generate_hero_padding_top_mobile' => 'number', '_generate_hero_padding_top_unit_mobile' => 'text', '_generate_hero_padding_right_mobile' => 'number', '_generate_hero_padding_right_unit_mobile' => 'text', '_generate_hero_padding_bottom_mobile' => 'number', '_generate_hero_padding_bottom_unit_mobile' => 'text', '_generate_hero_padding_left_mobile' => 'number', '_generate_hero_padding_left_unit_mobile' => 'text', '_generate_hero_background_image' => 'key', '_generate_hero_disable_featured_image' => 'key', '_generate_hero_background_color' => 'color', '_generate_hero_text_color' => 'color', '_generate_hero_link_color' => 'color', '_generate_hero_background_link_color_hover' => 'color', '_generate_hero_background_overlay' => 'key', '_generate_hero_background_position' => 'text', '_generate_hero_background_parallax' => 'key', '_generate_site_header_merge' => 'key', '_generate_site_header_height' => 'number', '_generate_site_header_height_mobile' => 'number', '_generate_navigation_colors' => 'key', '_generate_site_logo' => 'number', '_generate_retina_logo' => 'number', '_generate_navigation_logo' => 'number', '_generate_mobile_logo' => 'number', '_generate_navigation_location' => 'key', '_generate_site_header_background_color' => 'text', '_generate_site_header_title_color' => 'text', '_generate_site_header_tagline_color' => 'text', '_generate_navigation_background_color' => 'text', '_generate_navigation_text_color' => 'text', '_generate_navigation_background_color_hover' => 'text', '_generate_navigation_text_color_hover' => 'text', '_generate_navigation_background_color_current' => 'text', '_generate_navigation_text_color_current' => 'text', ); foreach ( $hero_values as $key => $type ) { $value = false; if ( isset( $_POST[ $key ] ) ) { if ( 'number' === $type ) { $value = absint( $_POST[ $key ] ); } elseif ( 'key' === $type ) { $value = sanitize_key( $_POST[ $key ] ); } elseif ( 'attribute' === $type ) { $value = esc_attr( $_POST[ $key ] ); } else { $value = sanitize_text_field( $_POST[ $key ] ); } } if ( '_generate_hero_padding_top_mobile' === $key || '_generate_hero_padding_right_mobile' === $key || '_generate_hero_padding_bottom_mobile' === $key || '_generate_hero_padding_left_mobile' === $key ) { if ( '0' === $_POST[ $key ] ) { $value = 'zero'; } } if ( $value ) { if ( 'zero' === $value ) { $value = '0'; // String on purpose. } update_post_meta( $post_id, $key, $value ); } else { delete_post_meta( $post_id, $key ); } } } // Save Layout type. if ( 'layout' === $type_value ) { $layout_values = array( '_generate_sidebar_layout' => 'key', '_generate_footer_widgets' => 'key', '_generate_disable_site_header' => 'key', '_generate_disable_mobile_header' => 'key', '_generate_disable_top_bar' => 'key', '_generate_disable_primary_navigation' => 'key', '_generate_disable_secondary_navigation' => 'key', '_generate_disable_featured_image' => 'key', '_generate_disable_content_title' => 'key', '_generate_disable_footer' => 'key', '_generate_content_area' => 'key', '_generate_content_width' => 'number', ); foreach ( $layout_values as $key => $type ) { $value = false; if ( isset( $_POST[ $key ] ) ) { if ( 'number' === $type ) { $value = absint( $_POST[ $key ] ); } elseif ( 'key' === $type ) { $value = sanitize_key( $_POST[ $key ] ); } else { $value = sanitize_text_field( $_POST[ $key ] ); } } if ( $value ) { update_post_meta( $post_id, $key, $value ); } else { delete_post_meta( $post_id, $key ); } } } $ignore_languages = false; if ( isset( $_POST['_generate_element_ignore_languages'] ) ) { $ignore_languages = sanitize_key( $_POST['_generate_element_ignore_languages'] ); } if ( $ignore_languages ) { update_post_meta( $post_id, '_generate_element_ignore_languages', $ignore_languages ); } else { delete_post_meta( $post_id, '_generate_element_ignore_languages' ); } // Display conditions. $conditions = get_post_meta( $post_id, '_generate_element_display_conditions', true ); $new_conditions = array(); $rules = $_POST['display-condition']; $objects = $_POST['display-condition-object']; $count = count( $rules ); for ( $i = 0; $i < $count; $i++ ) { if ( '' !== $rules[ $i ] ) { if ( in_array( $rules[ $i ], $rules ) ) { $new_conditions[ $i ]['rule'] = sanitize_text_field( $rules[ $i ] ); $new_conditions[ $i ]['object'] = sanitize_key( $objects[ $i ] ); } else { $new_conditions[ $i ]['rule'] = ''; $new_conditions[ $i ]['object'] = ''; } } } if ( 'block' === $type_value ) { $dynamic_css_posts = get_option( 'generateblocks_dynamic_css_posts', array() ); if ( $dynamic_css_posts ) { foreach ( $new_conditions as $condition ) { if ( $condition['object'] && isset( $dynamic_css_posts[ $condition['object'] ] ) ) { unset( $dynamic_css_posts[ $condition['object'] ] ); } if ( ! $condition['object'] ) { $dynamic_css_posts = array(); break; } } update_option( 'generateblocks_dynamic_css_posts', $dynamic_css_posts ); } } if ( ! empty( $new_conditions ) && $new_conditions !== $conditions ) { update_post_meta( $post_id, '_generate_element_display_conditions', $new_conditions ); } elseif ( empty( $new_conditions ) && $conditions ) { delete_post_meta( $post_id, '_generate_element_display_conditions', $conditions ); } // Exclude conditions. $exclude_conditions = get_post_meta( $post_id, '_generate_element_exclude_conditions', true ); $new_exclude_conditions = array(); $exclude_rules = $_POST['exclude-condition']; $exclude_objects = $_POST['exclude-condition-object']; $exclude_count = count( $exclude_rules ); for ( $i = 0; $i < $exclude_count; $i++ ) { if ( '' !== $exclude_rules[ $i ] ) { if ( in_array( $exclude_rules[ $i ], $exclude_rules ) ) { $new_exclude_conditions[ $i ]['rule'] = sanitize_text_field( $exclude_rules[ $i ] ); $new_exclude_conditions[ $i ]['object'] = sanitize_key( $exclude_objects[ $i ] ); } else { $new_exclude_conditions[ $i ]['rule'] = ''; $new_exclude_conditions[ $i ]['object'] = ''; } } } if ( ! empty( $new_exclude_conditions ) && $new_exclude_conditions !== $exclude_conditions ) { update_post_meta( $post_id, '_generate_element_exclude_conditions', $new_exclude_conditions ); } elseif ( empty( $new_exclude_conditions ) && $exclude_conditions ) { delete_post_meta( $post_id, '_generate_element_exclude_conditions', $exclude_conditions ); } // User conditions. $user_conditions = get_post_meta( $post_id, '_generate_element_user_conditions', true ); $new_user_conditions = array(); $user_rules = $_POST['user-condition']; $user_count = count( $user_rules ); for ( $i = 0; $i < $user_count; $i++ ) { if ( '' !== $user_rules[ $i ] ) { if ( in_array( $user_rules[ $i ], $user_rules ) ) { $new_user_conditions[ $i ] = sanitize_text_field( $user_rules[ $i ] ); } else { $new_user_conditions[ $i ] = ''; } } } if ( ! empty( $new_user_conditions ) && $new_user_conditions !== $user_conditions ) { update_post_meta( $post_id, '_generate_element_user_conditions', $new_user_conditions ); } elseif ( empty( $new_user_conditions ) && $user_conditions ) { delete_post_meta( $post_id, '_generate_element_user_conditions', $user_conditions ); } // Internal notes. $notes_key = '_generate_element_internal_notes'; if ( isset( $_POST[ $notes_key ] ) ) { if ( function_exists( 'sanitize_textarea_field' ) ) { $notes_value = sanitize_textarea_field( $_POST[ $notes_key ] ); } else { $notes_value = sanitize_text_field( $_POST[ $notes_key ] ); } if ( $notes_value ) { update_post_meta( $post_id, $notes_key, $notes_value ); } else { delete_post_meta( $post_id, $notes_key ); } } } /** * Get terms of a set taxonomy. * * @since 1.7 */ public function get_terms() { check_ajax_referer( 'generate-elements-location', 'nonce' ); $current_user_can = 'manage_options'; if ( apply_filters( 'generate_elements_metabox_ajax_allow_editors', false ) ) { $current_user_can = 'edit_posts'; } if ( ! current_user_can( $current_user_can ) ) { return; } if ( ! isset( $_POST['id'] ) ) { return; } $tax_id = sanitize_text_field( $_POST['id'] ); echo wp_json_encode( self::get_taxonomy_terms( $tax_id ) ); die(); } /** * Get all posts inside a specific post type. * * @since 1.7 */ public function get_posts() { check_ajax_referer( 'generate-elements-location', 'nonce' ); $current_user_can = 'manage_options'; if ( apply_filters( 'generate_elements_metabox_ajax_allow_editors', false ) ) { $current_user_can = 'edit_posts'; } if ( ! current_user_can( $current_user_can ) ) { return; } if ( ! isset( $_POST['id'] ) ) { return; } $post_type = sanitize_text_field( $_POST['id'] ); echo wp_json_encode( self::get_post_type_posts( $post_type ) ); die(); } /** * Get all of our posts and terms in one go on load. * * @since 2.0.0 */ public function get_all_objects() { check_ajax_referer( 'generate-elements-location', 'nonce' ); $current_user_can = 'manage_options'; if ( apply_filters( 'generate_elements_metabox_ajax_allow_editors', false ) ) { $current_user_can = 'edit_posts'; } if ( ! current_user_can( $current_user_can ) ) { return; } if ( ! isset( $_POST['posts'] ) && ! isset( $_POST['terms'] ) ) { return; } $posts = array(); $terms = array(); if ( ! empty( $_POST['posts'] ) ) { $posts = array_map( 'sanitize_text_field', $_POST['posts'] ); } if ( ! empty( $_POST['terms'] ) ) { $terms = array_map( 'sanitize_text_field', $_POST['terms'] ); } $all_posts = self::get_post_type_posts( $posts ); $all_terms = self::get_taxonomy_terms( $terms ); echo wp_json_encode( array_merge( $all_posts, $all_terms ) ); die(); } /** * Look up posts inside a post type. * * @since 1.7 * * @param string|array $post_type The post type to look up. * @return array */ public static function get_post_type_posts( $post_type ) { if ( ! is_array( $post_type ) ) { $post_type = array( $post_type ); } $data = array(); foreach ( $post_type as $type ) { global $wpdb; $post_status = array( 'publish', 'future', 'draft', 'pending', 'private' ); $object = get_post_type_object( $type ); $data[ $type ] = array( 'type' => 'posts', 'postType' => $type, 'label' => $object->label, 'objects' => array(), ); if ( 'attachment' === $type ) { $posts = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_title from $wpdb->posts where post_type = %s ORDER BY post_title", $type ) ); } else { $format = implode( ', ', array_fill( 0, count( $post_status ), '%s' ) ); $query = sprintf( "SELECT ID, post_title from $wpdb->posts where post_type = '%s' AND post_status IN(%s) ORDER BY post_title", $type, $format ); // @codingStandardsIgnoreLine $posts = $wpdb->get_results( $wpdb->prepare( $query, $post_status ) ); } foreach ( $posts as $post ) { $title = ( '' !== $post->post_title ) ? esc_attr( $post->post_title ) : $type . '-' . $post->ID; $data[ $type ]['objects'][] = array( 'id' => $post->ID, 'name' => $title, ); } } return $data; } /** * Get taxonomy terms for a specific taxonomy. * * @since 1.7 * * @param int|array $tax_id The taxonomy ID. * @return array */ public static function get_taxonomy_terms( $tax_id ) { if ( ! is_array( $tax_id ) ) { $tax_id = array( $tax_id ); } $data = array(); foreach ( $tax_id as $id ) { $tax = get_taxonomy( $id ); $terms = get_terms( array( 'taxonomy' => $id, 'hide_empty' => false, ) ); $data[ $id ] = array( 'type' => 'terms', 'taxonomy' => $id, 'label' => $tax->label, 'objects' => array(), ); foreach ( $terms as $term ) { $data[ $id ]['objects'][] = array( 'id' => $term->term_id, 'name' => esc_attr( $term->name ), ); } } return $data; } /** * Build our entire list of hooks to display. * * @since 1.7 * * @return array Our list of hooks. */ public static function get_available_hooks() { return GeneratePress_Elements_Helper::get_available_hooks(); } /** * Add available template tags to Header Elements. * * @since 1.7 */ public static function template_tags() { ?>