Created
December 5, 2014 19:47
-
-
Save drrobotnik/16ebf14e5543a2192745 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php /** | |
* Plugin Name: Jetpack widget visibility query args | |
* Plugin URI: http://caavadesign.com | |
* Description: Add the ability to add query args to jetpacks visibility widget | |
* Version: 1.0.0 | |
* Author: Brandon lavigne | |
* Author URI: http://caavadesign.com | |
* License: A short license name. Example: GPL2 | |
*/ | |
remove_action( 'init', array( 'Jetpack_Widget_Conditions', 'init' ) ); | |
/** | |
* Hide or show widgets conditionally. | |
*/ | |
class Caava_Widget_Conditions extends Jetpack_Widget_Conditions { | |
public static function init() { | |
parent::init(); | |
} | |
public static function widget_admin_setup() { | |
if( is_rtl() ) { | |
wp_enqueue_style( 'widget-conditions', plugins_url( 'widget-conditions/rtl/widget-conditions-rtl.css', __FILE__ ) ); | |
} else { | |
wp_enqueue_style( 'widget-conditions', plugins_url( 'widget-conditions/widget-conditions.css', __FILE__ ) ); | |
} | |
wp_enqueue_style( 'widget-conditions', plugins_url( 'widget-conditions/widget-conditions.css', __FILE__ ) ); | |
wp_enqueue_script( 'widget-conditions', plugins_url( 'widget-conditions/widget-conditions.js', __FILE__ ), array( 'jquery', 'jquery-ui-core' ), 20140721, true ); | |
} | |
public static function condition_minor_visible( $rule ) { | |
$result = apply_filters( 'widget_visibility_minor_visible', true, $rule ); | |
if( true != $result ) { | |
echo ' hidden'; | |
} | |
} | |
/** | |
* Provided a second level of granularity for widget conditions. | |
*/ | |
public static function widget_conditions_options_echo( $major = '', $minor = '' ) { | |
switch ( $major ) { | |
case 'category': | |
?> | |
<option value=""><?php _e( 'All category pages', 'jetpack' ); ?></option> | |
<?php | |
$categories = get_categories( array( 'number' => 1000, 'orderby' => 'count', 'order' => 'DESC' ) ); | |
usort( $categories, array( __CLASS__, 'strcasecmp_name' ) ); | |
foreach ( $categories as $category ) { | |
?> | |
<option value="<?php echo esc_attr( $category->term_id ); ?>" <?php selected( $category->term_id, $minor ); ?>><?php echo esc_html( $category->name ); ?></option> | |
<?php | |
} | |
break; | |
case 'loggedin': | |
?> | |
<option value="loggedin" <?php selected( 'loggedin', $minor ); ?>><?php _e( 'Logged In', 'jetpack' ); ?></option> | |
<option value="loggedout" <?php selected( 'loggedout', $minor ); ?>><?php _e( 'Logged Out', 'jetpack' ); ?></option> | |
<?php | |
break; | |
case 'author': | |
?> | |
<option value=""><?php _e( 'All author pages', 'jetpack' ); ?></option> | |
<?php | |
foreach ( get_users( array( 'orderby' => 'name', 'exclude_admin' => true ) ) as $author ) { | |
?> | |
<option value="<?php echo esc_attr( $author->ID ); ?>" <?php selected( $author->ID, $minor ); ?>><?php echo esc_html( $author->display_name ); ?></option> | |
<?php | |
} | |
break; | |
case 'role': | |
global $wp_roles; | |
foreach ( $wp_roles->roles as $role_key => $role ) { | |
?> | |
<option value="<?php echo esc_attr( $role_key ); ?>" <?php selected( $role_key, $minor ); ?> ><?php echo esc_html( $role['name'] ); ?></option> | |
<?php | |
} | |
break; | |
case 'tag': | |
?> | |
<option value=""><?php _e( 'All tag pages', 'jetpack' ); ?></option> | |
<?php | |
$tags = get_tags( array( 'number' => 1000, 'orderby' => 'count', 'order' => 'DESC' ) ); | |
usort( $tags, array( __CLASS__, 'strcasecmp_name' ) ); | |
foreach ( $tags as $tag ) { | |
?> | |
<option value="<?php echo esc_attr($tag->term_id ); ?>" <?php selected( $tag->term_id, $minor ); ?>><?php echo esc_html( $tag->name ); ?></option> | |
<?php | |
} | |
break; | |
case 'date': | |
?> | |
<option value="" <?php selected( '', $minor ); ?>><?php _e( 'All date archives', 'jetpack' ); ?></option> | |
<option value="day"<?php selected( 'day', $minor ); ?>><?php _e( 'Daily archives', 'jetpack' ); ?></option> | |
<option value="month"<?php selected( 'month', $minor ); ?>><?php _e( 'Monthly archives', 'jetpack' ); ?></option> | |
<option value="year"<?php selected( 'year', $minor ); ?>><?php _e( 'Yearly archives', 'jetpack' ); ?></option> | |
<?php | |
break; | |
case 'page': | |
// Previously hardcoded post type options. | |
if ( ! $minor ) | |
$minor = 'post_type-page'; | |
else if ( 'post' == $minor ) | |
$minor = 'post_type-post'; | |
?> | |
<option value="front" <?php selected( 'front', $minor ); ?>><?php _e( 'Front page', 'jetpack' ); ?></option> | |
<option value="posts" <?php selected( 'posts', $minor ); ?>><?php _e( 'Posts page', 'jetpack' ); ?></option> | |
<option value="archive" <?php selected( 'archive', $minor ); ?>><?php _e( 'Archive page', 'jetpack' ); ?></option> | |
<option value="404" <?php selected( '404', $minor ); ?>><?php _e( '404 error page', 'jetpack' ); ?></option> | |
<option value="search" <?php selected( 'search', $minor ); ?>><?php _e( 'Search results', 'jetpack' ); ?></option> | |
<optgroup label="<?php esc_attr_e( 'Post type:', 'jetpack' ); ?>"> | |
<?php | |
$post_types = get_post_types( array( 'public' => true ), 'objects' ); | |
foreach ( $post_types as $post_type ) { | |
?> | |
<option value="<?php echo esc_attr( 'post_type-' . $post_type->name ); ?>" <?php selected( 'post_type-' . $post_type->name, $minor ); ?>><?php echo esc_html( $post_type->labels->singular_name ); ?></option> | |
<?php | |
} | |
?> | |
</optgroup> | |
<optgroup label="<?php esc_attr_e( 'Static page:', 'jetpack' ); ?>"> | |
<?php | |
echo str_replace( ' value="' . esc_attr( $minor ) . '"', ' value="' . esc_attr( $minor ) . '" selected="selected"', preg_replace( '/<\/?select[^>]*?>/i', '', wp_dropdown_pages( array( 'echo' => false ) ) ) ); | |
?> | |
</optgroup> | |
<?php | |
break; | |
case 'taxonomy': | |
?> | |
<option value=""><?php _e( 'All taxonomy pages', 'jetpack' ); ?></option> | |
<?php | |
$taxonomies = get_taxonomies( array( '_builtin' => false ), 'objects' ); | |
usort( $taxonomies, array( __CLASS__, 'strcasecmp_name' ) ); | |
foreach ( $taxonomies as $taxonomy ) { | |
?> | |
<optgroup label="<?php esc_attr_e( $taxonomy->labels->name . ':', 'jetpack' ); ?>"> | |
<option value="<?php echo esc_attr( $taxonomy->name ); ?>" <?php selected( $taxonomy->name, $minor ); ?>><?php echo 'All ' . esc_html( $taxonomy->name ) . ' pages'; ?></option> | |
<?php | |
$terms = get_terms( array( $taxonomy->name ), array( 'number' => 250, 'hide_empty' => false ) ); | |
foreach ( $terms as $term ) { | |
?> | |
<option value="<?php echo esc_attr( $taxonomy->name . '_tax_' . $term->term_id ); ?>" <?php selected( $taxonomy->name . '_tax_' . $term->term_id, $minor ); ?>><?php echo esc_html( $term->name ); ?></option> | |
<?php | |
} | |
?> | |
</optgroup> | |
<?php | |
} | |
break; | |
} | |
} | |
/** | |
* This is the AJAX endpoint for the second level of conditions. | |
*/ | |
public static function widget_conditions_options() { | |
self::widget_conditions_options_echo( $_REQUEST['major'], isset( $_REQUEST['minor'] ) ? $_REQUEST['minor'] : '' ); | |
die; | |
} | |
/** | |
* Add the widget conditions to each widget in the admin. | |
* | |
* @param $widget unused. | |
* @param $return unused. | |
* @param array $instance The widget settings. | |
*/ | |
public static function widget_conditions_admin( $widget, $return, $instance ) { | |
$conditions = array(); | |
if ( isset( $instance['conditions'] ) ) | |
$conditions = $instance['conditions']; | |
if ( ! isset( $conditions['action'] ) ) | |
$conditions['action'] = 'show'; | |
if ( empty( $conditions['rules'] ) ) | |
$conditions['rules'][] = apply_filters( 'widget_visibility_conditions', array( 'major' => '', 'minor' => '' ) ); | |
?> | |
<div class="widget-conditional <?php if ( empty( $_POST['widget-conditions-visible'] ) || $_POST['widget-conditions-visible'] == '0' ) { ?>widget-conditional-hide<?php } ?>"> | |
<input type="hidden" name="widget-conditions-visible" value="<?php if ( isset( $_POST['widget-conditions-visible'] ) ) { echo esc_attr( $_POST['widget-conditions-visible'] ); } else { ?>0<?php } ?>" /> | |
<?php if ( ! isset( $_POST['widget-conditions-visible'] ) ) { ?><a href="#" class="button display-options"><?php _e( 'Visibility', 'jetpack' ); ?></a><?php } ?> | |
<div class="widget-conditional-inner"> | |
<div class="condition-top"> | |
<?php printf( _x( '%s if:', 'placeholder: dropdown menu to select widget visibility; hide if or show if', 'jetpack' ), '<select name="conditions[action]"><option value="show" ' . selected( $conditions['action'], 'show', false ) . '>' . esc_html_x( 'Show', 'Used in the "%s if:" translation for the widget visibility dropdown', 'jetpack' ) . '</option><option value="hide" ' . selected( $conditions['action'], 'hide', false ) . '>' . esc_html_x( 'Hide', 'Used in the "%s if:" translation for the widget visibility dropdown', 'jetpack' ) . '</option></select>' ); ?> | |
</div><!-- .condition-top --> | |
<div class="conditions"> | |
<?php | |
foreach ( $conditions['rules'] as $rule ) { | |
?> | |
<div class="condition"> | |
<div class="selection alignleft"> | |
<select class="conditions-rule-major" name="conditions[rules_major][]"> | |
<option value="" <?php selected( "", $rule['major'] ); ?>><?php echo esc_html_x( '-- Select --', 'Used as the default option in a dropdown list', 'jetpack' ); ?></option> | |
<option value="category" <?php selected( "category", $rule['major'] ); ?>><?php esc_html_e( 'Category', 'jetpack' ); ?></option> | |
<option value="author" <?php selected( "author", $rule['major'] ); ?>><?php echo esc_html_x( 'Author', 'Noun, as in: "The author of this post is..."', 'jetpack' ); ?></option> | |
<?php | |
// this doesn't work on .com because of caching | |
if( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) { | |
?> | |
<option value="loggedin" <?php selected( "loggedin", $rule['major'] ); ?>><?php echo esc_html_x( 'User', 'Noun', 'jetpack' ); ?></option> | |
<option value="role" <?php selected( "role", $rule['major'] ); ?>><?php echo esc_html_x( 'Role', 'Noun, as in: "The user role of that can access this widget is..."', 'jetpack' ); ?></option> | |
<?php } ?> | |
<option value="tag" <?php selected( "tag", $rule['major'] ); ?>><?php echo esc_html_x( 'Tag', 'Noun, as in: "This post has one tag."', 'jetpack' ); ?></option> | |
<option value="date" <?php selected( "date", $rule['major'] ); ?>><?php echo esc_html_x( 'Date', 'Noun, as in: "This page is a date archive."', 'jetpack' ); ?></option> | |
<option value="page" <?php selected( "page", $rule['major'] ); ?>><?php echo esc_html_x( 'Page', 'Example: The user is looking at a page, not a post.', 'jetpack' ); ?></option> | |
<?php if ( get_taxonomies( array( '_builtin' => false ) ) ) : ?> | |
<option value="taxonomy" <?php selected( "taxonomy", $rule['major'] ); ?>><?php echo esc_html_x( 'Taxonomy', 'Noun, as in: "This post has one taxonomy."', 'jetpack' ); ?></option> | |
<?php do_action('widget_visibility_condition_major', $rule ); ?> | |
<?php endif; ?> | |
</select> | |
<?php _ex( 'is', 'Widget Visibility: {Rule Major [Page]} is {Rule Minor [Search results]}', 'jetpack' ); ?> | |
<select class="conditions-rule-minor<?php self::condition_minor_visible( $rule ); ?>" name="conditions[rules_minor][]" <?php if ( ! $rule['major'] ) { ?> disabled="disabled"<?php } ?> data-loading-text="<?php esc_attr_e( 'Loading...', 'jetpack' ); ?>"> | |
<?php self::widget_conditions_options_echo( $rule['major'], $rule['minor'] ); ?> | |
</select> | |
<?php do_action( 'widget_visibility_additional_fields', $rule ); ?> | |
</div> | |
<div class="condition-control"> | |
<span class="condition-conjunction"><?php echo esc_html_x( 'or', 'Shown between widget visibility conditions.', 'jetpack' ); ?></span> | |
<div class="actions alignright"> | |
<a href="#" class="delete-condition"><?php esc_html_e( 'Delete', 'jetpack' ); ?></a> | <a href="#" class="add-condition"><?php esc_html_e( 'Add', 'jetpack' ); ?></a> | |
</div> | |
</div> | |
</div><!-- .condition --> | |
<?php | |
} | |
?> | |
</div><!-- .conditions --> | |
</div><!-- .widget-conditional-inner --> | |
</div><!-- .widget-conditional --> | |
<?php | |
} | |
/** | |
* On an AJAX update of the widget settings, process the display conditions. | |
* | |
* @param array $new_instance New settings for this instance as input by the user. | |
* @param array $old_instance Old settings for this instance. | |
* @return array Modified settings. | |
*/ | |
public static function widget_update( $instance, $new_instance, $old_instance ) { | |
$conditions = array(); | |
$conditions['action'] = $_POST['conditions']['action']; | |
$conditions['rules'] = array(); | |
foreach ( $_POST['conditions']['rules_major'] as $index => $major_rule ) { | |
if ( ! $major_rule ) | |
continue; | |
$defaults = array( | |
'major' => $major_rule, | |
'minor' => isset( $_POST['conditions']['rules_minor'][$index] ) ? $_POST['conditions']['rules_minor'][$index] : '' | |
); | |
$conditions['rules'][] = apply_filters( 'widget_conditions_defaults', $defaults, $index ); | |
} | |
if ( ! empty( $conditions['rules'] ) ) | |
$instance['conditions'] = $conditions; | |
else | |
unset( $instance['conditions'] ); | |
if ( | |
( isset( $instance['conditions'] ) && ! isset( $old_instance['conditions'] ) ) | |
|| | |
( | |
isset( $instance['conditions'], $old_instance['conditions'] ) | |
&& | |
serialize( $instance['conditions'] ) != serialize( $old_instance['conditions'] ) | |
) | |
) { | |
do_action( 'widget_conditions_save' ); | |
} | |
else if ( ! isset( $instance['conditions'] ) && isset( $old_instance['conditions'] ) ) { | |
do_action( 'widget_conditions_delete' ); | |
} | |
return $instance; | |
} | |
/** | |
* Filter the list of widgets for a sidebar so that active sidebars work as expected. | |
* | |
* @param array $widget_areas An array of widget areas and their widgets. | |
* @return array The modified $widget_area array. | |
*/ | |
public static function sidebars_widgets( $widget_areas ) { | |
$settings = array(); | |
foreach ( $widget_areas as $widget_area => $widgets ) { | |
if ( empty( $widgets ) ) | |
continue; | |
if ( 'wp_inactive_widgets' == $widget_area ) | |
continue; | |
foreach ( $widgets as $position => $widget_id ) { | |
// Find the conditions for this widget. | |
if ( preg_match( '/^(.+?)-(\d+)$/', $widget_id, $matches ) ) { | |
$id_base = $matches[1]; | |
$widget_number = intval( $matches[2] ); | |
} | |
else { | |
$id_base = $widget_id; | |
$widget_number = null; | |
} | |
if ( ! isset( $settings[$id_base] ) ) { | |
$settings[$id_base] = get_option( 'widget_' . $id_base ); | |
} | |
// New multi widget (WP_Widget) | |
if ( ! is_null( $widget_number ) ) { | |
if ( isset( $settings[$id_base][$widget_number] ) && false === self::filter_widget( $settings[$id_base][$widget_number] ) ) { | |
unset( $widget_areas[$widget_area][$position] ); | |
} | |
} | |
// Old single widget | |
else if ( ! empty( $settings[ $id_base ] ) && false === self::filter_widget( $settings[$id_base] ) ) { | |
unset( $widget_areas[$widget_area][$position] ); | |
} | |
} | |
} | |
return $widget_areas; | |
} | |
/** | |
* Determine whether the widget should be displayed based on conditions set by the user. | |
* | |
* @param array $instance The widget settings. | |
* @return array Settings to display or bool false to hide. | |
*/ | |
public static function filter_widget( $instance ) { | |
global $wp_query; | |
if ( empty( $instance['conditions'] ) || empty( $instance['conditions']['rules'] ) ) | |
return $instance; | |
$condition_result = false; | |
foreach ( $instance['conditions']['rules'] as $rule ) { | |
switch ( $rule['major'] ) { | |
case 'date': | |
switch ( $rule['minor'] ) { | |
case '': | |
$condition_result = is_date(); | |
break; | |
case 'month': | |
$condition_result = is_month(); | |
break; | |
case 'day': | |
$condition_result = is_day(); | |
break; | |
case 'year': | |
$condition_result = is_year(); | |
break; | |
} | |
break; | |
case 'page': | |
// Previously hardcoded post type options. | |
if ( 'post' == $rule['minor'] ) | |
$rule['minor'] = 'post_type-post'; | |
else if ( ! $rule['minor'] ) | |
$rule['minor'] = 'post_type-page'; | |
switch ( $rule['minor'] ) { | |
case '404': | |
$condition_result = is_404(); | |
break; | |
case 'search': | |
$condition_result = is_search(); | |
break; | |
case 'archive': | |
$condition_result = is_archive(); | |
break; | |
case 'posts': | |
$condition_result = $wp_query->is_posts_page; | |
break; | |
case 'home': | |
$condition_result = is_home(); | |
break; | |
case 'front': | |
if ( current_theme_supports( 'infinite-scroll' ) ) | |
$condition_result = is_front_page(); | |
else { | |
$condition_result = is_front_page() && !is_paged(); | |
} | |
break; | |
default: | |
if ( substr( $rule['minor'], 0, 10 ) == 'post_type-' ) | |
$condition_result = is_singular( substr( $rule['minor'], 10 ) ); | |
else { | |
// $rule['minor'] is a page ID | |
$condition_result = is_page( $rule['minor'] ); | |
} | |
break; | |
} | |
break; | |
case 'tag': | |
if ( ! $rule['minor'] && is_tag() ) | |
$condition_result = true; | |
else if ( is_singular() && $rule['minor'] && has_tag( $rule['minor'] ) ) | |
$condition_result = true; | |
else { | |
$tag = get_tag( $rule['minor'] ); | |
if ( $tag && is_tag( $tag->slug ) ) | |
$condition_result = true; | |
} | |
break; | |
case 'category': | |
if ( ! $rule['minor'] && is_category() ) | |
$condition_result = true; | |
else if ( is_category( $rule['minor'] ) ) | |
$condition_result = true; | |
else if ( is_singular() && $rule['minor'] && in_array( 'category', get_post_taxonomies() ) && has_category( $rule['minor'] ) ) | |
$condition_result = true; | |
break; | |
case 'loggedin': | |
$condition_result = is_user_logged_in(); | |
if ( 'loggedin' !== $rule['minor'] ) { | |
$condition_result = ! $condition_result; | |
} | |
break; | |
case 'author': | |
$post = get_post(); | |
if ( ! $rule['minor'] && is_author() ) | |
$condition_result = true; | |
else if ( $rule['minor'] && is_author( $rule['minor'] ) ) | |
$condition_result = true; | |
else if ( is_singular() && $rule['minor'] && $rule['minor'] == $post->post_author ) | |
$condition_result = true; | |
break; | |
case 'role': | |
if( is_user_logged_in() ) { | |
global $current_user; | |
get_currentuserinfo(); | |
$user_roles = $current_user->roles; | |
if( in_array( $rule['minor'], $user_roles ) ) { | |
$condition_result = true; | |
} else { | |
$condition_result = false; | |
} | |
} else { | |
$condition_result = false; | |
} | |
break; | |
case 'taxonomy': | |
$term = explode( '_tax_', $rule['minor'] ); // $term[0] = taxonomy name; $term[1] = term id | |
if ( isset( $term[1] ) && is_tax( $term[0], $term[1] ) ) | |
$condition_result = true; | |
else if ( isset( $term[1] ) && is_singular() && $term[1] && has_term( $term[1], $term[0] ) ) | |
$condition_result = true; | |
else if ( is_singular() && $post_id = get_the_ID() ){ | |
$terms = get_the_terms( $post_id, $rule['minor'] ); // Does post have terms in taxonomy? | |
if( $terms & ! is_wp_error( $terms ) ) { | |
$condition_result = true; | |
} | |
} | |
break; | |
} | |
$condition_result = apply_filters( 'widget_visibility_filter_widget', $condition_result, $rule ); | |
} | |
if ( ( 'show' == $instance['conditions']['action'] && ! $condition_result ) || ( 'hide' == $instance['conditions']['action'] && $condition_result ) ) | |
return false; | |
return $instance; | |
} | |
public static function strcasecmp_name( $a, $b ) { | |
return strcasecmp( $a->name, $b->name ); | |
} | |
} | |
add_action( 'init', array( 'Caava_Widget_Conditions', 'init' ), 100 ); | |
function tt_widget_visibility_js() { ?> | |
<script> | |
jQuery(function($) { | |
$( document ).on( 'change.widgetconditions', 'select.conditions-rule-major', function() { | |
var $conditionsRuleMajor = $ ( this ); | |
var $conditionsRuleMinor = $conditionsRuleMajor.siblings( 'select.conditions-rule-minor:first' ); | |
var $conditionsRuleQueryVars = $conditionsRuleMajor.siblings( 'input.conditions-rule-query-var' ); | |
console.log($conditionsRuleMajor.val()); | |
console.log($conditionsRuleMinor); | |
if ( $conditionsRuleMajor.val() == 'query_vars' ) { | |
$conditionsRuleMinor.hide(); | |
$conditionsRuleQueryVars.show(); | |
}else{ | |
$conditionsRuleMinor.show(); | |
$conditionsRuleQueryVars.hide(); | |
} | |
}); | |
}); | |
</script><?php | |
} | |
function tt_widget_visibility_minor_visible( $default, $rule ) { | |
if ( 'query_vars' == $rule['major'] ) { | |
return false; | |
} | |
return true; | |
} | |
function tt_widget_visibility_condition_major( $rule ) { ?> | |
<option value="query_vars" <?php selected( "query_vars", $rule['major'] ); ?>><?php esc_html_e( 'Query Vars', 'jetpack' ); ?></option> | |
<?php } | |
function tt_widget_visibility_conditions( $conditions ) { | |
return array( 'major' => '', 'minor' => '', 'query_var_key' => '', 'query_var_value' => '' ); | |
} | |
function tt_widget_visibility_additional_fields( $rule ) { | |
?><input class="conditions-rule-query-var small-text<?php if ( ! $rule['major'] || 'query_vars' != $rule['major'] ) { ?> hidden<?php } ?>" name="conditions[rules_query_var_key][]" value="<?php echo $rule['query_var_key']; ?>" data-brandon="" placeholder="key" /> | |
<input class="conditions-rule-query-var small-text<?php if ( ! $rule['major'] || 'query_vars' != $rule['major'] ) { ?> hidden<?php } ?>" name="conditions[rules_query_var_value][]" value="<?php echo $rule['query_var_value']; ?>" data-brandon="" placeholder="value" /><?php | |
} | |
function tt_widget_conditions_defaults( $defaults, $index ) { | |
$defaults['query_var_key'] = isset( $_POST['conditions']['rules_query_var_key'][$index] ) ? $_POST['conditions']['rules_query_var_key'][$index] : ''; | |
$defaults['query_var_value'] = isset( $_POST['conditions']['rules_query_var_value'][$index] ) ? $_POST['conditions']['rules_query_var_value'][$index] : ''; | |
return $defaults; | |
} | |
function tt_widget_visibility_filter_widget( $condition_result, $rule ) { | |
if( 'query_vars' == $rule['major']) { | |
if( ICL_LANGUAGE_CODE == $rule['query_var_value'] ) { | |
$condition_result = true; | |
} else { | |
$condition_result = false; | |
} | |
} | |
return $condition_result; | |
} | |
add_action('admin_init','tt_jetpack_plugin_init'); | |
add_filter( 'widget_conditions_defaults', 'tt_widget_conditions_defaults', 10, 2 ); | |
add_filter( 'widget_visibility_conditions', 'tt_widget_visibility_conditions' ); | |
add_filter( 'widget_visibility_minor_visible', 'tt_widget_visibility_minor_visible', 10, 2 ); | |
add_filter( 'widget_visibility_filter_widget', 'tt_widget_visibility_filter_widget', 10, 2 ); | |
add_action( 'widget_visibility_condition_major', 'tt_widget_visibility_condition_major' ); | |
add_action( "widget_visibility_additional_fields", 'tt_widget_visibility_additional_fields' ); | |
function tt_jetpack_plugin_init() { | |
global $pagenow; | |
if( 'widgets.php' == $pagenow) { | |
add_action( 'admin_head', 'tt_widget_visibility_js',100); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment