Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 21.50 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Access Category Password
  4. Text Domain: access-category-password
  5. Plugin URI: https://wordpress.org/plugins/access-category-password/
  6. Description: Protects posts in categories by setting a unique Password for all restricted categories.
  7. Author: Jojaba
  8. Version: 1.4.1
  9. Author URI: http://perso.jojaba.fr/
  10. */
  11.  
  12. /**
  13.  * Language init
  14.  */
  15. function acpwd_lang_init() {
  16.  load_plugin_textdomain( 'access-category-password', false, basename(dirname(__FILE__)) );
  17. }
  18. add_action('plugins_loaded', 'acpwd_lang_init');
  19.  
  20. /* ******************************* */
  21. /* Backend of the plugin (options) */
  22. /* ******************************* */
  23.  
  24. add_action( 'admin_menu', 'acpwd_options_add_page' );
  25. /**
  26.  * Load up the options page
  27.  */
  28. if( !function_exists('acpwd_options_add_page'))  {
  29.     function acpwd_options_add_page() {
  30.         add_options_page(
  31.             __( 'Access Category Password', 'access-category-password' ), // Title for the page
  32.             __( 'Access Category Password', 'access-category-password' ), //  Page name in admin menu
  33.             'manage_options', //  Minimum role required to see the page
  34.             'acpwd_options_page', // unique identifier
  35.             'acpwd_options_do_page'  // name of function to display the page
  36.         );
  37.         add_action( 'admin_init', 'acpwd_options_settings' );
  38.     }
  39. }
  40. /**
  41.  * Create the options page
  42.  */
  43.  
  44. if( !function_exists('acpwd_options_do_page'))  {
  45.     function acpwd_options_do_page() { ?>
  46.  
  47. <div class="wrap">
  48.  
  49.         <h2><?php _e( 'Access Category Password Options', 'access-category-password' ) ?></h2>
  50.  
  51.         <?php
  52.         /*** To debug, here we can print the plugin options **/
  53.         /*
  54.         echo '<pre>';
  55.         $options = get_option( 'acpwd_settings_options' );
  56.         print_r($options);
  57.         echo '</pre>';
  58.         */
  59.          ?>
  60.  
  61.         <form method="post" action="options.php">
  62.             <?php settings_fields( 'acpwd_settings_options' ); ?>
  63.             <?php do_settings_sections('acpwd_setting_section'); ?>
  64.             <p><input class="button-primary"  name="Submit" type="submit" value="<?php esc_attr_e(__('Save Changes', 'access-category-password')); ?>" /></p>
  65.         </form>
  66.  
  67. </div>
  68.  
  69. <?php
  70.     } // end acpwd_options_do_page
  71. }
  72.  
  73. /**
  74.  * Init plugin options to white list our options
  75.  */
  76. if( !function_exists('acpwd_options_settings'))  {
  77.     function acpwd_options_settings(){
  78.         /* Register acpwd settings. */
  79.         register_setting(
  80.             'acpwd_settings_options',  //$option_group , A settings group name. Must exist prior to the register_setting call. This must match what's called in settings_fields()
  81.             'acpwd_settings_options', // $option_name The name of an option to sanitize and save.
  82.             'acpwd_options_validate' // $sanitize_callback  A callback function that sanitizes the option's value.
  83.         );
  84.  
  85.         /** Add a section **/
  86.         add_settings_section(
  87.             'acpwd_option_main', //  section name unique ID
  88.             '&nbsp;', // Title or name of the section (to be output on the page), you can leave nbsp here if not wished to display
  89.             'acpwd_option_section_text',  // callback to display the content of the section itself
  90.             'acpwd_setting_section' // The page name. This needs to match the text we gave to the do_settings_sections function call
  91.         );
  92.  
  93.         /** Register each option **/
  94.         add_settings_field(
  95.             'password',  //$id a unique id for the field
  96.             __( 'The password', 'access-category-password' ), // the title for the field
  97.             'acpwd_func_password',  // the function callback, to display the input box
  98.             'acpwd_setting_section',  // the page name that this is attached to (same as the do_settings_sections function call).
  99.             'acpwd_option_main' // the id of the settings section that this goes into (same as the first argument to add_settings_section).
  100.         );
  101.  
  102.         add_settings_field(
  103.             'impacted_categories',
  104.             __( 'Impacted categories', 'access-category-password' ),
  105.             'acpwd_func_impacted_categories',
  106.             'acpwd_setting_section',
  107.             'acpwd_option_main'
  108.         );
  109.  
  110.         add_settings_field(
  111.             'allowed_users',
  112.             __( 'Granting users', 'access-category-password' ),
  113.             'acpwd_func_allowed_users',
  114.             'acpwd_setting_section',
  115.             'acpwd_option_main'
  116.         );
  117.  
  118.         add_settings_field(
  119.             'only_single',
  120.             __( 'Only single post', 'access-category-password' ),
  121.             'acpwd_func_only_single',
  122.             'acpwd_setting_section',
  123.             'acpwd_option_main'
  124.         );
  125.  
  126.  
  127.         add_settings_field(
  128.             'info_message',
  129.             __( 'Info message', 'access-category-password' ),
  130.             'acpwd_func_info_message',
  131.             'acpwd_setting_section',
  132.             'acpwd_option_main'
  133.         );
  134.  
  135.         add_settings_field(
  136.             'input_placeholder',
  137.             __( 'Input placeholder', 'access-category-password' ),
  138.             'acpwd_func_input_placeholder',
  139.             'acpwd_setting_section',
  140.             'acpwd_option_main'
  141.         );
  142.  
  143.         add_settings_field(
  144.             'error_message',
  145.             __( 'Error message', 'access-category-password' ),
  146.             'acpwd_func_error_message',
  147.             'acpwd_setting_section',
  148.             'acpwd_option_main'
  149.         );
  150.  
  151.         add_settings_field(
  152.             'valid_button_text',
  153.             __( 'The validation button text', 'access-category-password' ),
  154.             'acpwd_func_valid_button_text',
  155.             'acpwd_setting_section',
  156.             'acpwd_option_main'
  157.         );
  158.  
  159.         add_settings_field(
  160.             'output_styling',
  161.             __( 'Styling the form', 'access-category-password' ),
  162.             'acpwd_func_output_styling',
  163.             'acpwd_setting_section',
  164.             'acpwd_option_main'
  165.         );
  166.  
  167.         add_settings_field(
  168.             'feed_desc_text',
  169.             __( 'The feed item description text', 'access-category-password' ),
  170.             'acpwd_func_feed_desc_text',
  171.             'acpwd_setting_section',
  172.             'acpwd_option_main'
  173.         );
  174.     }
  175. }
  176.  
  177. /** the theme section output**/
  178. if( !function_exists('acpwd_option_section_text'))  {
  179.     function acpwd_option_section_text(){
  180.     echo '<p>'.__( 'Here you can set the options of Access Category Password plugin. Set a password, check the categories with restricted access (the posts in these categories will require a password authentication), check the users roles that don\'t need authentification to access these categories  and define some strings used to inform the user on frontend page.', 'access-category-password' ).'</p>';
  181.     }
  182. }
  183.  
  184. /** The password field **/
  185. if( !function_exists('acpwd_func_password'))  {
  186.     function acpwd_func_password() {
  187.          /* Get the option value from the database. */
  188.         $options = get_option( 'acpwd_settings_options' );
  189.         $password = (isset($options['password']) && $options['password'] != '') ? '' : 'acpwdpass' ;
  190.         /* Echo the field. */ ?>
  191.         <label for="paswword" > <?php _e( 'Password', 'access-category-password' ); ?></label>
  192.         <input type="password" id="limit_true" name="acpwd_settings_options[password]" value="<?php echo $password ?>" />
  193.         <p class="description">
  194.             <?php _e( 'You can type a string or a sentence, or whatever you want. If not set, the default password is <strong>acpwdpass</strong>.', 'access-category-password' ); ?>
  195.         </p>
  196.     <?php }
  197. }
  198.  
  199.  
  200. /** The Impacted categories Checkboxes **/
  201. if( !function_exists('acpwd_func_impacted_categories'))  {
  202.     function acpwd_func_impacted_categories(){
  203.     /* Get the option value from the database. */
  204.         $options = get_option( 'acpwd_settings_options' );
  205.         $impacted_categories = (isset($options['impacted_categories'])) ? $options['impacted_categories'] : array();
  206.         /* Echo the field. */ ?>
  207.         <div id="impacted_categories">
  208.         <?php
  209.         $cats = get_categories(array('hide_empty' => 0));
  210.         foreach( $cats as $cat ) { ?>
  211.             <input type="checkbox" name="acpwd_settings_options[impacted_categories][]" value="<?php echo $cat->cat_ID ?>"<?php if (in_array($cat->cat_ID, $impacted_categories)) echo ' checked'; ?> /> <?php echo $cat->cat_name ?><br>
  212.         <?php } ?>
  213.         <p class="description">
  214.             <?php _e( 'Check the categories that you want to have password restricted post access.', 'access-category-password' ); ?>
  215.         </p>
  216.         </div>
  217.     <?php }
  218. }
  219.  
  220. /** The users that don't have to enter the Password  **/
  221. if( !function_exists('acpwd_func_allowed_users'))  {
  222.     function acpwd_func_allowed_users(){
  223.     /* Get the option value from the database. */
  224.         $options = get_option( 'acpwd_settings_options' );
  225.         $allowed_users =  (isset($options['allowed_users'])) ? $options['allowed_users'] : array();
  226.         /* Function to translate the user role */
  227.         $role_label = array('administrator'=>__('Administrator', 'access-category-password'), 'editor'=>__('Editor', 'access-category-password'), 'author'=>__('Author', 'access-category-password'), 'contributor'=>__('Contributor', 'access-category-password'), 'subscriber'=>__('Subscriber', 'access-category-password'));
  228.         /* Echo the field. */ ?>
  229.         <div id="allowed_users">
  230.         <?php
  231.         $roles = get_editable_roles();
  232.         foreach( $roles as $role => $role_info ) { ?>
  233.             <input type="checkbox" name="acpwd_settings_options[allowed_users][]" value="<?php echo $role ?>"<?php if (in_array($role, $allowed_users)) echo ' checked'; ?> /> <?php echo $role_label[$role] ?><br>
  234.         <?php } ?>
  235.         <p class="description">
  236.             <?php _e( 'Check the users roles granted to access the protected categories ressources without having to provide the password.', 'access-category-password' ); ?>
  237.         </p>
  238.         </div>
  239.     <?php }
  240. }
  241.  
  242. /** Only hide the single pos content **/
  243. if( !function_exists('acpwd_func_only_single'))  {
  244.     function acpwd_func_only_single(){
  245.     /* Get the option value from the database. */
  246.         $options = get_option( 'acpwd_settings_options' );
  247.         $only_single = (isset($options['only_single'])) ? $options['only_single'] : 0;
  248.         /* Echo the field. */ ?>
  249.         <div id="allowed_users">
  250.             <input type="checkbox" name="acpwd_settings_options[only_single]" value="1"<?php if (isset($only_single) && $only_single == 1) echo ' checked'; ?> /> <?php _e( 'Only hide the content of the single posts', 'access-category-password' ); ?>
  251.         <p class="description">
  252.             <?php _e( 'Check this if you only want to hide the content of the single post view. This will allow to display excerpt and content of protected posts on other pages like category, homepage, search, and so on&hellip;', 'access-category-password' ); ?>
  253.         </p>
  254.         </div>
  255.     <?php }}
  256.  
  257. /** The info field */
  258. if( !function_exists('acpwd_func_info_message'))  {
  259.     function acpwd_func_info_message(){
  260.     /* Get the option value from the database. */
  261.         $options = get_option( 'acpwd_settings_options' );
  262.         $info_option = (isset($options['info_message']) && $options['info_message'] != '') ? $options['info_message'] : __('This content has restricted access, please type the password below and get access.', 'access-category-password');
  263.         /* Echo the field. */ ?>
  264.         <textarea style="width: 50%; height: 100px;" id="info_message" name="acpwd_settings_options[info_message]"><?php echo stripslashes($info_option); ?></textarea>
  265.         <p class="description">
  266.             <?php _e( 'The message displayed before the password form of the protected resource (HTML formating with allowed tags).', 'access-category-password' ); ?><br>
  267.             <?php echo '<strong>'.__('Allowed tags:', 'access-category-password').'</strong> '. allowed_tags() ?>
  268.         </p>
  269.     <?php }
  270. }
  271.  
  272. /** The Input placeholder **/
  273. if( !function_exists('acpwd_func_input_placeholder'))  {
  274.     function acpwd_func_input_placeholder(){
  275.     /* Get the option value from the database. */
  276.         $options = get_option( 'acpwd_settings_options' );
  277.         $input_placeholder = (isset($options['input_placeholder'])) ? $options['input_placeholder'] : '';
  278.     /* Echo the field. */ ?>
  279.         <input type="text" style="width: 95%;" id="input_placeholder" name="acpwd_settings_options[input_placeholder]" value="<?php echo stripslashes($input_placeholder); ?>" />
  280.         <p class="description">
  281.             <?php _e( 'The placeholder displayed in the Password field on page load (HTML formating not allowed).', 'access-category-password' ) ?>
  282.     </p>
  283.     <?php }
  284. }
  285.  
  286. /** The error message **/
  287. if( !function_exists('acpwd_func_error_message'))  {
  288.     function acpwd_func_error_message(){
  289.     /* Get the option value from the database. */
  290.         $options = get_option( 'acpwd_settings_options' );
  291.         $error_message = (isset($options['error_message']) && $options['error_message'] != '') ? $options['error_message'] : __('Sorry, but this is the wrong password.', 'access-category-password');
  292.         /* Echo the field. */ ?>
  293.         <input style="width: 95%;" type="text" id="message_error_option" name="acpwd_settings_options[error_message]" value="<?php echo stripslashes($error_message); ?>" />
  294.         <p class="description">
  295.             <?php _e( 'The message that will display if the user typed the wrong password (HTML formating with allowed tags).', 'access-category-password' ) ?><br>
  296.             <?php echo '<strong>'.__('Allowed tags:', 'access-category-password').'</strong> '. allowed_tags() ?>
  297.         </p>
  298.     <?php }
  299. }
  300.  
  301. /** The validation button text **/
  302. if( !function_exists('acpwd_func_valid_button_text'))  {
  303.     function acpwd_func_valid_button_text(){
  304.     /* Get the option value from the database. */
  305.         $options = get_option( 'acpwd_settings_options' );
  306.         $valid_button_text_option = (isset($options['valid_button_text']) && $options['valid_button_text'] != '') ? $options['valid_button_text'] : __('Get access', 'access-category-password');
  307.         /* Echo the field. */ ?>
  308.         <input type="text" style="width: 95%;" id="feed_desc_text" name="acpwd_settings_options[valid_button_text]" value="<?php echo stripslashes($valid_button_text_option); ?>" />
  309.         <p class="description">
  310.             <?php _e( 'The validation button text to submit the entered password (HTML formating not allowed).', 'access-category-password' ) ?>
  311.         </p>
  312.     <?php }
  313. }
  314.  
  315. /** The output styling **/
  316. if( !function_exists('acpwd_func_output_styling'))  {
  317.     function acpwd_func_output_styling(){
  318. ?>
  319. <p><?php _e( 'You can style the form that replace the content of a protected article by using the <strong style="color: #0073AA">classes</strong> you can see in the code below in your current theme stylesheet:', 'access-category-password' ) ?><br><code>
  320. <?php echo get_template_directory_uri(); ?>/style.css</code>.</p>
  321. <pre style="width: 90%; overflow: auto; padding: 1% 2%; margin: 1% 0; background: #FFF; border: #ddd 1px solid;">&lt;div class&equals;&quot;<span style="color: #0073AA">acpwd-container</span>&quot;&gt;&NewLine;    &lt;p class&equals;&quot;<span style="color: #0073AA">acpwd-info-message</span>&quot;&gt;This content has restricted access&comma; please type the password below and get access&period;&lt;&sol;p&gt;&NewLine;    &lt;form class&equals;&quot;<span style="color: #0073AA">acpwd-form</span>&quot; action&equals;&quot;&quot; method&equals;&quot;post&quot;&gt;&NewLine;        &lt;input class&equals;&quot;<span style="color: #0073AA">acpwd-pass</span>&quot; type&equals;&quot;password&quot; name&equals;&quot;acpwd-pass&quot;&gt;&NewLine;        &lt;input class&equals;&quot;<span style="color: #0073AA">acpwd-submit</span>&quot; type&equals;&quot;submit&quot; value&equals;&quot;Get access&quot;&gt;&NewLine;    &lt;&sol;form&gt;&NewLine;    &lt;p class&equals;&quot;<span style="color: #0073AA">acpwd-error-message</span>&quot; style&equals;&quot;color&colon; darkred&semi;&quot;&gt;Sorry&comma; but this is the wrong password&period;&lt;&sol;p&gt;&NewLine;&lt;&sol;div&gt;</pre>
  322.     <?php }
  323. }
  324.  
  325.  
  326. /** The feed item description text **/
  327. if( !function_exists('acpwd_func_feed_desc_text'))  {
  328.     function acpwd_func_feed_desc_text(){
  329.     /* Get the option value from the database. */
  330.         $options = get_option( 'acpwd_settings_options' );
  331.         $feed_desc_text_option = (isset($options['feed_desc_text']) && $options['feed_desc_text'] != '') ? $options['feed_desc_text'] : __('Access to this post restricted, please go to the website to read it.', 'access-category-password');
  332.         /* Echo the field. */ ?>
  333.         <input type="text" style="width: 95%;" id="feed_desc_text" name="acpwd_settings_options[feed_desc_text]" value="<?php echo stripslashes($feed_desc_text_option); ?>" />
  334.         <p class="description">
  335.             <?php _e( 'The feed item descriptions that belong to access restricted posts will be replaced by this sentence (HTML formating not allowed).', 'access-category-password' ) ?>
  336.         </p>
  337.     <?php }
  338. }
  339.  
  340. /**
  341.  * Sanitize and validate input. Accepts an array, return a sanitized array.
  342.  */
  343. if( !function_exists('acpwd_options_validate'))  {
  344.     function acpwd_options_validate( $input ) {
  345.     $options = get_option( 'acpwd_settings_options' );
  346.  
  347.     /** Password crypting */
  348.     if ($input['password'] != '')
  349.         $options['password'] = crypt($input['password'], $input['password']);
  350.  
  351.     /** Impacted Categories validation **/
  352.   if ( ! isset( $input['impacted_categories'] ) )
  353.       $input['impacted_categories'] = array();
  354.   $options['impacted_categories'] = $input['impacted_categories'];
  355.  
  356.   /** Allowed users validation **/
  357.    if ( ! isset( $input['allowed_users'] ) )
  358.        $input['allowed_users'] = array();
  359.    $options['allowed_users'] = $input['allowed_users'];
  360.  
  361.   /** Show the excerpt validation **/
  362.   if ( ! isset( $input['only_single'] ) )
  363.       $input['only_single'] = 0;
  364.   $options['only_single'] = $input['only_single'];
  365.  
  366.     /** clean info field, HTML allowed for the format */
  367.     $options['info_message'] = wp_filter_kses( $input['info_message'] );
  368.  
  369.   /** clean input placeholder text HTML not allowed */
  370.     $options['input_placeholder'] = wp_filter_nohtml_kses( esc_attr($input['input_placeholder']) );
  371.  
  372.     /** clean error message field, HTML allowed for the format */
  373.     $options['error_message'] = wp_filter_kses( $input['error_message'] );
  374.  
  375.     /** validation button text */
  376.     $options['valid_button_text'] = wp_filter_nohtml_kses( esc_attr($input['valid_button_text']) );
  377.  
  378.     /** clean feed desc text HTML not allowed */
  379.     $options['feed_desc_text'] = wp_filter_nohtml_kses( esc_attr($input['feed_desc_text']) );
  380.  
  381.     return $options;
  382.     }
  383. }
  384.  
  385. /* ******************************* */
  386. /* Frontend of the plugin          */
  387. /* ******************************* */
  388.  
  389. /* Start and destroy sessions */
  390. add_action('init', 'acpwdStartSession', 1);
  391. add_action('wp_logout', 'acpwdEndSession');
  392. add_action('wp_login', 'acpwdEndSession');
  393.  
  394. function acpwdStartSession() {
  395.     if (!session_id()) {
  396.         session_start();
  397.     }
  398. }
  399.  
  400. function acpwdEndSession() {
  401.     unset($_SESSION['acpwd_session']);
  402.     session_destroy ();
  403. }
  404.  
  405. /* Validation of the password */
  406. function acpwd_session_check() {
  407.     // The form has been submited
  408.     if(isset($_POST['acpwd-pass'])) {
  409.         // Checking password
  410.         $acpwd_options = get_option('acpwd_settings_options');
  411.         if(crypt($_POST['acpwd-pass'], $_POST['acpwd-pass']) == $acpwd_options['password']) {
  412.             $_SESSION['acpwd_session'] = 1;
  413.         }
  414.         elseif (crypt($_POST['acpwd-pass'], $_POST['acpwd-pass']) != $acpwd_options['password']) {
  415.             $_POST['acpwd-msg'] = ($acpwd_options['error_message'] != '') ? '<p class="acpwd-error-message" style="color: darkred;">'.stripslashes($acpwd_options['error_message']).'</p>' : '<p class="acpwd-error-message" style="color: darkred;">'.__('Sorry, but this is the wrong password.', 'access-category-password').'</p>';
  416.             $_SESSION['acpwd_session'] = 0;
  417.         }
  418.     }
  419. }
  420. add_action('init', 'acpwd_session_check', 2);
  421.  
  422. /* Displaying the password form or the feed replacement sentence */
  423. function acpwd_frontend_changes($content) {
  424.     if (is_user_logged_in()) {
  425.         $user = wp_get_current_user();
  426.         $u_roles = $user->roles;
  427.         $u_role = $u_roles[0];
  428.     } else {
  429.         $u_role = 'not logged in';
  430.     }
  431.     $acpwd_options = get_option('acpwd_settings_options');
  432.     $impacted_categories = (isset($acpwd_options['impacted_categories'])) ? $acpwd_options['impacted_categories'] : array();
  433.     $allowed_users = (isset($acpwd_options['allowed_users'])) ? $acpwd_options['allowed_users'] : array();
  434.     if ( in_category($impacted_categories) ) {
  435.         if ((isset($_SESSION['acpwd_session']) && $_SESSION['acpwd_session'] == 1) || (isset($acpwd_options['only_single']) && $acpwd_options['only_single'] == 1 && !is_single()) || in_array($u_role, $allowed_users)) {
  436.             $content = $content;
  437.         } else {
  438.             if (is_feed()) {
  439.                 // Feed content replacement
  440.                 $content = stripslashes($acpwd_options['feed_desc_text']) ;
  441.             } else {
  442.                 // Post or excerpt content replacement
  443.                 $content = '<div class="acpwd-container" id="acpwd-'.get_the_ID().'">';
  444.                 $content .= (isset($acpwd_options['info_message']) && $acpwd_options['info_message'] != '') ? '<p class="acpwd-info-message">'.stripslashes($acpwd_options['info_message']).'</p>' : '<p class="acpwd-info-message">'.__('This content has restricted access, please type the password below and get access.', 'access-category-password').'</p>';
  445.                 $content .= '<form style="display:none" class="acpwd-form" action="'.$_SERVER['REQUEST_URI'].'#acpwd-'.get_the_ID().'" method="post">';
  446.                 $content .= '<input class="acpwd-pass" type="password" name="acpwd-pass" placeholder="'.stripslashes($acpwd_options['input_placeholder']).'">';
  447.                 $content .= (isset($acpwd_options['valid_button_text']) && $acpwd_options['valid_button_text'] != '') ? '<input class="acpwd-submit" type="submit" value="'.$acpwd_options['valid_button_text'].'">' : '<input class="acpwd-submit" type="submit" value="'.__('Get access', 'access-category-password').'">';
  448.                 $content .= '</form>';
  449.                 if (isset ($_POST['acpwd-msg']))
  450.                     $content .= $_POST['acpwd-msg'];
  451.                 $content .= '</div>';
  452.             }
  453.         }
  454.     }
  455.     return $content;
  456. }
  457. add_filter( 'the_content', 'acpwd_frontend_changes' );
  458. add_filter( 'get_the_excerpt', 'acpwd_frontend_changes' );
  459. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement