= count( self::$usedWidgets ) ) { return false; } foreach ( $widgets as $widget ) { if ( array_search( $widget, self::$usedWidgets ) ) { return true; } } return false; } /** * Check if widget is used * @param string $widget * @return bool */ public function didWidget( string $widget ) { if ( is_array( self::$usedWidgets ) && 0 >= count( self::$usedWidgets ) ) return false; if ( array_search( $widget, self::$usedWidgets ) ) return true; return false; } // WC public function didWCWidgets() { if ( is_array( self::$usedWidgets ) && 0 >= count( self::$usedWidgets ) ) return false; if ( preg_grep( '/WC_Widget_/i', self::$usedWidgets ) ) return true; return false; } // BBP public function didBBPWidgets() { if ( is_array( self::$usedWidgets ) && 0 >= count( self::$usedWidgets ) ) return false; if ( preg_grep( '/BBP_/i', self::$usedWidgets ) ) return true; return false; } // BP public function didBPWidgets() { if ( is_array( self::$usedWidgets ) && 0 >= count( self::$usedWidgets ) ) return false; if ( preg_grep( '/BP_/i', self::$usedWidgets ) ) return true; return false; } /** * Properties **/ /** * Defaults **/ public $defaults = array(); /** * Default Devices **/ public $defaultsShow = array( 'home' => 'on', 'front' => 'on', 'blog' => 'on', 'archive' => 'on', 'singular' => 'on', 'error' => 'on', 'device' => 'all', ); /** * Default Devices **/ public $defaultsHide = array( 'wc_page' => '', 'wc_cart' => '', 'wc_checkout' => '', 'wc_account_page' => '', ); /** * Default Devices **/ public $defaultsHideInWC = array( 'wc_page' => '', 'wc_cart' => '', 'wc_checkout' => '', 'wc_account_page' => '', ); /** * Default Devices **/ public $defaultsShowInPlugins = array( 'woocommerce_page' => 'on', 'bbpress_page' => 'on', 'buddypress_page' => 'on', ); /** * WooCommerce Pages **/ public $pageTypes = array(); /** * Public Post Types **/ public $publicPostTypes = array(); /** * WooCommerce Pages **/ public $wcPages = array(); /** * BuddyPress Pages **/ public $bpPages = array(); /** * Devices **/ public $devices = array(); /** * Init **/ /** * Public Initializer * @return AceWidgetManager **/ public static function getInstance() { if ( null === self::$instance ) self::$instance = new Self(); return self::$instance; } /** * Constructor **/ protected function __construct() { if ( is_admin() ) { $this->init(); } $this->initHooks(); } /** * Init **/ protected function init() { // Page Types $this->pageTypes = array( 'home' => __( 'Home', Ace::TEXTDOMAIN ), 'front' => __( 'Front Page', Ace::TEXTDOMAIN ), 'blog' => __( 'Blog Page', Ace::TEXTDOMAIN ), 'archive' => __( 'Archive Pages', Ace::TEXTDOMAIN ), 'singular' => __( 'Content Pages', Ace::TEXTDOMAIN ), 'error' => __( '404 Pages', Ace::TEXTDOMAIN ), ); if ( function_exists( 'is_woocommerce' ) ) { $this->pageTypes['woocommerce_page'] = __( 'WooCommerce Page', Ace::TEXTDOMAIN ); } if ( function_exists( 'is_bbpress' ) ) { $this->pageTypes['bbpress_page'] = __( 'bbPress Page', Ace::TEXTDOMAIN ); } if ( function_exists( 'bp_is_blog_page' ) ) { $this->pageTypes['buddypress_page'] = __( 'BuddyPress Page', Ace::TEXTDOMAIN ); } // WC Pages $this->wcPages = array( 'wc_page' => __( 'WooCommerce', Ace::TEXTDOMAIN ), 'wc_cart' => __( 'Cart', Ace::TEXTDOMAIN ), 'wc_checkout' => __( 'Checkout', Ace::TEXTDOMAIN ), 'wc_account_page' => __( 'Account Page', Ace::TEXTDOMAIN ), ); // Devices $this->devices = array( 'all' => esc_html( 'All Devices', Ace::TEXTDOMAIN ), 'pc' => __( 'PC', Ace::TEXTDOMAIN ), 'mobile' => __( 'Only Mobile', Ace::TEXTDOMAIN ), ); } /** * Init Hooks */ protected function initHooks() { // Enqueue Admin Scripts add_action( 'admin_enqueue_scripts', array( $this, 'adminEnqueueScripts' ) ); // Check add_action( 'the_widget', array( $this, 'registerUsedWidget' ), 10, 3 ); // Add Settings to Each Widget add_filter( 'widget_display_callback', array( $this, 'widgetDisplayCallback' ), 10, 3 ); add_filter( 'widget_update_callback', array( $this, 'widgetUpdateCallback' ), 10, 4 ); add_action( 'in_widget_form', array( $this, 'renderFormDisplayConditionInWidgetForm' ), 10, 3 ); // Check used Widget add_filter( 'widget_display_callback', array( $this, 'registerUsedWidget' ), 100, 3 ); } /** * Add action for init widget class * Need seperate by php version( 5.3+ or not ) * * @param string $widget_class_name **/ protected function initWidget( $widget_class_name ) { // Check the required param if ( ! isset( $widget_class_name ) || ! is_string( $widget_class_name ) || '' === $widget_class_name ) { return; } // 5.2+ or less if ( version_compare( PHP_VERSION, '5.3.0', '<' ) ) { add_action( 'widgets_init', create_function( '', 'return register_widget( "' . $widget_class_name . '" );' ) ); } // 5.3+ else { add_action( 'widgets_init', function() use ( $widget_class_name ) { register_widget( $widget_class_name ); } ); } } /** * Enqueue Scripts in Admin **/ public function adminEnqueueScripts( $hook ) { wp_enqueue_style( 'ace-widget-form' ); wp_enqueue_script( 'ace-widget-popup-setting-box' ); } /** * Check **/ /** * **/ public function registerUsedWidget( $widget, $instance ) { if ( ! is_object( $instance ) || ! is_subclass_of( $instance, 'WP_Widget' ) ) return $widget; if( false === $widget ) return $widget; $class_name = get_class( $instance ); $this->addUsedWidget( $class_name ); return $widget; } /** * Additional Settings for Each Widget **/ /** * Filter method to Judge if $widget is displayed * * @param array $instance * @param obj $widget * @param array $args * * @return array|bool $instance : return $instance if is displayed, otherwise returns false **/ function widgetDisplayCallback( $instance, $widget, $args ) { $defaults = array( 'ace_display_condition' => '{}' ); $instance = wp_parse_args( ( array ) $instance, $defaults ); $condition = isset( $instance['ace_display_condition'] ) ? json_decode( $instance['ace_display_condition'], true ) : array(); unset( $defaults ); if ( $condition === array() ) { return $instance; } $show = isset( $condition['show'] ) ? $condition['show'] : $this->defaultsShow; $hide = isset( $condition['hide'] ) ? $condition['hide'] : $this->defaultsHide; // Get Device Settings $show_in_device = esc_attr( isset( $show['device'] ) ? $show['device'] : 'all' ); // Device Detect if( $show_in_device === 'all' ) { } elseif( 'pc' === $show_in_device && wp_is_mobile() ) { return false; } elseif( 'mobile' === $show_in_device && ! wp_is_mobile() ) { return false; } $show_in_device = null; // Priority $display_home = esc_attr( isset( $show['home'] ) ? $show['home'] : '' ); $display_front = esc_attr( isset( $show['front'] ) ? $show['front'] : '' ); $display_blog = esc_attr( isset( $show['blog'] ) ? $show['blog'] : '' ); // General Page $display_archive = esc_attr( isset( $show['archive'] ) ? $show['archive'] : '' ); $display_singular = esc_attr( isset( $show['singular'] ) ? $show['singular'] : '' ); $display_404 = esc_attr( isset( $show['error'] ) ? $show['error'] : '' ); // Plugin Page $display_buddypress = esc_attr( isset( $show['buddypress_page'] ) ? $show['buddypress_page'] : '' ); $display_bbpress = esc_attr( isset( $show['bbpress_page'] ) ? $show['bbpress_page'] : '' ); $display_woocommerce = esc_attr( isset( $show['woocommerce_page'] ) ? $show['woocommerce_page'] : '' ); // Page Detect if( is_home() && is_front_page() ) { if ( 'on' !== $display_home ) return false; } elseif( ! is_home() && is_front_page() ) { if ( 'on' !== $display_front ) return false; } elseif( is_home() && ! is_front_page() ) { if ( 'on' !== $display_blog ) return false; } elseif( function_exists( 'bp_is_blog_page' ) && ! bp_is_blog_page() ) { if ( 'on' !== $display_buddypress ) return false; } elseif( function_exists( 'is_bbpress' ) && is_bbpress() ) { if ( 'on' !== $display_bbpress ) return false; } elseif( function_exists( 'is_woocommerce' ) && ( is_woocommerce() || is_cart() || is_checkout() || is_account_page() || is_wc_endpoint_url() ) ) { if ( 'on' !== $display_woocommerce ) return false; } elseif( is_archive() ) { if ( 'on' !== $display_archive ) return false; } elseif( is_singular() ) { if ( 'on' !== $display_singular ) return false; } elseif ( is_404() ) { if ( 'on' !== $display_404 ) return false; } else { return false; } $display_home = $display_front = $display_blog = $display_archive = $display_singular = null; // Post Type if( ! is_front_page() && is_singular() ) { // Post Type global $post; $post_type = $post->post_type; if( isset( $hide['post_type_' . $post_type ] ) && 'on' === $hide['post_type_' . $post_type ] ) { return false; } // WooCommerce if( function_exists( 'is_woocommerce' ) && ( is_woocommerce() || is_cart() || is_checkout() || is_account_page() || is_wc_endpoint_url() ) ) { if ( isset( $hide['wc_page'] ) && 'on' === $hide['wc_page'] ) { return false; } elseif ( is_cart() && isset( $hide['wc_cart'] ) && 'on' === $hide['wc_cart'] ) { return false; } elseif ( is_checkout() && isset( $hide['wc_checkout'] ) && 'on' === $hide['wc_checkout'] ) { return false; } elseif ( is_account_page() && isset( $hide['wc_account_page'] ) && 'on' === $hide['wc_account_page'] ) { return false; } if( in_array( $post_type, array( 'shop_order', 'shop_order_refund', 'postshop_coupon', 'shop_webhook' ) ) ) { return false; } } } else { if( function_exists( 'is_woocommerce' ) && ( is_woocommerce() || is_cart() || is_checkout() || is_account_page() || is_wc_endpoint_url() ) && ! empty( $hide['wc_page'] ) ) { return false; } } return $instance; } /** * Filter Method to Update * * @param array $instance * @param array $new_instance * @param array $old_instance * @param object $widget * * @return array $instance **/ function widgetUpdateCallback( $instance, $new_instance, $old_instance, $widget ) { // Post Type $this->publicPostTypes = get_post_types( array( 'public' => true ) ); foreach ( $this->publicPostTypes as $post_type ) { $this->defaultsHide['post_type_' . $post_type ] = ''; } // Show $show = array(); if ( isset( $new_instance['ace_widget_show'] ) && is_array( $new_instance['ace_widget_show'] ) && 0 < count( $new_instance['ace_widget_show'] ) ) { foreach ( $this->pageTypes as $page => $value ) { $show[ $page ] = sanitize_text_field( ( isset( $new_instance['ace_widget_show'][ $page ] ) && 'on' === $new_instance['ace_widget_show'][ $page ] ) ? $new_instance['ace_widget_show'][ $page ] : '' ); } } unset( $new_instance['ace_widget_show'] ); $show['device'] = sanitize_text_field( isset( $new_instance['ace_widget_show_in_devices'] ) ? $new_instance['ace_widget_show_in_devices'] : '' ); $new_show = wp_parse_args( $show, $this->defaultsShow ); unset( $new_instance['ace_widget_show_in_devices'] ); // Hide // Post Types $hide_in_post_types = array(); if ( $new_instance['ace_widget_hide_in_post_types'] && is_array( $new_instance['ace_widget_hide_in_post_types'] ) && 0 < count( $new_instance['ace_widget_hide_in_post_types'] ) ) { foreach( $this->publicPostTypes as $post_type ) { if ( isset( $new_instance['ace_widget_hide_in_post_types'][ $post_type ] ) && is_string( $new_instance['ace_widget_hide_in_post_types'][ $post_type ] ) && 'on' === $new_instance['ace_widget_hide_in_post_types'][ $post_type ] ) { $value = 'on'; } else { $value = ''; } $key = 'post_type_' . $post_type; $hide_in_post_types[ $key ] = $value; } unset( $new_instance['ace_widget_hide_in_post_types'] ); } // Plugins $hide_in_plugin_pages = array(); // WooCommerce $hide_in_wc_pages = array(); if( function_exists( 'is_woocommerce' ) ) { if ( $new_instance['ace_widget_hide_in_woocommerce'] && is_array( $new_instance['ace_widget_hide_in_woocommerce'] ) && 0 < count( $new_instance['ace_widget_hide_in_woocommerce'] ) ) { foreach( $this->defaultsHideInWC as $wc_page => $default ) { if ( isset( $new_instance['ace_widget_hide_in_woocommerce'][ $wc_page ] ) && is_string( $new_instance['ace_widget_hide_in_woocommerce'][ $wc_page ] ) && 'on' === $new_instance['ace_widget_hide_in_woocommerce'][ $wc_page ] ) { $value = 'on'; } else { $value = ''; } $hide_in_wc_pages[ $wc_page ] = $value; } unset( $new_instance['ace_widget_hide_in_woocommerce'] ); } } $hide_in_plugin_pages = wp_parse_args( $hide_in_plugin_pages, $hide_in_wc_pages ); // BuddyPress /* $show_in_bp_pages = array(); if( function_exists( 'bp_is_blog_page' ) ) { if ( $new_instance['ace_widget_show_in_buddypress'] && is_array( $new_instance['ace_widget_show_in_buddypress'] ) && 0 < count( $new_instance['ace_widget_show_in_buddypress'] ) ) { foreach( $this->defaultsShowInPlugins as $bp_page => $default ) { $value = ''; if ( isset( $new_instance['ace_widget_show_in_buddypress'][ $bp_page ] ) && is_string( $new_instance['ace_widget_show_in_buddypress'][ $bp_page ] ) && 'on' === $new_instance['ace_widget_show_in_buddypress'][ $bp_page ] ) { $value = 'on'; } $show_in_bp_pages[ $bp_page ] = $value; } unset( $new_instance['ace_widget_show_in_buddypress'] ); } } $new_show = wp_parse_args( $new_show, $show_in_bp_pages ); */ $show_condition = array( 'show' => $new_show, 'hide' => wp_parse_args( $hide_in_post_types, $hide_in_plugin_pages ) ); $show_condition_json = json_encode( $show_condition, JSON_UNESCAPED_UNICODE ); $instance['ace_display_condition'] = sanitize_text_field( $show_condition_json ); return $instance; } /** * Print Form in Widget Form **/ function renderFormDisplayConditionInWidgetForm( $widget, $return, $instance ) { // Post Type $this->publicPostTypes = get_post_types( array( 'public' => true ) ); foreach ( $this->publicPostTypes as $post_type ) { $this->defaultsHide['post_type_' . $post_type ] = ''; } $this->defaultsHide = wp_parse_args( $this->defaultsHide, $this->defaultsHideInWC ); $defaults = array( 'ace_display_condition' => '{}' ); $instance = wp_parse_args( ( array ) $instance, $defaults ); $condition = json_decode( $instance['ace_display_condition'], true ); $show = isset( $condition['show'] ) ? $condition['show'] : array(); $show = wp_parse_args( $show, $this->defaultsShow ); $show = wp_parse_args( $show, $this->defaultsShowInPlugins ); $hide = isset( $condition['hide'] ) ? $condition['hide'] : array(); $hide = wp_parse_args( $hide, $this->defaultsHide ); ?>


pageTypes as $page_type => $label ) { $name = sprintf( '%1$s[%2$s]', $widget->get_field_name( 'ace_widget_show' ), $page_type ); $id = sprintf( '%1$s_%2$s', $widget->get_field_id( 'ace_widget_show' ), $page_type ) ?> />


publicPostTypes as $post_type ) { if( in_array( $post_type, array( 'revision', 'nav_menu_item', 'product', 'product_variation', 'shop_order', 'shop_order_refund', 'shop_coupon', 'shop_webhook' ) ) ) continue; $name = sprintf( '%1$s[%2$s]', $widget->get_field_name( 'ace_widget_hide_in_post_types' ), $post_type ); $id = sprintf( '%1$s_%2$s', $widget->get_field_id( 'ace_widget_hide_in_post_types' ), $post_type ) ?> />


wcPages as $wc_page => $label ) { $name = sprintf( '%1$s[%2$s]', $widget->get_field_name( 'ace_widget_hide_in_woocommerce' ), $wc_page ); $id = sprintf( '%1$s_%2$s', $widget->get_field_id( 'ace_widget_hide_in_woocommerce' ), $wc_page ) ?> />


devices as $device => $label ) { $name = $widget->get_field_name( 'ace_widget_show_in_devices' ); $id = sprintf( '%1$s_%2$s', $widget->get_field_id( 'ace_widget_show_in_devices' ), $device ) ?> />