Ejemplo n.º 1
0
/**
 * Display the form at the bottom of each widget.
 */
function conditional_widgets_form($widget, $return, $instance)
{
    /** always show the form */
    if ($return == 'noform') {
        $return = true;
    }
    //prefill variables
    $instance = conditional_widgets_init_instance($instance);
    //whether to display ON or OFF so users can easily see which widgets are conditional
    $active = cets_conditional_widgets_instance_is_active($instance);
    ?>
	<div class="cets-conditional-widget">
		<div class="conditional-widget-top">
        	<div class="conditional-widget-title-action">
            	<a href="#" id="conditional-widget-toggle-wrap-<?php 
    print $widget->id;
    ?>
" onclick="conditional_widgets_form_toggle('conditional-widget-form-<?php 
    print $widget->id;
    ?>
'); return false;"></a>
            </div>

            <div class="conditional-widget-title">
                <h5><?php 
    _e('Widget Display Control', 'conditional-widgets');
    ?>
                <?php 
    if ($active) {
        ?>
					<span class="conditional-widgets-active"><?php 
        _e('ON', 'conditional-widgets');
        ?>
</span>
					<?php 
    } else {
        ?>
                    <span class="conditional-widgets-inactive"><?php 
        _e('OFF', 'conditional-widgets');
        ?>
</span>
					<?php 
    }
    ?>
                </h5>
            </div>
		</div>

		<div class="conditional-widget-form" id="conditional-widget-form-<?php 
    print $widget->id;
    ?>
">
			<p class='cw-instructions'>
				<?php 
    _e('Select a combination of options to control on which sections of your site this widget is shown.', 'conditional-widgets');
    ?>
			</p>
			<p>
				<input type="checkbox" name="cw_home_enable_checkbox" id="cw_home_enable_checkbox_<?php 
    print $widget->id;
    ?>
" <?php 
    checked($instance['cw_home_enable_checkbox']);
    ?>
>
				<?php 
    conditional_widgets_form_show_hide_select('cw_select_home_page', $instance['cw_select_home_page'], true, true);
    ?>
				<label for="cw_home_enable_checkbox_<?php 
    print $widget->id;
    ?>
"><?php 
    _e('on Front Page', 'conditional-widgets');
    ?>
</label>
			</p>
			<?php 
    $type_tax_pairs = apply_filters('conditional_widgets_type_tax_pairs', array());
    if ($type_tax_pairs) {
        foreach ($type_tax_pairs as $pair) {
            $tax = $pair['tax'];
            $type = $pair['type'];
            $selected_ids = array();
            // valid taxonomy
            $taxonomy_object = get_taxonomy($tax);
            if ($taxonomy_object == false) {
                continue;
            }
            // valid type
            $post_type_object = get_post_type_object($type);
            if (!$post_type_object) {
                continue;
            }
            // taxonomy applied to type
            // @TODO - validate
            // for prefilling form fields..
            if (isset($instance['cw_custom'][$type][$tax])) {
                $custom_subdata = $instance['cw_custom'][$type][$tax];
            } else {
                $custom_subdata = conditional_widgets_get_default_custom_subdata();
            }
            if (isset($custom_subdata['selected_ids'])) {
                $selected_ids = $custom_subdata['selected_ids'];
            }
            echo "<h6 class='conditional-widget-header conditional-widget-sub-heading'>{$taxonomy_object->labels->name}</h6>";
            printf(__('%s and %s', 'conditional-widgets'), "<input type='checkbox' name='cw_custom[{$type}][{$tax}][enable]' value='1' " . checked($custom_subdata['enable'], 1, 0) . '>' . sprintf(_x('Enable %s Logic', '(Custom) Post Type will fill the placeholder', 'conditional-widgets'), $post_type_object->labels->singular_name) . '</label>', conditional_widgets_form_show_hide_select("cw_custom[{$type}][{$tax}][select]", $custom_subdata['select']) . sprintf(__('on %1$s in selected %2$s:', 'conditional-widgets'), $post_type_object->labels->name, $taxonomy_object->labels->name));
            echo "<p>";
            echo "<span class='cw_sub_checkbox'><label>";
            echo "<input type='checkbox' name='cw_custom[{$type}][{$tax}][all]' value='1' " . checked($custom_subdata['all'], 1, 0) . " >";
            printf(__('ALL %s (or select below)', 'conditional-widgets'), $taxonomy_object->labels->name);
            echo "</label></span>";
            if (is_taxonomy_hierarchical($tax)) {
                echo "<span class='cw_sub_checkbox'><label>";
                echo "<input type='checkbox' name='cw_custom[{$type}][{$tax}][sub]' " . checked($custom_subdata['sub'], 1, 0) . ">";
                printf(__('Include sub-%s automatically', 'conditional-widgets'), $taxonomy_object->labels->name);
                echo "</label></span>";
            }
            echo "</p>";
            echo "<div class='conditional-widgets-checkbox-wrapper'>";
            conditional_widgets_term_checkboxes($tax, $post_type_object->name, $selected_ids);
            echo "</div>";
        }
        // /foreach
    }
    ?>
			<h6 class="conditional-widget-header conditional-widget-sub-heading"><?php 
    _e('Pages');
    ?>
</h6>
			<p>
				<?php 
    printf(__('%s and %s', 'conditional-widgets'), '<label><input type="checkbox" name="cw_pages_enable_checkbox" ' . checked($instance['cw_pages_enable_checkbox'], 1, false) . '>' . sprintf(_x('Enable %s Logic', '(Custom) Post Type will fill the placeholder', 'conditional-widgets'), __('Page')) . '</label>', conditional_widgets_form_show_hide_select('cw_select_pages', $instance['cw_select_pages'], false, false) . __('on selected Pages:', 'conditional-widgets'));
    if (!isset($instance['cw_pages_all'])) {
        $instance['cw_pages_all'] = 0;
    }
    ?>
				<span class="cw_sub_checkbox">
					<label>
						<input type="checkbox" name="cw_pages_all" value="1" <?php 
    checked($instance['cw_pages_all']);
    ?>
>
						<?php 
    _e('ALL pages (or select below)', 'conditional-widgets');
    ?>
					</label>
				</span>

				<span class="cw_sub_checkbox">
					<label>
						<input type="checkbox" name="cw_pages_sub_checkbox" <?php 
    checked($instance['cw_pages_sub_checkbox']);
    ?>
>
						<?php 
    _e('Include sub-pages automatically', 'conditional-widgets');
    ?>
					</label>
				</span>
			</p>

			<div class="conditional-widgets-checkbox-wrapper">
			<?php 
    $selected_pages = array();
    if (isset($instance['cw_selected_pages']) && is_array($instance['cw_selected_pages'])) {
        $selected_pages = $instance['cw_selected_pages'];
    }
    conditional_widgets_page_checkboxes($selected_pages);
    ?>
			</div>

			<h6 class="conditional-widget-header conditional-widget-sub-heading"><?php 
    _e('Special Page Options', 'conditional-widgets');
    ?>
</h6>
			<ul class="conditional-widgets-special-page-option-list">
				<!-- posts page -->
				<li>
					<label>
						<input type="checkbox" name="cw_posts_page_hide_checkbox" <?php 
    checked($instance['cw_posts_page_hide']);
    ?>
>
						<?php 
    _e('Hide on Posts Page (when using a static front page)', 'conditional-widgets');
    ?>
					</label>
				</li>

				<!-- 404 -->
				<li>
					<label>
						<input type="checkbox" name="cw_404_hide_checkbox" <?php 
    checked($instance['cw_404_hide']);
    ?>
>
						<?php 
    _e('Hide on 404s (Page Not Found)', 'conditional-widgets');
    ?>
					</label>
				</li>

				<!-- search results -->
				<li>
					<label>
						<input type="checkbox" name="cw_search_hide_checkbox" <?php 
    checked($instance['cw_search_hide']);
    ?>
>
						<?php 
    _e('Hide when displaying Search Results', 'conditional-widgets');
    ?>
					</label>
				</li>

				<!-- archives -->
				<li>
					<label>
						<input type="checkbox" name="cw_author_archive_hide_checkbox" <?php 
    checked($instance['cw_author_archive_hide']);
    ?>
>
						<?php 
    _e('Hide on Author Archives', 'conditional-widgets');
    ?>
					</label>
				</li>
				<li>
					<label>
						<input type="checkbox" name="cw_date_archive_hide_checkbox" <?php 
    checked($instance['cw_date_archive_hide']);
    ?>
>
						<?php 
    _e('Hide on Date Archives', 'conditional-widgets');
    ?>
					</label>
				</li>
				<li>
					<label>
						<input type="checkbox" name="cw_tag_archive_hide_checkbox" <?php 
    checked($instance['cw_tag_archive_hide']);
    ?>
>
						<?php 
    _e('Hide on Tag Archives', 'conditional-widgets');
    ?>
					</label>
				</li>
			</ul>

			<h6 class="conditional-widget-header conditional-widget-sub-heading"><?php 
    _e('Other Options', 'conditional-widgets');
    ?>
</h6>
			<p><?php 
    _e('Note: The following options will take precendence over those above.', 'conditional-widgets');
    ?>
</p>
			<ul class="conditional-widgets-special-page-option-list">
				<li>
					<label>
						<input type="checkbox" name="cw_mobile_hide_checkbox" <?php 
    checked($instance['cw_mobile_hide']);
    ?>
>
						<?php 
    _e('Hide on Mobile', 'conditional-widgets');
    ?>
					</label>
				</li>
				<li>
					<label>
						<input type="checkbox" name="cw_desktop_hide_checkbox" <?php 
    checked($instance['cw_desktop_hide']);
    ?>
>
						<?php 
    _e('Hide on Desktop', 'conditional-widgets');
    ?>
					</label>
				</li>
			</ul>

			
			

		</div> <!-- toggled div -->

	</div> <!-- /.cets-conditional-widgets -->
	<?php 
}
Ejemplo n.º 2
0
/**
 * Determine whether or not this widget should be displayed on this page request
 */
function conditional_widgets_widget($instance)
{
    /* variables we have access to
    	$instance['cw_home_enable_checkbox']
    	$instance['cw_select_home_page']  //show == 1, hide == 0, show only on home == 2
    	
    	// pages
    	$instance['cw_pages_enable_checkbox']
    	$instance['cw_select_pages']
    	$instance['cw_pages_sub_checkbox']
    	$instance['cw_selected_pages']
    	$instance['cw_pages_all'] // since 1.8
    	
    	// custom_taxes  - since 1.9
    	$instance['cw_custom'] 
    	$instance['cw_custom'][$type][$tax]['enable']
    	$instance['cw_custom'][$type][$tax]['select']
    	$instance['cw_custom'][$type][$tax]['all'] 
    	$instance['cw_custom'][$type][$tax]['sub']
    	$instance['cw_custom'][$type][$tax]['selected_ids']
    	
    	// utility
    	$instance['cw_posts_page_hide']
    	$instance['cw_404_hide']
    	$instance['cw_search_hide']
    	$instance['cw_date_archive_hide']
    	$instance['cw_author_archive_hide']
    	$instance['cw_tag_archive_hide']
    
    	// utility - other
    	$instance['cw_mobile_hide'] - since 2.2
    	
    	*/
    // First, process 'other' options which take precedence over the following
    /* is_mobile */
    if (isset($instance['cw_mobile_hide']) && $instance['cw_mobile_hide'] == 1) {
        if (wp_is_mobile()) {
            return false;
        }
    }
    /* is_NOT_mobile */
    if (isset($instance['cw_desktop_hide']) && $instance['cw_desktop_hide'] == 1) {
        if (!wp_is_mobile()) {
            return false;
        }
    }
    global $wp_query;
    $qvars = $wp_query->query_vars;
    $type_tax_pairs = apply_filters('conditional_widgets_type_tax_pairs', array());
    $instance = conditional_widgets_init_instance($instance);
    if ($instance['cw_home_enable_checkbox']) {
        //box checked for home page logic takes priority by processing first
        switch ($instance['cw_select_home_page']) {
            case 0:
                //hide if front page
                if (is_front_page()) {
                    return false;
                }
                break;
            case 1:
                //show if front page
                if (is_front_page()) {
                    return $instance;
                }
                break;
            case 2:
                //only show on front, hide otherwise
                if (is_front_page()) {
                    return $instance;
                } else {
                    return false;
                }
                break;
        }
    }
    $arr_pages = $instance['cw_selected_pages'];
    if (!is_array($arr_pages)) {
        $arr_pages = array();
    }
    if ($instance['cw_pages_enable_checkbox'] && is_page()) {
        // We care about pages and this is a page.
        // see if we are using same logic for ALL pages
        if (isset($instance['cw_pages_all']) && $instance['cw_pages_all'] == 1) {
            if ($instance['cw_select_pages'] == 1) {
                // SHOW
                return $instance;
            } else {
                // HIDE
                return false;
            }
        }
        $current_page_id = $wp_query->post->ID;
        //see if we care about subpages
        if ($instance['cw_pages_sub_checkbox'] == 1) {
            foreach ($arr_pages as $page) {
                $args = 'child_of=' . $page;
                $newpages = get_pages($args);
                foreach ($newpages as $newpage) {
                    array_push($arr_pages, $newpage->ID);
                }
            }
        }
        //if subpages count
        $match = false;
        if (in_array($current_page_id, $arr_pages)) {
            $match = true;
        }
        if ($match) {
            //we matched a page
            if ($instance['cw_select_pages'] == 1) {
                //and we're showing on matched pages - SHOW
                return $instance;
            } else {
                //and we're hiding on matched pages - so HIDE
                return false;
            }
        } else {
            //we did NOT match a page
            if ($instance['cw_select_pages'] == 1) {
                //and we're telling it to show on matched pages - so HIDE
                return false;
            } else {
                //we didn't match a page, and we told it to hide on those pages - so SHOW
                return $instance;
            }
        }
    }
    //is_page && we care
    // is individual post of any type (other than page)
    if (is_single()) {
        $current_post_id = $wp_query->post->ID;
        $type = get_post_type();
        if (isset($instance['cw_custom'][$type])) {
            $subdata_type = $instance['cw_custom'][$type];
            foreach ($subdata_type as $tax => $subdata_type_tax) {
                // loop over each taxonomy with CW logic associated with this type
                if ($subdata_type_tax['enable'] != 1) {
                    // we don't care about this post type, so just display.
                    return $instance;
                }
                if ($subdata_type_tax['all'] == 1) {
                    // Don't care about specific terms, just the post type.
                    if ($subdata_type_tax['select'] == 1) {
                        return $instance;
                    } else {
                        return false;
                    }
                }
                // we DO care about specific terms, so see if we match a term or not
                $terms_to_match = $subdata_type_tax['selected_ids'];
                if (!is_array($terms_to_match)) {
                    $terms_to_match = array();
                }
                if ($subdata_type_tax['sub'] == 1) {
                    if (isset($subdata_type_tax['selected_ids']) && is_array($subdata_type_tax['selected_ids'])) {
                        foreach ($subdata_type_tax['selected_ids'] as $term) {
                            $kiddos = get_term_children($term, $tax);
                            $terms_to_match = array_merge($terms_to_match, $kiddos);
                        }
                    }
                }
                if ($terms_to_match) {
                    $match = is_object_in_term($current_post_id, $tax, $terms_to_match);
                } else {
                    $match = false;
                }
                if ($match) {
                    if ($subdata_type_tax['select'] == 1) {
                        return $instance;
                    } else {
                        return false;
                    }
                } else {
                    if ($subdata_type_tax['select'] == 1) {
                        return false;
                    } else {
                        return $instance;
                    }
                }
                //is_object_in_taxonomy - TODO?
            }
        } else {
            // we have no settings for this post type
            return $instance;
        }
    }
    // /is_single
    if (is_category()) {
        if (isset($instance['cw_custom']['post']['category'])) {
            $cat_subdata = $instance['cw_custom']['post']['category'];
        } else {
            // no post specific logic here
            return $instance;
        }
        if ($cat_subdata['enable'] != 1) {
            // we don't care about categories
            return $instance;
        }
        if ($cat_subdata['all'] == 1) {
            // Don't care about specific terms, just the post type.
            if ($cat_subdata['select'] == 1) {
                return $instance;
            } else {
                return false;
            }
        }
        // we DO care about specific terms, so see if we match a term or not
        $terms_to_match = $cat_subdata['selected_ids'];
        if ($cat_subdata['sub'] == 1) {
            if (isset($cat_subdata['selected_ids']) && is_array($cat_subdata['selected_ids'])) {
                foreach ($cat_subdata['selected_ids'] as $term) {
                    $kiddos = get_term_children($term, 'category');
                    $terms_to_match = array_merge($terms_to_match, $kiddos);
                }
            }
        }
        if (!is_array($terms_to_match)) {
            $terms_to_match = array();
        }
        $cat = $qvars['cat'];
        $match = in_array($cat, $terms_to_match);
        if ($match) {
            if ($cat_subdata['select'] == 1) {
                return $instance;
            } else {
                return false;
            }
        } else {
            if ($cat_subdata['select'] == 1) {
                return false;
            } else {
                return $instance;
            }
        }
    }
    // /is_category
    // TODO
    /*
    if (is_tax()) {
    	
    }
    */
    //since 1.4
    if (is_home()) {
        if ($instance['cw_posts_page_hide'] == 1) {
            return false;
        }
    }
    // since 1.1
    if (is_404()) {
        if ($instance['cw_404_hide'] == 1) {
            return false;
        }
    }
    //if 404
    // since 1.1
    if (is_search()) {
        if ($instance['cw_search_hide'] == 1) {
            return false;
        }
    }
    // if search
    // since 1.1
    if (is_author()) {
        if ($instance['cw_author_archive_hide'] == 1) {
            return false;
        }
    }
    // since 1.1
    if (is_date()) {
        if ($instance['cw_date_archive_hide'] == 1) {
            return false;
        }
    }
    //since 1.2
    if (is_tag()) {
        if ($instance['cw_tag_archive_hide'] == 1) {
            return false;
        }
    }
    //default to showing
    return $instance;
}