Example #1
0
function pc_restrictions_helper($subj, $param, $tax = false)
{
    $restr = array();
    // post types
    if ($subj == 'post') {
        // search in the taxonomy term
        $term = get_term_by('id', $param, $tax);
        $allowed = trim((string) get_option('taxonomy_' . $term->term_id . '_pg_redirect'));
        if ($allowed != '') {
            $restr[$term->name] = array();
            if ($allowed == 'all') {
                $restr[$term->name] = __('any logged user', 'pc_ml');
            } else {
                $allowed = explode(',', $allowed);
                $allowed_names = array();
                if (count($allowed) > 0) {
                    foreach ($allowed as $user_cat) {
                        $uc_data = get_term_by('id', $user_cat, 'pg_user_categories');
                        $allowed_names[] = $uc_data->name;
                    }
                    $restr[$term->name] = implode(', ', $allowed_names);
                }
            }
        }
        // check parent categories
        if (isset($term->category_parent) && $term->category_parent != 0) {
            $parent = get_term_by('id', $term->category_parent, $tax);
            // recursive
            $rec_restr = pc_restrictions_helper('post', $parent->term_id, $tax);
            if ($rec_restr) {
                $restr = array_merge($restr, $rec_restr);
            }
        }
    } else {
        // check parents page
        if ($param->post_parent != 0) {
            $parent = get_post($param->post_parent);
            $allowed = get_post_meta($parent->ID, 'pg_redirect', true);
            if ($allowed && is_array($allowed) && count($allowed) > 0) {
                if ($allowed[0] == 'all') {
                    $restr[$parent->post_title] = __('any logged user', 'pc_ml');
                } elseif ($allowed[0] == 'unlogged') {
                    $restr[$parent->post_title] = __('unlogged users', 'pc_ml');
                } else {
                    $allowed_names = array();
                    foreach ($allowed as $user_cat) {
                        $uc_data = get_term_by('id', $user_cat, 'pg_user_categories');
                        $allowed_names[] = $uc_data->name;
                    }
                    $restr[$parent->post_title] = implode(', ', $allowed_names);
                }
            }
            // check deeper in parents
            if ($param->post_parent != 0) {
                $post_obj = get_post($param->post_parent);
                // recursive
                $rec_restr = pc_restrictions_helper('page', $post_obj);
                if ($rec_restr) {
                    $restr = array_merge($restr, $rec_restr);
                }
            }
        }
    }
    return empty($restr) ? false : $restr;
}
Example #2
0
function pc_redirect_meta_setup()
{
    include_once PC_DIR . '/functions.php';
    global $post, $pc_users;
    $user_categories = get_terms('pg_user_categories', 'orderby=name&hide_empty=0');
    $pc_redirect = (array) get_post_meta($post->ID, 'pg_redirect', true);
    $pc_unlogged_redirect = get_post_meta($post->ID, 'pg_unlogged_redirect', true);
    ?>
    
    <p style="margin-bottom: 7px;"><?php 
    _e('Which user categories can see the page?', 'pc_ml');
    ?>
</p> 
    <div class="pc_tax_cat_list">
        <select name="pg_redirect[]" multiple="multiple" class="lcweb-chosen pc_pag_restr" data-placeholder="<?php 
    _e('Select categories', 'pc_ml');
    ?>
 .." autocomplete="off">
          <option value="all" class="pc_all_field" <?php 
    if (isset($pc_redirect[0]) && $pc_redirect[0] == 'all') {
        echo 'selected="selected"';
    }
    ?>
><?php 
    _e('All', 'pc_ml');
    ?>
</option>
          <option value="unlogged" class="pc_unl_field" <?php 
    if (isset($pc_redirect[0]) && $pc_redirect[0] == 'unlogged') {
        echo 'selected="selected"';
    }
    ?>
><?php 
    _e('Unlogged Users', 'pc_ml');
    ?>
</option>
          <?php 
    foreach ($user_categories as $ucat) {
        $selected = isset($pc_redirect[0]) && in_array($ucat->term_id, $pc_redirect) ? 'selected="selected"' : '';
        echo '<option value="' . $ucat->term_id . '" ' . $selected . '>' . $ucat->name . '</option>';
    }
    ?>
        </select>   

		<div id="pc_unl_redir_wrap" <?php 
    if (!in_array('unlogged', $pc_redirect)) {
        echo 'style="display: none;"';
    }
    ?>
>
            <p style="margin-bottom: 7px;"><?php 
    _e('Where to move logged users?', 'pc_ml');
    ?>
</p>
        
            <select name="pg_unlogged_redirect" class="lcweb-chosen" data-placeholder="<?php 
    _e('Select an option', 'pc_ml');
    ?>
 .." style="width: 254px;" autocomplete="off">
              <option value=""><?php 
    _e('Use main redirect target', 'pc_ml');
    ?>
</option>
              <?php 
    foreach (get_pages() as $pag) {
        if ($pag->ID == $post->ID) {
            continue;
        }
        // avoid loops
        $selected = $pc_unlogged_redirect == $pag->ID ? 'selected="selected"' : '';
        echo '<option value="' . $pag->ID . '" ' . $selected . '>' . $pag->post_title . '</option>';
    }
    ?>
            </select>
        </div>   
    </div>    
	
    <?php 
    //// check parent restrictions and print an helper
    global $current_screen;
    $restr = array();
    // post types
    if ($current_screen->id == 'post') {
        // search in every involved taxonomy
        foreach (pc_affected_tax() as $tax) {
            $terms = wp_get_post_terms($post->ID, $tax);
            if (is_array($terms)) {
                foreach ($terms as $term) {
                    $response = pc_restrictions_helper('post', $term->term_id, $tax);
                    if ($response) {
                        $restr = array_merge($restr, $response);
                    }
                }
            }
        }
        $sing_plur = count($restr) == 1 ? __('this category', 'pc_ml') : __('these categories', 'pc_ml');
    } else {
        $response = pc_restrictions_helper('page', $post);
        if ($response) {
            $restr = array_merge($restr, $response);
        }
        $sing_plur = count($restr) == 1 ? __('this parent', 'pc_ml') : __('these parents', 'pc_ml');
    }
    // print helper
    if (is_array($restr) && count($restr) > 0) {
        echo '<div id="pc_page_rest_helper">
			<strong>' . __('Page already restricted by', 'pc_ml') . ' ' . $sing_plur . ':</strong>
			<dl>';
        foreach ($restr as $index => $rs) {
            echo '<dt>' . $index . '</dt>
				<dd><em>' . __('visible by', 'pc_ml') . ' ' . $rs . '</em></dd>';
        }
        echo '</dl></div>';
    }
    ?>
    
    
    <?php 
    // comments block restriction
    if ($pc_users->wp_user_sync) {
        $pc_hide_comments = (array) get_post_meta($post->ID, 'pg_hide_comments', true);
        $pc_hc_warn = get_post_meta($post->ID, 'pg_hc_use_warning', true);
        ?>
		<hr style="margin-top: 18px;"/>
		
		<p style="margin-bottom: 7px; font-size: 99.2%;"><?php 
        _e('Which user categories can see comments?', 'pc_ml');
        ?>
</p>
		<div class="pc_tax_cat_list">
			<select name="pg_hide_comments[]" multiple="multiple" class="lcweb-chosen pc_pag_restr" data-placeholder="<?php 
        _e('Select categories', 'pc_ml');
        ?>
 .." autocomplete="off">
			  <option value="all" class="pc_all_field" <?php 
        if (isset($pc_hide_comments[0]) && $pc_hide_comments[0] == 'all') {
            echo 'selected="selected"';
        }
        ?>
><?php 
        _e('All', 'pc_ml');
        ?>
</option>
	
			  <?php 
        foreach ($user_categories as $ucat) {
            $selected = isset($pc_hide_comments[0]) && in_array($ucat->term_id, $pc_hide_comments) ? 'selected="selected"' : '';
            echo '<option value="' . $ucat->term_id . '" ' . $selected . '>' . $ucat->name . '</option>';
        }
        ?>
			</select> 
			
			<div id="pc_hc_use_warning" <?php 
        if (empty($pc_hide_comments)) {
            echo 'style="display: none;"';
        }
        ?>
>
				<p style="margin-bottom: 7px;"><?php 
        _e('Display warning box?', 'pc_ml');
        ?>
</p>
			
				<select name="pg_hc_use_warning" class="lcweb-chosen" data-placeholder="<?php 
        _e('Select an option', 'pc_ml');
        ?>
 .." style="width: 254px;" autocomplete="off">
				  <option value="default" <?php 
        selected($pc_hc_warn, 'default');
        ?>
><?php 
        _e('as default', 'pc_ml');
        ?>
</option>
				  <option value="yes" <?php 
        selected($pc_hc_warn, 'yes');
        ?>
><?php 
        _e('yes', 'pc_ml');
        ?>
</option>
				  <option value="no" <?php 
        selected($pc_hc_warn, 'no');
        ?>
><?php 
        _e('no', 'pc_ml');
        ?>
</option>
				</select>
			</div>   
		</div>    
    <?php 
    }
    ?>
    
    <?php 
    // create a custom nonce for submit verification later
    echo '<input type="hidden" name="pc_redirect_noncename" value="' . wp_create_nonce(__FILE__) . '" />';
    ?>
    
	<script src="<?php 
    echo PC_URL;
    ?>
/js/chosen/chosen.jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript" charset="utf8">
	jQuery(document).ready(function($) {
		
		// all/unlogged toggles
		jQuery('body').delegate('.pc_tax_cat_list select', 'change', function() {
			var pc_sel = jQuery(this).val();
			if(!pc_sel) {pc_sel = jQuery.makeArray();}
			
			// if ALL is selected, discard the rest
			if(jQuery.inArray("all", pc_sel) >= 0) {
				jQuery(this).children('option').prop('selected', false);
				jQuery(this).children('.pc_all_field').prop('selected', true);
				
				jQuery(this).trigger("chosen:updated");
			}
			
			// if UNLOGGED is selected, discard the rest
			else if(jQuery.inArray("unlogged", pc_sel) >= 0) {
				jQuery(this).children('option').prop('selected', false);
				jQuery(this).children('.pc_unl_field').prop('selected', true);
				
				jQuery(this).trigger("chosen:updated");
				var unlogged_chosen = true;
			}	

			// unloggged redirect toggle
			if(jQuery(this).attr('name') == 'pg_redirect[]') {
				if(typeof(unlogged_chosen) != 'undefined') {
					jQuery('#pc_unl_redir_wrap').slideDown();	
				} else {
					jQuery('#pc_unl_redir_wrap').slideUp();	
				}
			}
			
			// hidden comments warning
			if(jQuery(this).attr('name') == 'pg_hide_comments[]') {
				if(typeof(pc_sel[0]) != 'undefined') {
					jQuery('#pc_hc_use_warning').slideDown();	
				} else {
					jQuery('#pc_hc_use_warning').slideUp();	
				}
			}
		});
		
		// chosen
		jQuery('.lcweb-chosen').each(function() {
			var w = jQuery(this).css('width');
			jQuery(this).chosen({width: w}); 
		});
		jQuery(".lcweb-chosen-deselect").chosen({allow_single_deselect:true});
	});
	</script>
    
    <?php 
}