Advertisement
Guest User

wordpress-access-control-v3.1.4-forked.php

a guest
Jun 17th, 2013
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 33.58 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Plugin Name: WordPress Access Control
  4.  * Plugin URI: http://brandonwamboldt.ca/plugins/members-only-menu-plugin/
  5.  * Author: Brandon Wamboldt
  6.  * Author URI: http://brandonwamboldt.ca/
  7.  * Version: 3.1.4
  8.  * Description: This plugin is a powerful tool which gives you fine grained control over your pages and posts (and custom post types), allowing you to restrict a page, post, or custom post type to members, non-members, or even specific roles. You can customize how these pages and posts show up in search results, where users are directed when they visit them, and much more. <strong>You can even make your entire blog members only!</strong>.
  9.  */
  10.  
  11. add_action( 'after_setup_theme', array( 'WordPressAccessControl', 'load_custom_widgets' ) );
  12. add_action( 'wp', array( 'WordPressAccessControl', 'check_for_members_only' ) );
  13. add_action( 'wp', array( 'WordPressAccessControl', 'check_for_nonmembers_only' ) );
  14. add_action( 'init', array( 'WordPressAccessControl', 'add_wpac_nav_menus' ) );
  15. add_action( 'admin_init', array( 'WordPressAccessControl', 'admin_init' ) );
  16. add_action( 'admin_menu', array( 'WordPressAccessControl', 'admin_menu' ) );
  17. add_action( 'add_meta_boxes', array( 'WordPressAccessControl', 'add_wp_access_meta_boxes' ) );
  18. add_action( 'save_post', array( 'WordPressAccessControl', 'save_postdata' ) );
  19. add_action( 'manage_pages_custom_column', array( 'WordPressAccessControl', 'show_column' ) );
  20.  
  21. add_filter( 'get_pages', array( 'WordPressAccessControl', 'get_pages' ) );
  22. add_filter( 'manage_edit-page_columns', array( 'WordPressAccessControl', 'add_column' ) );
  23. add_filter( 'the_excerpt', array( 'WordPressAccessControl', 'remove_excerpt' ) );
  24. add_filter( 'the_content', array( 'WordPressAccessControl', 'remove_excerpt' ) );
  25. add_filter( 'plugin_row_meta', array( 'WordPressAccessControl', 'plugin_row_meta' ), 10, 5 );
  26. add_filter( 'posts_join_paged', array( 'WordPressAccessControl', 'posts_join_paged' ), 10, 2 );
  27. add_filter( 'posts_where_paged', array( 'WordPressAccessControl', 'posts_where_paged' ), 10, 2 );
  28. add_filter( 'wp_nav_menu_args', array( 'WordPressAccessControl', 'wp_nav_menu_members_only' ) );
  29.  
  30. add_shortcode( 'member', array( 'WordPressAccessControl', 'shortcode_members' ) );
  31. add_shortcode( 'members', array( 'WordPressAccessControl', 'shortcode_members' ) );
  32. add_shortcode( 'non-members', array( 'WordPressAccessControl', 'shortcode_nonmembers' ) );
  33. add_shortcode( 'nonmembers', array( 'WordPressAccessControl', 'shortcode_nonmembers' ) );
  34. add_shortcode( 'non-member', array( 'WordPressAccessControl', 'shortcode_nonmembers' ) );
  35. add_shortcode( 'nonmember', array( 'WordPressAccessControl', 'shortcode_nonmembers' ) );
  36.  
  37. // We check to see if the class exists because it isn't part of pre WordPress 3 systems
  38. /*if ( class_exists( 'Walker_Nav_Menu' ) ) {
  39.     add_filter( 'wp_nav_menu_args', array( 'WordPressAccessControl', 'wp_nav_menu_args' ) );
  40.  
  41.     /**
  42.      * A custom walker that checks our conditions to make sure the user has
  43.      * permission to view the page linked to from the menu item
  44.      *
  45.      * @author Brandon Wamboldt
  46.      * @package WordPress
  47.      * @subpackage WordPressAccessControl
  48.      *------------BREAK THIS TO UNCOMMENT--------------/
  49.     class WPAC_Nav_Menu_Walker extends Walker_Nav_Menu
  50.     {
  51.         var $in_private = false;
  52.         var $private_lvl = 0;
  53.  
  54.         function start_lvl( & $output, $depth )
  55.         {
  56.             if ( ! $this->in_private ) {
  57.                 $indent = str_repeat( '    ', $depth );
  58.                 $output .= "\n$indent<ul class=\"sub-menu\"><li style='display:none;'></li>\n";
  59.             }
  60.         }
  61.  
  62.         function end_lvl( & $output, $depth )
  63.         {
  64.             if ( ! $this->in_private ) {
  65.                 $indent = str_repeat( '    ', $depth );
  66.                 $output .= "$indent</ul>\n";
  67.             }
  68.         }
  69.  
  70.         function start_el( & $output, $item, $depth, $args )
  71.         {
  72.             global $wp_query;
  73.  
  74.             if ( WordPressAccessControl::check_conditions( $item->object_id ) || get_option( 'wpac_show_in_menus', 'with_access' ) == 'always' ) {
  75.                 if ( ! $this->in_private ) {
  76.                     $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  77.  
  78.                     $class_names = $value = '';
  79.  
  80.                     $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  81.  
  82.                     $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
  83.                     $class_names = ' class="' . esc_attr( $class_names ) . '"';
  84.  
  85.                     $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
  86.  
  87.                     $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
  88.                     $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
  89.                     $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
  90.                     $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';
  91.  
  92.                     $item_output = $args->before;
  93.                     $item_output .= '<a'. $attributes .'>';
  94.                     $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
  95.                     $item_output .= '</a>';
  96.                     $item_output .= $args->after;
  97.  
  98.                     $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  99.                 }
  100.             } else {
  101.                 $this->in_private = true;
  102.                 $this->private_lvl++;
  103.             }
  104.         }
  105.  
  106.         function end_el( & $output, $item, $depth )
  107.         {
  108.             if ( WordPressAccessControl::check_conditions( $item->object_id ) || get_option( 'wpac_show_in_menus', 'with_access' ) == 'always' ) {
  109.                 if ( ! $this->in_private ) {
  110.                     $output .= "</li>\n";
  111.                 }
  112.             } else {
  113.                 $this->private_lvl--;
  114.  
  115.                 if ( $this->private_lvl == 0 ) {
  116.                     $this->in_private = false;
  117.                 }
  118.             }
  119.         }
  120.     }
  121. }*/
  122.  
  123. // We check to see if the class exists because it may get removed in the future
  124. if ( class_exists( 'Walker_Page' ) ) {
  125.     add_filter( 'wp_page_menu_args', array( 'WordPressAccessControl' , 'wp_page_menu_args' ) );
  126.  
  127.     /**
  128.      * A custom walker that checks our conditions to make sure the user has
  129.      * permission to view the page linked to from the menu item
  130.      *
  131.      * @author Brandon Wamboldt
  132.      * @package WordPress
  133.      * @subpackage WordPressAccessControl
  134.      */
  135.     class WPAC_Page_Walker extends Walker_Page
  136.     {
  137.         var $in_private = false;
  138.         var $private_lvl = 0;
  139.  
  140.         function start_lvl( & $output, $depth )
  141.         {
  142.             if ( ! $this->in_private ) {
  143.                 $indent = str_repeat( '    ', $depth );
  144.                 $output .= "\n$indent<ul class=\"sub-menu\"><li style='display:none;'></li>\n";
  145.             }
  146.         }
  147.  
  148.         function end_lvl( & $output, $depth )
  149.         {
  150.             if ( ! $this->in_private ) {
  151.                 $indent = str_repeat( '    ', $depth );
  152.                 $output .= "$indent</ul>\n";
  153.             }
  154.         }
  155.  
  156.         function start_el( & $output, $page, $depth, $args, $current_page )
  157.         {
  158.             global $wp_query;
  159.  
  160.             if ( WordPressAccessControl::check_conditions( $page->ID ) ) {
  161.                 if ( ! $this->in_private ) {
  162.                     $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  163.  
  164.                     extract( $args, EXTR_SKIP );
  165.  
  166.                     $css_class = array( 'page_item', 'page-item-' . $page->ID );
  167.  
  168.                     if ( $current_page != '' ) {
  169.                         $_current_page = get_page( $current_page );
  170.  
  171.                         if ( isset( $_current_page->ancestors ) && in_array( $page->ID, (array) $_current_page->ancestors ) ) {
  172.                             $css_class[] = 'current_page_ancestor';
  173.                         }
  174.  
  175.                         if ( $page->ID == $current_page ) {
  176.                             $css_class[] = 'current_page_item';
  177.                         } elseif ( $_current_page && $page->ID == $_current_page->post_parent ) {
  178.                             $css_class[] = 'current_page_parent';
  179.                         }
  180.                     } elseif ( $page->ID == get_option('page_for_posts') ) {
  181.                         $css_class[] = 'current_page_parent';
  182.                     }
  183.  
  184.                     $css_class = implode( ' ', apply_filters( 'page_css_class', $css_class, $page ) );
  185.  
  186.                     $output .= $indent . '<li class="' . $css_class . '"><a href="' . get_page_link( $page->ID ) . '" title="' . esc_attr( wp_strip_all_tags( apply_filters( 'the_title', $page->post_title, $page->ID ) ) ) . '">' . $link_before . apply_filters( 'the_title', $page->post_title, $page->ID ) . $link_after . '</a>';
  187.  
  188.                     if ( ! empty( $show_date ) ) {
  189.                         if ( 'modified' == $show_date ) {
  190.                             $time = $page->post_modified;
  191.                         } else {
  192.                             $time = $page->post_date;
  193.                         }
  194.  
  195.                         $output .= ' ' . mysql2date( $date_format, $time );
  196.                     }
  197.                 }
  198.             } else {
  199.                 $this->in_private = true;
  200.                 $this->private_lvl++;
  201.             }
  202.         }
  203.  
  204.         function end_el( & $output, $page, $depth )
  205.         {
  206.             if ( WordPressAccessControl::check_conditions( $page->ID ) ) {
  207.                 if ( ! $this->in_private ) {
  208.                     $output .= "</li>\n";
  209.                 }
  210.             } else {
  211.                 $this->private_lvl--;
  212.  
  213.                 if ( $this->private_lvl == 0 ) {
  214.                     $this->in_private = false;
  215.                 }
  216.             }
  217.         }
  218.     }
  219. }
  220.  
  221. /**
  222.  * The main plugin
  223.  *
  224.  * @author Brandon Wamboldt
  225.  * @package WordPress
  226.  * @subpackage WordPressAccessControl
  227.  */
  228. class WordPressAccessControl
  229. {
  230.     function admin_init()
  231.     {
  232.         wp_enqueue_style( 'wpac-style', plugin_dir_url( __FILE__ ) . 'public/css/wpac.css' );
  233.  
  234.         // Load translation files
  235.         if ( ! load_plugin_textdomain( 'wpac' ) ) {
  236.             load_plugin_textdomain( 'wpac', false, 'wordpress-access-control/languages/' );
  237.         }
  238.     }
  239.  
  240.     function admin_menu()
  241.     {
  242.         add_options_page( 'WordPress Access Control Settings', 'Members Only', 'manage_options', 'wpac-options', array( 'WordPressAccessControl', 'options_page' ) );
  243.     }
  244.  
  245.     function options_page()
  246.     {
  247.         // Save options?
  248.         if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'update' ) {
  249.             if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'wpac_options_save' ) ) {
  250.                 $admin_error = __( 'Security check failed, please try again', 'wpac' );
  251.             } else {
  252.  
  253.                 // Make the blog members only
  254.                 if ( isset( $_REQUEST['wpac_members_only_blog'] ) && $_REQUEST['wpac_members_only_blog'] == 'yes' ) {
  255.                     update_option( 'wpac_members_only_blog', true );
  256.                 } else {
  257.                     update_option( 'wpac_members_only_blog', false );
  258.                 }
  259.  
  260.                 // Members blog redirect link
  261.                 if ( isset( $_REQUEST['wpac_members_blog_redirect'] ) ) {
  262.                     update_option( 'wpac_members_blog_redirect', esc_url( $_REQUEST['wpac_members_blog_redirect'] ) );
  263.                 } else {
  264.                     delete_option( 'wpac_members_blog_redirect' );
  265.                 }
  266.  
  267.                 // Support custom post types
  268.                 if ( isset( $_REQUEST['wpac_custom_post_types'] ) && is_array( $_REQUEST['wpac_custom_post_types'] ) ) {
  269.                     update_option( 'wpac_custom_post_types', $_REQUEST['wpac_custom_post_types'] );
  270.                 } else {
  271.                     delete_option( 'wpac_custom_post_types' );
  272.                 }
  273.  
  274.                 // Override per-page permissions
  275.                 if ( isset( $_REQUEST['wpac_always_accessible_by'] ) && is_array( $_REQUEST['wpac_always_accessible_by'] ) ) {
  276.                     $wpac_always_accessible_by = array();
  277.  
  278.                     foreach ( $_REQUEST['wpac_always_accessible_by'] as $role ) {
  279.                         $wpac_always_accessible_by[] = $role;
  280.                     }
  281.  
  282.                     update_option( 'wpac_always_accessible_by', $wpac_always_accessible_by );
  283.                 } else {
  284.                     update_option( 'wpac_always_accessible_by', array() );
  285.                 }
  286.  
  287.                 // Show in the menu settings
  288.                 if ( isset( $_REQUEST['wpac_show_in_menus'] ) && $_REQUEST['wpac_show_in_menus'] == 'always' ) {
  289.                     update_option( 'wpac_show_in_menus', 'always' );
  290.                 } else {
  291.                     update_option( 'wpac_show_in_menus', 'with_access' );
  292.                 }
  293.  
  294.                 // Default post state setting
  295.                 if ( isset( $_REQUEST['default_post_state'] ) && in_array( $_REQUEST['default_post_state'], array( 'public', 'members', 'nonmembers' ) ) ) {
  296.                     update_option( 'wpac_default_post_state', $_REQUEST['default_post_state'] );
  297.                 } else {
  298.                     update_option( 'wpac_default_post_state', 'public' );
  299.                 }
  300.  
  301.                 // Default post roles
  302.                 if ( isset( $_REQUEST['wpac_posts_default_restricted_to'] ) && is_array( $_REQUEST['wpac_posts_default_restricted_to'] ) ) {
  303.                     update_option( 'wpac_posts_default_restricted_to', $_REQUEST['wpac_posts_default_restricted_to'] );
  304.                 } else {
  305.                     update_option( 'wpac_posts_default_restricted_to', array() );
  306.                 }
  307.  
  308.                 // Default page state setting
  309.                 if ( isset( $_REQUEST['default_page_state'] ) && in_array( $_REQUEST['default_page_state'], array( 'public', 'members', 'nonmembers' ) ) ) {
  310.                     update_option( 'wpac_default_page_state', $_REQUEST['default_page_state'] );
  311.                 } else {
  312.                     update_option( 'wpac_default_page_state', 'public' );
  313.                 }
  314.  
  315.                 // Default page roles
  316.                 if ( isset( $_REQUEST['wpac_pages_default_restricted_to'] ) && is_array( $_REQUEST['wpac_pages_default_restricted_to'] ) ) {
  317.                     update_option( 'wpac_pages_default_restricted_to', $_REQUEST['wpac_pages_default_restricted_to'] );
  318.                 } else {
  319.                     update_option( 'wpac_pages_default_restricted_to', array() );
  320.                 }
  321.  
  322.                 // Default redirect
  323.                 if ( isset( $_REQUEST['wpac_default_members_redirect'] ) && ! empty( $_REQUEST['wpac_default_members_redirect'] ) ) {
  324.                     update_option( 'wpac_default_members_redirect', $_REQUEST['wpac_default_members_redirect'] );
  325.                 } else {
  326.                     update_option( 'wpac_default_members_redirect', '' );
  327.                 }
  328.  
  329.                 // Search Options
  330.                 if ( isset( $_REQUEST['show_posts_in_search'] ) && $_REQUEST['show_posts_in_search'] == 'yes' ) {
  331.                     update_option( 'wpac_show_posts_in_search', true );
  332.                 } else {
  333.                     update_option( 'wpac_show_posts_in_search', false );
  334.                 }
  335.  
  336.                 if ( isset( $_REQUEST['show_post_excerpts_in_search'] ) && $_REQUEST['show_post_excerpts_in_search'] == 'yes' ) {
  337.                     update_option( 'wpac_show_post_excerpts_in_search', true );
  338.                 } else {
  339.                     update_option( 'wpac_show_post_excerpts_in_search', false );
  340.                 }
  341.  
  342.                 if ( isset( $_REQUEST['show_pages_in_search'] ) && $_REQUEST['show_pages_in_search'] == 'yes' ) {
  343.                     update_option( 'wpac_show_pages_in_search', true );
  344.                 } else {
  345.                     update_option( 'wpac_show_pages_in_search', false );
  346.                 }
  347.  
  348.                 if ( isset( $_REQUEST['show_page_excerpts_in_search'] ) && $_REQUEST['show_page_excerpts_in_search'] == 'yes' ) {
  349.                     update_option( 'wpac_show_page_excerpts_in_search', true );
  350.                 } else {
  351.                     update_option( 'wpac_show_page_excerpts_in_search', false );
  352.                 }
  353.  
  354.                 // Custom excerpts
  355.                 if ( isset( $_REQUEST['page_excerpt_text'] ) ) {
  356.                     update_option( 'wpac_page_excerpt_text', $_REQUEST['page_excerpt_text'] );
  357.                 }
  358.  
  359.                 if ( isset( $_REQUEST['post_excerpt_text'] ) ) {
  360.                     update_option( 'wpac_post_excerpt_text', $_REQUEST['post_excerpt_text'] );
  361.                 }
  362.  
  363.                 $admin_message = '<strong>' . __( 'Settings Saved.') . '</strong>';
  364.             }
  365.         }
  366.  
  367.         include( dirname( __FILE__ ) . '/templates/options.php' );
  368.     }
  369.  
  370.     function add_column( $columns )
  371.     {
  372.         $columns['wpac'] = '<img src="' . plugin_dir_url( __FILE__ ) . 'public/images/lock.png" alt="Access" />';
  373.         return $columns;
  374.     }
  375.  
  376.     function show_column( $name )
  377.     {
  378.         global $post;
  379.  
  380.         switch ( $name ) {
  381.             case 'wpac':
  382.                 if ( get_post_meta( $post->ID, '_wpac_is_members_only', true ) ) {
  383.                     echo '<img src="' . plugin_dir_url( __FILE__ ) . 'public/images/lock.png" alt="Members Only" title="Members Only" />';
  384.                 } else if ( get_post_meta( $post->ID, '_wpac_is_nonmembers_only', true ) ) {
  385.                     echo '<img src="' . plugin_dir_url( __FILE__ ) . 'public/images/unlock.png" alt="Non-members Only" title="Non-members Only" />';
  386.                 }
  387.  
  388.                 break;
  389.         }
  390.     }
  391.  
  392.     function check_conditions( $page_id )
  393.     {
  394.         global $wp_roles, $current_user;
  395.  
  396.         if ( is_user_logged_in() ) {
  397.             get_currentuserinfo();
  398.  
  399.             $allowed_roles  = maybe_unserialize( get_post_meta( $page_id, '_wpac_restricted_to', true ) );
  400.             $override_roles = maybe_unserialize( get_option( 'wpac_always_accessible_by', array( 0 => 'administrator' ) ) );
  401.  
  402.             if ( empty( $allowed_roles ) ) {
  403.                 $allowed_roles_tmp = $wp_roles->get_names();
  404.                 $allowed_roles = array();
  405.  
  406.                 foreach ( $allowed_roles_tmp as $role => $garbage ) {
  407.                     $allowed_roles[] = $role;
  408.                 }
  409.             }
  410.  
  411.             $intersections = array_intersect( $current_user->roles, $allowed_roles );
  412.             $override_intersections = array_intersect( $current_user->roles, $override_roles );
  413.  
  414.             if ( empty( $intersections ) && empty( $override_intersections ) ) {
  415.                 $role = false;
  416.             } else {
  417.                 $role = true;
  418.             }
  419.         } else {
  420.             $role = true;
  421.         }
  422.  
  423.         $is_members_only = get_post_meta( $page_id, '_wpac_is_members_only', true );
  424.         $is_nonmembers_only = get_post_meta( $page_id, '_wpac_is_nonmembers_only', true );
  425.  
  426.         if ( ( is_user_logged_in() && $is_members_only && $role ) || ( ! is_user_logged_in() && $is_nonmembers_only ) || ( ! $is_members_only && ! $is_nonmembers_only ) ) {
  427.             return true;
  428.         } else {
  429.             return false;
  430.         }
  431.     }
  432.  
  433.     function check_for_members_only()
  434.     {
  435.         global $post;
  436.  
  437.         if ( is_admin() ) return;
  438.  
  439.         // Check for a members only blog
  440.         $blog_is_members_only = get_option( 'wpac_members_only_blog', false );
  441.  
  442.         if ( $blog_is_members_only && ! is_user_logged_in() ) {
  443.             $redirect_to = get_option( 'wpac_members_blog_redirect', wp_login_url( $_SERVER['REQUEST_URI'] ) );
  444.  
  445.             if ( empty( $redirect_to ) ) {
  446.                 $redirect_to = wp_login_url( $_SERVER['REQUEST_URI'] );
  447.             }
  448.  
  449.             header( 'Location: ' . add_query_arg( 'redirect_to', $_SERVER['REQUEST_URI'], $redirect_to ) );
  450.             exit();
  451.         }
  452.  
  453.         if ( get_post_meta( $post->ID, '_wpac_is_members_only', true ) && ! WordPressAccessControl::check_conditions( $post->ID ) ) {
  454.             if ( is_singular() ) {
  455.                 $redirect_to = get_post_meta( $post->ID, '_wpac_members_redirect_to', true );
  456.  
  457.                 if ( empty( $redirect_to ) ) {
  458.                     header( 'Location: ' . get_bloginfo( 'wpurl' ) . '/wp-login.php?redirect_to=' . $_SERVER['REQUEST_URI'] );
  459.                 } else {
  460.                     header( 'Location: ' . add_query_arg( 'redirect_to', $_SERVER['REQUEST_URI'], $redirect_to ) );
  461.                 }
  462.  
  463.                 exit();
  464.             }
  465.         }
  466.     }
  467.  
  468.     function check_for_nonmembers_only()
  469.     {
  470.         if ( is_admin() ) return;
  471.  
  472.         global $post;
  473.  
  474.         if ( get_post_meta( $post->ID, '_wpac_is_nonmembers_only', true ) && ! WordPressAccessControl::check_conditions( $post->ID ) ) {
  475.             $redirect_to = get_post_meta( $post->ID, '_wpac_nonmembers_redirect_to', true );
  476.  
  477.             if ( empty( $redirect_to ) ) {
  478.                 header( 'Location: ' . get_bloginfo( 'wpurl' ) . '/index.php' );
  479.             } else {
  480.                 header( 'Location: ' . $redirect_to );
  481.             }
  482.  
  483.             exit();
  484.         }
  485.     }
  486.  
  487.     function remove_excerpt( $excerpt )
  488.     {
  489.         if ( is_admin() || is_single() ) return $excerpt;
  490.  
  491.         global $post;
  492.  
  493.         if ( get_post_meta( $post->ID, '_wpac_is_members_only', true ) && ! WordPressAccessControl::check_conditions( $post->ID ) ) {
  494.             $show_excerpt = get_post_meta( $post->ID, '_wpac_show_excerpt_in_search', true );
  495.  
  496.             if ( ! is_singular() && $show_excerpt != 'yes' ) {
  497.                 if ( $post->post_type == 'post' ) {
  498.                     $excerpt = get_option( 'wpac_post_excerpt_text', __( 'To view the contents of this post, you must be authenticated and have the required access level.', 'wpac' ) );
  499.                 } else {
  500.                     $excerpt = get_option( 'wpac_page_excerpt_text', __( 'To view the contents of this page, you must be authenticated and have the required access level.', 'wpac' ) );
  501.                 }
  502.  
  503.                 return wpautop( $excerpt );
  504.             } else {
  505.                 return $excerpt;
  506.             }
  507.         }
  508.  
  509.         return $excerpt;
  510.     }
  511.  
  512.     function posts_join_paged( $join, $query )
  513.     {
  514.         global $wpdb;
  515.  
  516.         if ( is_admin() ) return $join;
  517.  
  518.         if ( ! is_singular() && ! is_admin() ) {
  519.             $join .= " LEFT JOIN {$wpdb->postmeta} PMWPAC3 ON PMWPAC3.post_id = {$wpdb->posts}.ID AND PMWPAC3.meta_key = '_wpac_show_in_search' LEFT JOIN {$wpdb->postmeta} PMWPAC ON PMWPAC.post_id = {$wpdb->posts}.ID AND PMWPAC.meta_key = '_wpac_is_members_only' LEFT JOIN {$wpdb->postmeta} PMWPAC2 ON PMWPAC2.post_id = {$wpdb->posts}.ID AND PMWPAC2.meta_key = '_wpac_is_nonmembers_only' ";
  520.         }
  521.  
  522.         return $join;
  523.     }
  524.  
  525.     function posts_where_paged( $where, $query )
  526.     {
  527.         global $wpdb;
  528.  
  529.         if ( is_admin() ) return $where;
  530.  
  531.         if ( ! is_singular() && ! is_admin() ) {
  532.             if ( ! is_user_logged_in() ) {
  533.                 $where .= " AND ( PMWPAC.meta_value IS NULL OR PMWPAC3.meta_value = '1' OR PMWPAC.meta_value != 'true' ) ";
  534.             } else {
  535.                 $where .= " AND ( PMWPAC2.meta_value IS NULL OR PMWPAC2.meta_value != 'true' ) ";
  536.             }
  537.         }
  538.  
  539.         return $where;
  540.     }
  541.  
  542.     function remove_restricted_posts( $posts, $query = false )
  543.     {
  544.         return $posts;
  545.         echo $query->request;exit();
  546.  
  547.         if ( ! is_singular() && ! is_admin() ) {
  548.             $new_posts = array();
  549.  
  550.             foreach ( $posts as $post ) {
  551.                 // The page is members only and we do NOT have access to it
  552.                 if ( get_post_meta( $post->ID, '_wpac_is_members_only', true ) && ! WordPressAccessControl::check_conditions( $post->ID ) ) {
  553.                     if ( get_post_meta( $post->ID, '_wpac_show_in_search', true ) != 1 ) {
  554.                         continue;
  555.                     }
  556.                 }
  557.  
  558.                 // The page is non-members only and we do NOT have access to it
  559.                 if ( get_post_meta( $post->ID, '_wpac_is_nonmembers_only', true ) && ! WordPressAccessControl::check_conditions( $post->ID ) ) {
  560.                     if ( get_post_meta( $post->ID, '_wpac_show_in_search', true ) != 1 ) {
  561.                         continue;
  562.                     }
  563.                 }
  564.  
  565.                 // Remove results where the search term is in a [member] or [nonmember] tag and we don't have access
  566.                 $st = get_search_query();
  567.                 $content = do_shortcode( $post->post_content );
  568.  
  569.  
  570.                 if ( ! empty( $st ) && is_search() && ! stristr( $content, $st ) ) {
  571.                     continue;
  572.                 }
  573.  
  574.                 $new_posts[] = $post;
  575.             }
  576.  
  577.             $query->found_posts   = count( $new_posts );
  578.             $query->max_num_pages = ceil( count( $new_posts ) / $query->query_vars['posts_per_page'] );
  579.             $query->posts         = $new_posts;
  580.             $query->post_count    = count( $new_posts );
  581.  
  582.             return $new_posts;
  583.         }
  584.  
  585.         return $posts;
  586.     }
  587.  
  588.     /**
  589.      * Add our custom meta box to all support post types
  590.      *
  591.      * @since WordPress Access Control 2.0
  592.      * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
  593.      */
  594.     function add_wp_access_meta_boxes()
  595.     {
  596.         $enabled_post_types = get_option( 'wpac_custom_post_types', array() );
  597.         $supported_pages = apply_filters( 'wp_access_supported_pages', array_merge( array( 'page', 'post' ), $enabled_post_types ) );
  598.  
  599.         foreach ( $supported_pages as $page_type ) {
  600.             add_meta_box( 'wpac_controls_meta', 'WordPress Access Control', array( 'WordPressAccessControl', 'add_wpac_meta_box' ), $page_type, 'side', 'high' );
  601.         }
  602.     }
  603.  
  604.     /**
  605.      * Display the various per-page/per-post options in the sidebar
  606.      *
  607.      * @since WordPress Access Control 2.0
  608.      * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
  609.      */
  610.     function add_wpac_meta_box()
  611.     {
  612.         global $post;
  613.  
  614.         $is_members_only = get_post_meta( $post->ID, '_wpac_is_members_only', true );
  615.         $is_nonmembers_only = get_post_meta( $post->ID, '_wpac_is_nonmembers_only', true );
  616.  
  617.         if ( $post->post_type == 'post' ) {
  618.             $wpac_default_post_state = get_option( 'wpac_default_post_state', 'public' );
  619.         } else {
  620.             $wpac_default_post_state = get_option( 'wpac_default_page_state', 'public' );
  621.         }
  622.  
  623.         if ( $is_members_only == 'true' || ( $post->post_status == 'auto-draft' && $wpac_default_post_state == 'members' ) ) {
  624.             $is_members_only = 'checked="checked"';
  625.         }
  626.  
  627.         if ( $is_nonmembers_only == 'true' || ( $post->post_status == 'auto-draft' && $wpac_default_post_state == 'nonmembers' ) ) {
  628.             $is_nonmembers_only = 'checked="checked"';
  629.         }
  630.  
  631.         $wpac_show_in_search = get_post_meta( $post->ID, '_wpac_show_in_search', true );
  632.         $wpac_show_excerpt_in_search = get_post_meta( $post->ID, '_wpac_show_excerpt_in_search', true );
  633.         $pass_to_children = get_post_meta( $post->ID, '_wpac_pass_to_children', true );
  634.  
  635.         include( dirname( __FILE__ ) . '/templates/meta_box.php' );
  636.     }
  637.  
  638.     /**
  639.      * Saves our custom meta data for the post being saved
  640.      *
  641.      * @since WordPress Access Control 2.0
  642.      * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
  643.      */
  644.     function save_postdata( $post_id )
  645.     {
  646.         // Security check
  647.         if ( ! isset( $_POST['members_only_nonce'] ) ) {
  648.             return $post_id;
  649.         }
  650.  
  651.         if ( ! wp_verify_nonce( $_POST['members_only_nonce'], 'members_only' ) ) {
  652.             return $post_id;
  653.         }
  654.  
  655.         // Meta data isn't transmitted during autosaves so don't do anything
  656.         if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  657.             return $post_id;
  658.         }
  659.  
  660.         if ( isset( $_POST['members_only'] ) && $_POST['members_only'] == 'true' ) {
  661.             update_post_meta( $post_id, '_wpac_is_members_only', 'true' );
  662.  
  663.             if ( isset( $_POST['wpac_restricted_to'] ) && ! empty( $_POST['wpac_restricted_to'] ) ) {
  664.                 update_post_meta( $post_id, '_wpac_restricted_to', serialize( $_POST['wpac_restricted_to'] ) );
  665.             } else {
  666.                 delete_post_meta( $post_id, '_wpac_restricted_to' );
  667.             }
  668.         } else {
  669.             delete_post_meta( $post_id, '_wpac_is_members_only' );
  670.         }
  671.  
  672.         if ( isset( $_POST['nonmembers_only'] ) && $_POST['nonmembers_only'] == 'true' ) {
  673.             update_post_meta( $post_id, '_wpac_is_nonmembers_only', 'true' );
  674.         } else {
  675.             delete_post_meta( $post_id, '_wpac_is_nonmembers_only' );
  676.         }
  677.  
  678.         if ( isset( $_POST['wpac_members_redirect_to'] ) ) {
  679.             update_post_meta( $post_id, '_wpac_members_redirect_to', $_POST['wpac_members_redirect_to'] );
  680.         }
  681.  
  682.         if ( isset( $_POST['wpac_show_in_search'] ) ) {
  683.             update_post_meta( $post_id, '_wpac_show_in_search', 1 );
  684.         } else {
  685.             update_post_meta( $post_id, '_wpac_show_in_search', 0 );
  686.         }
  687.  
  688.         if ( isset( $_POST['wpac_show_excerpt_in_search'] ) ) {
  689.             update_post_meta( $post_id, '_wpac_show_excerpt_in_search', 1 );
  690.         } else {
  691.             update_post_meta( $post_id, '_wpac_show_excerpt_in_search', 0 );
  692.         }
  693.  
  694.         if ( isset( $_POST['wpac_nonmembers_redirect_to'] ) ) {
  695.             update_post_meta( $post_id, '_wpac_nonmembers_redirect_to', $_POST['wpac_nonmembers_redirect_to'] );
  696.         }
  697.  
  698.         if ( isset( $_POST['pass_to_children'] ) && $_POST['pass_to_children'] == 'true' ) {
  699.             update_post_meta( $post_id, '_wpac_pass_to_children', 'true' );
  700.  
  701.             $original_id = $post_id;
  702.             $is_revision = wp_is_post_revision( $original_id );
  703.  
  704.             if ( $is_revision ) {
  705.                 $original_id = $is_revision;
  706.             }
  707.  
  708.             $children = get_pages( array( 'child_of' => $original_id ) );
  709.  
  710.             foreach ( $children as $child ) {
  711.                 WordPressAccessControl::save_postdata( $child->ID );
  712.             }
  713.         } else {
  714.             delete_post_meta( $post_id, '_wpac_pass_to_children' );
  715.         }
  716.  
  717.         return $post_id;
  718.     }
  719.  
  720.     /**
  721.      * Replaces the default WordPress nav menu walker with our own version
  722.      *
  723.      * @param array $args The arguments passed to the wp_nav_menu function
  724.      *
  725.      * @return array The original arguments, with our custom walker instead of the default
  726.      *
  727.      * @since WordPress Access Control 1.1
  728.      * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
  729.      */
  730.     function wp_nav_menu_args( $args )
  731.     {
  732.         $args['walker'] = new WPAC_Nav_Menu_Walker();
  733.         return $args;
  734.     }
  735.  
  736.     /**
  737.      * Prevents the code from degrading terribly when no nav menu is available
  738.      *
  739.      * @param array $args The arguments passed to the wp_nav_menu function
  740.      *
  741.      * @return array The original arguments, possible with our custom walker instead of the default
  742.      *
  743.      * @since WordPress Access Control 1.2
  744.      * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
  745.      */
  746.     function wp_page_menu_args( $args )
  747.     {
  748.         // Only remove the walker if it is ours
  749.         if ( isset( $args['walker'] ) && get_class( $args['walker']) == 'WPAC_Nav_Menu_Walker' ) {
  750.             $args['walker'] = new WPAC_Page_Walker();
  751.         }
  752.  
  753.         return $args;
  754.     }
  755.  
  756.     /**
  757.      * This hooks in at a higher level to make sure functions like wp_list_pages
  758.      * won't return members only pages.
  759.      *
  760.      * @param array $pages
  761.      * @return array
  762.      *
  763.      * @since WordPress Access Control 1.6.4
  764.      * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
  765.      */
  766.     function get_pages( $pages )
  767.     {
  768.         // Don't affect the display of pages when viewing the list in the admin
  769.         if ( is_admin() ) {
  770.             return $pages;
  771.         }
  772.  
  773.         // Whether or not the user is logged in
  774.         $auth = is_user_logged_in();
  775.  
  776.         // Go through each page, remove ones we can't see
  777.         foreach ( $pages as $key => $page ) {
  778.             $is_members_only = get_post_meta( $page->ID, '_wpac_is_members_only', true );
  779.             $is_nonmembers_only = get_post_meta( $page->ID, '_wpac_is_nonmembers_only', true );
  780.  
  781.             if ( $is_members_only && ! $auth ) {
  782.                 unset( $pages[$key] );
  783.             }
  784.  
  785.             if ( $is_nonmembers_only && $auth ) {
  786.                 unset( $pages[$key] );
  787.             }
  788.         }
  789.  
  790.         return $pages;
  791.     }
  792.  
  793.     /**
  794.      * Adds links to the plugin row to view the options page or view the plugin
  795.      * documentation.
  796.      *
  797.      * @param array $plugin_meta The links to display in the plugin row
  798.      * @param string $plugin_file The directory name and filename of the plugin (wordpress-access-control/wordpress-access-control.php)
  799.      * @param array $plugin_data
  800.      * @param string $status The status of the plugin (active or inactive)
  801.      *
  802.      * @return array The $plugin_meta array of links for this plugin
  803.      *
  804.      * @uses plugin_dir_url() to get the URL of the plugin directory since that is where the documentation is
  805.      *
  806.      * @since WordPress Access Control 3.0
  807.      * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
  808.      */
  809.     function plugin_row_meta( $plugin_meta, $plugin_file, $plugin_data, $status )
  810.     {
  811.         if ( $plugin_file == str_replace( '.php', '/', basename( __FILE__ ) ) . basename( __FILE__ ) ) {
  812.             $plugin_meta[] = '<a href="options-general.php?page=wpac-options">' . __( 'Visit options page', 'wpac' ) . '</a>';
  813.             $plugin_meta[] = '<a href="' . plugin_dir_url( __FILE__ ) . 'documentation/index.html">' . __( 'Plugin Documentation', 'wpac' ) . '</a>';
  814.         }
  815.  
  816.         return $plugin_meta;
  817.     }
  818.  
  819.  
  820.     /**
  821.      * Hides content for users that are not logged in and displays content for
  822.      * users who are logged in. Calls do_shortcode on the content to allow
  823.      * nested shortcodes.
  824.      *
  825.      * @uses is_user_logged_in() to determine if the user is logged in or not
  826.      * @uses wpautop() to format the text in the shortcodes
  827.      * @uses do_shortcode() to execute nested shortcodes in our shortcode
  828.      *
  829.      * @since WordPress Access Control 3.0
  830.      * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
  831.      */
  832.     function shortcode_members( $attributes, $content = NULL )
  833.     {
  834.         global $current_user, $post;
  835.  
  836.         if ( is_user_logged_in() ) {
  837.             if (isset($attributes['role'])) {
  838.  
  839.                 // Determine the syntax (Singular role, OR syntax or NOT syntax)
  840.                 if (substr($attributes['role'], 0, 3) == 'or:') {
  841.                     $roles = explode(',', substr($attributes['role'], 3));
  842.  
  843.                     $has_a_role = false;
  844.  
  845.                     foreach ($roles as $role) {
  846.                         if (in_array($role, $current_user->roles)) {
  847.                             $has_a_role = true;
  848.                             break;
  849.                         }
  850.                     }
  851.  
  852.                     if (!$has_a_role) {
  853.                         return '';
  854.                     }
  855.                 } else if (substr($attributes['role'], 0, 4) == 'not:') {
  856.                     $roles = explode(',', substr($attributes['role'], 4));
  857.  
  858.                     foreach ($roles as $role) {
  859.                         if (in_array($role, $current_user->roles)) {
  860.                             return '';
  861.                         }
  862.                     }
  863.                 } else {
  864.                     if (!in_array($attributes['role'], $current_user->roles)) {
  865.                         return '';
  866.                     }
  867.                 }
  868.             }
  869.  
  870.             return wpautop( do_shortcode( $content ) );
  871.         }
  872.  
  873.         return '';
  874.     }
  875.  
  876.     /**
  877.      * Hides content for users that are logged in and displays content for users
  878.      * who are not logged in. Calls do_shortcode on the content to allow nested
  879.      * shortcodes.
  880.      *
  881.      * @uses is_user_logged_in() to determine if the user is logged in or not
  882.      * @uses wpautop() to format the text in the shortcodes
  883.      * @uses do_shortcode() to execute nested shortcodes in our shortcode
  884.      *
  885.      * @since WordPress Access Control 3.0
  886.      * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
  887.      */
  888.     function shortcode_nonmembers( $attributes, $content = NULL )
  889.     {
  890.         global $post;
  891.  
  892.         if ( ! is_user_logged_in() ) {
  893.             return wpautop( do_shortcode( $content ) );
  894.         }
  895.  
  896.         return '';
  897.     }
  898.  
  899.     /*
  900.      * Loads a file that will override certain WordPress default widgets with
  901.      * a slightly modified version that allows admins to select it's visibility
  902.      * (Members only or non members only).
  903.      *
  904.      * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
  905.      * @since WordPress Access Control 3.1
  906.      */
  907.     function load_custom_widgets()
  908.     {
  909.         require( dirname( __FILE__ ) . '/default-widgets.php' );
  910.     }
  911.  
  912.     /**
  913.      * Adds our member only menus for each regular menu
  914.      *
  915.      * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
  916.      * @since WordPress Access Control 3.1
  917.      */
  918.     function add_wpac_nav_menus()
  919.     {
  920.         global $_wp_registered_nav_menus;
  921.  
  922.         $original = (array) $_wp_registered_nav_menus;
  923.  
  924.         foreach ( $original as $location => $label ) {
  925.             $_wp_registered_nav_menus[$location . '_wpac'] = $label . ' - Members Only';
  926.         }
  927.     }
  928.  
  929.     /**
  930.      * Checks to see if a members only version of the current menu is available
  931.      * and will load that instead of the normal menu.
  932.      *
  933.      * @param array $args The arguments passed to the wp_nav_menu function
  934.      *
  935.      * @return array The original arguments, possible with a new value for theme_location
  936.      *
  937.      * @uses is_user_logged_in() to determine if the user is logged in
  938.      * @uses get_nav_menu_locations() to get a list of valid theme locations
  939.      * @uses wp_get_nav_menu_object() to get our custom members only menu
  940.      *
  941.      * @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
  942.      * @since WordPress Access Control 3.1
  943.      */
  944.     function wp_nav_menu_members_only( $args )
  945.     {
  946.         // Don't do anything if the user isn't logged in
  947.         if ( is_user_logged_in() ) {
  948.  
  949.             // See if the theme even passed a proper menu ID
  950.             if ( ! empty( $args['theme_location'] ) ) {
  951.  
  952.                 // Member only menus end in _wpac
  953.                 $theme_location = $args['theme_location'] . '_wpac';
  954.  
  955.                 // Get the nav menu based on the theme_location
  956.                 if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $theme_location ] ) ) {
  957.                     $menu = wp_get_nav_menu_object( $locations[ $theme_location ] );
  958.  
  959.                     // Only use the member only menu if it's not empty
  960.                     if ( $menu && $menu->count > 0 ) {
  961.                         $args['theme_location'] = $theme_location;
  962.                     }
  963.                 }
  964.             }
  965.         }
  966.  
  967.         return $args;
  968.     }
  969. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement