'application/x-httpd-php',
'codemirror' => array(
'indentUnit' => 2,
'tabSize' => 2,
),
)
);
$deps[] = 'code-editor';
} else {
$settings = false;
}
wp_enqueue_script( 'generate-elements-metabox', plugin_dir_url( __FILE__ ) . 'assets/admin/metabox.js', $deps, GP_PREMIUM_VERSION );
wp_localize_script( 'generate-elements-metabox', 'elements', array(
'nonce' => wp_create_nonce( 'generate-elements-location' ),
'settings' => $settings ? wp_json_encode( $settings ) : false,
'type' => get_post_meta( get_the_ID(), '_generate_element_type', true ),
'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', plugin_dir_url( __FILE__ ) . 'assets/admin/wp-color-picker-alpha.min.js', array( 'wp-color-picker' ), GP_PREMIUM_VERSION );
if ( function_exists( 'wp_add_inline_script' ) && function_exists( 'generate_get_default_color_palettes' ) ) {
// Grab our palette array and turn it into JS
$palettes = 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( 'generate-select2', GP_LIBRARY_DIRECTORY_URL . 'select2/select2.min.css', array(), GP_PREMIUM_VERSION );
wp_enqueue_script( 'generate-select2', GP_LIBRARY_DIRECTORY_URL . 'select2/select2.full.min.js', array( 'jquery', 'generate-elements-metabox' ), GP_PREMIUM_VERSION );
}
}
}
/**
* Register our metabox.
*
* @since 1.7
*/
public function register_metabox() {
// Title not translated on purpose.
add_meta_box( 'generate_premium_elements', 'Element', array( $this, 'element_fields' ), 'gp_elements', 'normal' );
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 = get_post_meta( get_the_ID(), '_generate_element_type', true );
$type_chosen = '' !== $type ? true : false;
$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 );
?>
>
|
|
|
|
?
|
$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_disable_site_header' => 'key',
'_generate_hook_disable_site_footer' => 'key',
'_generate_hook_execute_shortcodes' => 'key',
'_generate_hook_execute_php' => 'key',
'_generate_hook_priority' => 'number',
);
// 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_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 ( ! 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' );
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
if ( ! isset( $_POST['id'] ) ) {
return;
}
$tax_id = sanitize_text_field( $_POST['id'] );
echo 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' );
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
if ( ! isset( $_POST['id'] ) ) {
return;
}
$post_type = sanitize_text_field( $_POST['id'] );
echo json_encode( self::get_post_type_posts( $post_type ) );
die();
}
/**
* Look up posts inside a post type.
*
* @since 1.7
*
* @param string $post_type The post type to look up.
* @return array
*/
public static function get_post_type_posts( $post_type ) {
global $wpdb;
$post_status = array( 'publish', 'future', 'draft', 'pending', 'private' );
$object = get_post_type_object( $post_type );
$data = array(
'type' => 'posts',
'postType' => $post_type,
'label' => $object->label,
'objects' => array(),
);
if ( 'attachment' === $post_type ) {
$posts = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_title from $wpdb->posts where post_type = %s ORDER BY post_title", $post_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", $post_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 ) : $post_type . '-' . $post->ID;
$data['objects'][] = array(
'id' => $post->ID,
'name' => $title,
);
}
return $data;
}
/**
* Get taxonomy terms for a specific taxonomy.
*
* @since 1.7
*
* @param $tax_id
* @return array
*/
public static function get_taxonomy_terms( $tax_id ) {
$tax = get_taxonomy( $tax_id );
$terms = get_terms( array(
'taxonomy' => $tax_id,
'hide_empty' => false,
) );
$data = array(
'type' => 'terms',
'taxonomy' => $tax_id,
'label' => $tax->label,
'objects' => array(),
);
foreach ( $terms as $term ) {
$data['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() {
$hooks = array(
'scripts' => array(
'group' => esc_attr__( 'Scripts & Styles', 'gp-premium' ),
'hooks' => array(
'wp_head',
'wp_body_open',
'wp_footer',
),
),
'header' => array(
'group' => esc_attr__( 'Header', 'gp-premium' ),
'hooks' => array(
'generate_before_header',
'generate_after_header',
'generate_before_header_content',
'generate_after_header_content',
'generate_before_logo',
'generate_after_logo',
'generate_header',
),
),
'navigation' => array(
'group' => esc_attr__( 'Navigation', 'gp-premium' ),
'hooks' => array(
'generate_inside_navigation',
'generate_inside_secondary_navigation',
'generate_inside_mobile_menu',
'generate_inside_mobile_menu_bar',
'generate_inside_mobile_header',
'generate_inside_slideout_navigation',
'generate_after_slideout_navigation',
),
),
'content' => array(
'group' => esc_attr__( 'Content', 'gp-premium' ),
'hooks' => array(
'generate_inside_site_container',
'generate_inside_container',
'generate_before_main_content',
'generate_after_main_content',
'generate_before_content',
'generate_after_content',
'generate_after_entry_content',
'generate_after_primary_content_area',
'generate_before_entry_title',
'generate_after_entry_title',
'generate_after_entry_header',
'generate_before_archive_title',
'generate_after_archive_title',
'generate_after_archive_description',
),
),
'comments' => array(
'group' => esc_attr__( 'Comments', 'gp-premium' ),
'hooks' => array(
'generate_before_comments_container',
'generate_before_comments',
'generate_inside_comments',
'generate_below_comments_title',
),
),
'sidebars' => array(
'group' => esc_attr__( 'Sidebars', 'gp-premium' ),
'hooks' => array(
'generate_before_right_sidebar_content',
'generate_after_right_sidebar_content',
'generate_before_left_sidebar_content',
'generate_after_left_sidebar_content',
),
),
'footer' => array(
'group' => esc_attr__( 'Footer', 'gp-premium' ),
'hooks' => array(
'generate_before_footer',
'generate_after_footer',
'generate_after_footer_widgets',
'generate_before_footer_content',
'generate_after_footer_content',
'generate_footer',
),
),
);
if ( class_exists( 'WooCommerce' ) ) {
$hooks['navigation']['hooks'][] = 'generate_mobile_cart_items';
$hooks['woocommerce-global'] = array(
'group' => esc_attr__( 'WooCommerce - Global', 'gp-premium' ),
'hooks' => array(
'woocommerce_before_main_content',
'woocommerce_after_main_content',
'woocommerce_sidebar',
'woocommerce_breadcrumb',
),
);
$hooks['woocommerce-shop'] = array(
'group' => esc_attr__( 'WooCommerce - Shop', 'gp-premium' ),
'hooks' => array(
'woocommerce_archive_description',
'woocommerce_before_shop_loop',
'woocommerce_after_shop_loop',
'woocommerce_before_shop_loop_item_title',
'woocommerce_after_shop_loop_item_title',
),
);
$hooks['woocommerce-product'] = array(
'group' => esc_attr__( 'WooCommerce - Product', 'gp-premium' ),
'hooks' => array(
'woocommerce_before_single_product',
'woocommerce_before_single_product_summary',
'woocommerce_after_single_product_summary',
'woocommerce_single_product_summary',
'woocommerce_share',
'woocommerce_simple_add_to_cart',
'woocommerce_before_add_to_cart_form',
'woocommerce_after_add_to_cart_form',
'woocommerce_before_add_to_cart_button',
'woocommerce_after_add_to_cart_button',
'woocommerce_before_add_to_cart_quantity',
'woocommerce_after_add_to_cart_quantity',
'woocommerce_product_meta_start',
'woocommerce_product_meta_end',
'woocommerce_after_single_product',
),
);
$hooks['woocommerce-cart'] = array(
'group' => esc_attr__( 'WooCommerce - Cart', 'gp-premium' ),
'hooks' => array(
'woocommerce_before_calculate_totals',
'woocommerce_after_calculate_totals',
'woocommerce_before_cart',
'woocommerce_after_cart_table',
'woocommerce_before_cart_table',
'woocommerce_before_cart_contents',
'woocommerce_cart_contents',
'woocommerce_after_cart_contents',
'woocommerce_cart_coupon',
'woocommerce_cart_actions',
'woocommerce_before_cart_totals',
'woocommerce_cart_totals_before_order_total',
'woocommerce_cart_totals_after_order_total',
'woocommerce_proceed_to_checkout',
'woocommerce_after_cart_totals',
'woocommerce_after_cart',
),
);
$hooks['woocommerce-checkout'] = array(
'group' => esc_attr__( 'WooCommerce - Checkout', 'gp-premium' ),
'hooks' => array(
'woocommerce_before_checkout_form',
'woocommerce_checkout_before_customer_details',
'woocommerce_checkout_after_customer_details',
'woocommerce_checkout_billing',
'woocommerce_before_checkout_billing_form',
'woocommerce_after_checkout_billing_form',
'woocommerce_before_order_notes',
'woocommerce_after_order_notes',
'woocommerce_checkout_shipping',
'woocommerce_checkout_before_order_review',
'woocommerce_checkout_order_review',
'woocommerce_review_order_before_cart_contents',
'woocommerce_review_order_after_cart_contents',
'woocommerce_review_order_before_order_total',
'woocommerce_review_order_after_order_total',
'woocommerce_review_order_before_payment',
'woocommerce_review_order_before_submit',
'woocommerce_review_order_after_submit',
'woocommerce_review_order_after_payment',
'woocommerce_checkout_after_order_review',
'woocommerce_after_checkout_form',
),
);
$hooks['woocommerce-account'] = array(
'group' => esc_attr__( 'WooCommerce - Account', 'gp-premium' ),
'hooks' => array(
'woocommerce_before_account_navigation',
'woocommerce_account_navigation',
'woocommerce_after_account_navigation',
),
);
}
return apply_filters( 'generate_hooks_list', $hooks );
}
public static function template_tags() {
?>