Plugin Directory

Changeset 426390


Ignore:
Timestamp:
08/20/2011 02:52:10 PM (15 years ago)
Author:
btks
Message:

Version 0.2

Location:
wordpress-restrictions
Files:
2 edited
3 copied

Legend:

Unmodified
Added
Removed
  • wordpress-restrictions/tags/0.2/readme.txt

    r426019 r426390  
    55Requires at least: 3.1
    66Tested up to: 3.2.1
    7 Stable tag: 0.1.3.2
     7Stable tag: 0.2
    88
    99WordPress Restrictions allows you to set restrictions on when and what content can be edited/deleted on your WordPress Install.
     
    1313WordPress Restrictions allows you to set restrictions on when and what content can be edited/deleted on your WordPress Install.
    1414
    15 Supported Features:
     15<strong>Supported Features:</strong>
    1616
    1717<ul>
  • wordpress-restrictions/tags/0.2/wp-restrictions.php

    r426019 r426390  
    44Plugin URI: http://sonicedges.com/plugins/wordpress-restrictions/
    55Description: With WordPress Restrictions, you can specify when and what content may be edited/deleted by Editors and/or Authors.
    6 Version: 0.1.3.2
     6Version: 0.2
    77Author: Brandon Smith
    88Author URI: http://sonicedges.com/
    99*/
    1010
    11 define('WP_REST_VERSION', '0.1.3.2');
    12 define('WP_REST_CURR_DAY', date("j"));
    13 define('WP_REST_CURR_MONTH', date("n"));
    14 define('WP_REST_CURR_YEAR', date("Y"));
     11define('WP_REST_VERSION', '0.2'); // Define WP Restrictions Version
     12define('WP_REST_CURR_DAY', date("j")); // Define Current Day of Month
     13define('WP_REST_CURR_MONTH', date("n")); // Define Current Month (Numeric)
     14define('WP_REST_CURR_YEAR', date("Y")); // Define Current Year (YYYY)
     15define('WP_REST_URL', plugin_dir_url(__FILE__)); // Define WP Plugin URL
     16define('WP_REST_PATH', plugin_dir_path(__FILE__)); // Define WP Plugin Path
    1517
    16 function wp_restrictions() {
    17     wp_restrictions::get_user_role(); // Define Current User's Role
    18     wp_restrictions::get_user_id(); // Define Current User's ID
    19     wp_restrictions_max_posts(); // Restricts # of Posts per Day
    20 }
     18require WP_REST_PATH.'inc/define.class.php'; // Load Define Class
     19require WP_REST_PATH.'inc/excluded.class.php'; // Load Excluded Class
     20require WP_REST_PATH.'inc/restrictions.class.php'; // Load Restrictions Class
     21require WP_REST_PATH.'inc/admin.class.php'; // Load Restrictions Admin
    2122
    22 class wp_restrictions {
    23     public function get_user_role() {
    24         if (current_user_can('editor') || current_user_can('author')) {
    25             if (current_user_can('editor')) {
    26                 $role = 'editor';
    27             } else {
    28                 $role = 'author';
    29             }
    30         }
    31         define('WP_REST_ROLE', $role);
    32     }
    33     public function get_user_id() {
    34         global $current_user;
    35         get_currentuserinfo();
    36         $user_id = $current_user->ID;
    37         define('WP_REST_UID', $user_id);
    38     }
    39 }
    40 
    41 function wp_restrictions_excluded_user($user_id) {
    42     $wp_restrictions = get_option('wp_restrictions');
    43     $ids = explode(",", $wp_restrictions['excluded']['user_ids']);
    44 
    45     if (in_array($user_id,$ids)) {
    46         return true;
    47     } else {
    48         return false;
    49     }
    50 }
    51 
    52 function wp_restrictions_excluded_post($post_id) {
    53     $wp_restrictions = get_option('wp_restrictions');
    54     $ids = explode(",", $wp_restrictions['excluded']['post_ids']);
    55 
    56     if (in_array($post_id,$ids)) {
    57         return true;
    58     } else {
    59         return false;
    60     }
    61 }
    62 
    63 function wp_restrictions_excluded_page($page_id) {
    64     $wp_restrictions = get_option('wp_restrictions');
    65     $ids = explode(",", $wp_restrictions['excluded']['page_ids']);
    66 
    67     if (in_array($page_id,$ids)) {
    68         return true;
    69     } else {
    70         return false;
    71     }
    72 }
    73 
    74 function wp_restrictions_listusers() {
    75     $args = array('orderby' => 'user_id', 'role' => 'Editor');
    76     $wp_user_query = new WP_User_Query($args);
    77     $editors = $wp_user_query->get_results();
    78 
    79     foreach ($editors as $editor) {
    80         $editor = get_userdata($editor->ID);
    81 
    82         if (wp_restrictions_excluded_user($editor->ID)) {
    83             $excluded = "(excluded)";
    84         } else  {
    85             $excluded = "(not excluded)";
    86         }
    87 
    88         echo "<tr>";
    89         echo "<td>" . $editor->ID . " " . "<span style=\"font-weight: 700; color: #8B0000;\">" . $excluded . "</span></td>";
    90         echo "<td>" . $editor->user_login . "</td>";
    91         echo "<td>Editor</td>";
    92         echo "<td>" . $editor->display_name . "</td>";
    93         echo "<td>" . $editor->user_email . "</td>";
    94     }
    95 
    96     $args = array('orderby' => 'user_id', 'role' => 'Author');
    97     $wp_user_query = new WP_User_Query($args);
    98     $authors = $wp_user_query->get_results();
    99 
    100     foreach ($authors as $author) {
    101             $author = get_userdata($author->ID);
    102 
    103         if (wp_restrictions_excluded_user($author->ID)) {
    104             $excluded = "(excluded)";
    105         } else  {
    106             $excluded = "(not excluded)";
    107         }
    108 
    109         echo "<tr>";
    110         echo "<td>" . $author->ID . " " . "<span style=\"font-weight: 700; color: #8B0000;\">" . $excluded . "</span></td>";
    111         echo "<td>" . $author->user_login . "</td>";
    112         echo "<td>Author</td>";
    113         echo "<td>" . $author->display_name . "</td>";
    114         echo "<td>" . $author->user_email . "</td>";
    115         echo "</tr>";
    116     }
    117 }
    118 
    119 function wp_restrictions_user_role() {
    120     if (current_user_can('editor') || current_user_can('author')) {
    121         if (current_user_can('editor')) {
    122             $role = 'editor';
    123         } else {
    124             $role = 'author';
    125         }
    126     }
    127     define('WP_REST_ROLE', $role);
    128 }
    129 
    130 function wp_restrictions_mmc($caps, $cap, $user_id, $args) {
    131     global $current_user;
    132     get_currentuserinfo();
    133 
    134     $wp_restrictions = get_option('wp_restrictions');
    135 
    136     if (WP_REST_ROLE == 'author' && !wp_restrictions_excluded_user($current_user->ID)) {
    137         if ($cap == 'delete_post') {
    138             $posts = get_posts($args[0]);
    139             foreach ($posts as $post) {
    140                 setup_postdata($post);
    141                 if ($wp_restrictions['author']['delete_post'] == '0' || $wp_restrictions['author']['delete_post'] == '' || wp_restrictions_excluded_post(get_the_ID())) {
    142                     return;
    143                 } elseif ($wp_restrictions['author']['delete_post'] == 1) {
    144                     if (get_the_date() != date("F j, Y")) {
    145                         $caps[] = 'delete_post';
    146                     }
    147                 } else {
    148                     $post_date = get_the_date('F j, Y');
    149                     $num_days = "+" . $wp_restrictions['author']['delete_post'] . " " . "days";
    150                     $delete_until = strtotime(date("F j, Y", strtotime($post_date)) . " $num_days");
    151 
    152                     if (strtotime("now") > $delete_until) {
    153                         $caps[] = 'delete_post';
    154                     }
    155                 }
    156             }
    157         }
    158         if ($cap == 'edit_post') {
    159             $posts = get_posts($args[0]);
    160             foreach ($posts as $post) {
    161                 setup_postdata($post);
    162                 if ($wp_restrictions['author']['edit_post'] == 0 || $wp_restrictions['author']['edit_post'] == '' || wp_restrictions_excluded_post(get_the_ID())) {
    163                     return;
    164                 } elseif ($wp_restrictions['author']['edit_post'] == 1) {
    165                     if (get_the_date() != date("F j, Y")) {
    166                         $caps[] = 'delete_post';
    167                     }
    168                 } else {
    169                     $post_date = get_the_date('F j, Y');
    170                     $num_days = "+" . $wp_restrictions['author']['edit_post'] . " " . "days";
    171                     $edit_until = strtotime(date("F j, Y", strtotime($post_date)) . " $num_days");
    172 
    173                     if (strtotime("now") > $edit_until) {
    174                         $caps[] = 'edit_post';
    175                     }
    176                 }
    177             }
    178         }
    179     }
    180 
    181     if (WP_REST_ROLE == 'editor' && !wp_restrictions_excluded_user($current_user->ID)) {
    182         if ($cap == 'delete_post') {
    183             $posts = get_posts($args[0]);
    184             foreach ($posts as $post) {
    185                 setup_postdata($post);
    186                 if ($wp_restrictions['editor']['delete_post'] == '0' || $wp_restrictions['editor']['delete_post'] == '' || wp_restrictions_excluded_post(get_the_ID())) {
    187                     return;
    188                 } elseif ($wp_restrictions['editor']['delete_post'] == 1) {
    189                     if (get_the_date() != date("F j, Y")) {
    190                         $caps[] = 'delete_post';
    191                     }
    192                 } else {
    193                     $post_date = get_the_date('F j, Y');
    194                     $num_days = "+" . $wp_restrictions['editor']['delete_post'] . " " . "days";
    195                     $delete_until = strtotime(date("F j, Y", strtotime($post_date)) . " $num_days");
    196 
    197                     if (strtotime("now") > $delete_until) {
    198                         $caps[] = 'delete_post';
    199                     }
    200                 }
    201             }
    202         }
    203         if ($cap == 'edit_post') {
    204             $posts = get_posts($args[0]);
    205             foreach ($posts as $post) {
    206                 setup_postdata($post);
    207                 if ($wp_restrictions['editor']['edit_post'] == 0 || $wp_restrictions['editor']['edit_post'] == '' || wp_restrictions_excluded_post(get_the_ID())) {
    208                     return;
    209                 } elseif ($wp_restrictions['editor']['edit_post'] == 1) {
    210                     if (get_the_date() != date("F j, Y")) {
    211                         $caps[] = 'delete_post';
    212                     }
    213                 } else {
    214                     $post_date = get_the_date('F j, Y');
    215                     $num_days = "+" . $wp_restrictions['editor']['edit_post'] . " " . "days";
    216                     $edit_until = strtotime(date("F j, Y", strtotime($post_date)) . " $num_days");
    217 
    218                     if (strtotime("now") > $edit_until) {
    219                         $caps[] = 'edit_post';
    220                     }
    221                 }
    222             }
    223         }
    224         if ($cap == 'delete_page') {
    225             $pages = get_pages($args[0]);
    226             foreach ($pages as $page) {
    227                 if ($wp_restrictions['editor']['delete_page'] == 0 || $wp_restrictions['editor']['delete_page'] == '' || wp_restrictions_excluded_page(get_the_ID())) {
    228                     return;
    229                 } elseif ($wp_restrictions['editor']['delete_page'] == 1) {
    230                     if (get_the_date() != date("F j, Y")) {
    231                         $caps[] = 'delete_post';
    232                     }
    233                 } else {
    234                     $page_date = get_the_date('F j, Y');
    235                     $num_days = "+" . $wp_restrictions['editor']['delete_page'] . " " . "days";
    236                     $delete_until = strtotime(date("F j, Y", strtotime($page_date)) . " $num_days");
    237 
    238                     if (strtotime("now") > $delete_until) {
    239                         $caps[] = 'delete_page';
    240                     }
    241                 }
    242             }
    243         }
    244         if ($cap == 'edit_page') {
    245             $pages = get_pages($args[0]);
    246             foreach ($pages as $page) {
    247                 if ($wp_restrictions['editor']['edit_page'] == 0 || $wp_restrictions['editor']['edit_page'] == '' || wp_restrictions_excluded_page(get_the_ID())) {
    248                     return;
    249                 } elseif ($wp_restrictions['editor']['edit_page'] == 1) {
    250                     if (get_the_date() != date("F j, Y")) {
    251                         $caps[] = 'edit_page';
    252                     }
    253                 } else {
    254                     $page_date = get_the_date('F j, Y');
    255                     $num_days = "+" . $wp_restrictions['editor']['edit_page'] . " " . "days";
    256                     $edit_until = strtotime(date("F j, Y", strtotime($page_date)) . " $num_days");
    257 
    258                     if (strtotime("now") > $edit_until) {
    259                         $caps[] = 'edit_page';
    260                     }
    261                 }
    262             }
    263         }
    264     }
    265     return $caps;
    266 }
    267 
    268 function wp_restrictions_admin() {
    269     if (!current_user_can('manage_options')) {
    270         wp_die( __('You do not have sufficient permissions to access this page.') );
    271     }
    272 
    273     if (isset($_POST['wp_restrictions_submit'])) {
    274         $wp_restrictions = array(
    275                 'editor' => array(
    276                     'delete_post' => $_POST['editor_delete_posts'],
    277                     'edit_post' => $_POST['editor_edit_posts'],
    278                     'delete_page' => $_POST['editor_delete_pages'],
    279                     'edit_page' => $_POST['editor_edit_pages'],
    280                     'max_posts' => $_POST['editor_max_posts']
    281                 ),
    282                 'author' => array(
    283                     'delete_post' => $_POST['author_delete_posts'],
    284                     'edit_post' => $_POST['author_edit_posts'],
    285                     'max_posts' => $_POST['author_max_posts']
    286                 ),
    287                 'excluded' => array(
    288                     'user_ids' => $_POST['excluded_user_ids'],
    289                     'post_ids' => $_POST['excluded_post_ids'],
    290                     'page_ids' => $_POST['excluded_page_ids']
    291                 )
    292         );
    293 
    294         foreach($wp_restrictions as $var => $key) {
    295             $wp_restrictions[$var] = preg_replace('/[^0-9,]/', '', $key);
    296         }
    297 
    298         update_option('wp_restrictions', $wp_restrictions);
    299     }
    300 
    301     if (isset($_POST['wp_restrictions_uninstall'])) {
    302         if ($_POST['wp_restrictions_uninstall'] == 'Uninstall') {
    303             wp_restrictions_uninstall();
    304         }
    305     }
    306 
    307     $wp_restrictions = get_option('wp_restrictions');
    308 ?>
    309     <h3>WordPress Restrictions</h3>
    310     <p>I (Brandon Smith) developed WordPress Restrictions to offer an easier and more practicable solution to control what can be edited/deleted on your WordPress Install. While several other plugins already allow you to modify WordPress User Roles and Capabilities, they require that you tweak the default settings of each user role or create a new user role (which can get a bit confusing for the average webmaster). With WordPress Restrictions, you can specify when and what content may be edited by Editors and/or Authors. If you need help or assistance, or would like to submit a feature request, please contact me at btks1995@gmail.com.</p>
    311     <form name="wordpress_restrictions" method="POST" action="">
    312     <h3>Restrictions for Editors</h3>
    313     <label>Delete Posts Timeframe (In Days): </label><input type="text" name="editor_delete_posts" value="<?php echo $wp_restrictions['editor']['delete_post']; ?>" /><span style="margin-left: 5px;">Specify a Number of Days. A post published on 8-16-2011 would be deletable by <strong>EDITORS</strong> until 8-20-2011 if the timeframe was 4 Days.</span><br />
    314     <label>Delete Pages Timeframe (In Days): </label><input type="text" name="editor_delete_pages" value="<?php echo $wp_restrictions['editor']['delete_page']; ?>" /> <span style="margin-left: 5px;">Specify a Number of Days. A page published on 8-16-2011 would be deletable by <strong>EDITORS</strong> until 8-20-2011 if the timeframe was 4 Days.</span><br />
    315     <label>Edit Posts Timeframe (In Days): </label><input type="text" name="editor_edit_posts" value="<?php echo $wp_restrictions['editor']['edit_post']; ?>" /> <span style="margin-left: 5px;">Specify a Number of Days. A post published on 8-16-2011 would be editable by <strong>EDITORS</strong> until 8-20-2011 if the timeframe was 4 Days.</span><br />
    316     <label>Edit Pages Timeframe (In Days): </label><input type="text" name="editor_edit_pages" value="<?php echo $wp_restrictions['editor']['edit_page']; ?>" /> <span style="margin-left: 5px;">Specify a Number of Days. A page published on 8-16-2011 would be deletable by <strong>EDITORS</strong> until 8-20-2011 if the timeframe was 4 Days.</span><br />
    317     <label>Max Number of Posts a Day: </label><input type="text" name="editor_max_posts" value="<?php echo $wp_restrictions['editor']['max_posts']; ?>" /> <span style="margin-left: 5px;">Specify a Number. If you input a number of '5', then each <strong>EDITOR</strong> can make up to 5 posts within 24 hours.</span><br />
    318 
    319     <h3>Restrictions for Authors</h3>
    320     <label>Delete Posts Timeframe (In Days): </label><input type="text" name="author_delete_posts" value="<?php echo $wp_restrictions['author']['delete_post']; ?>" /><span style="margin-left: 5px;">Specify a Number of Days. A post published on 8-16-2011 would be deletable by the <strong>AUTHOR</strong> until 8-20-2011 if the timeframe was 4 Days.</span><br />
    321     <label>Edit Posts Timeframe (In Days): </label><input type="text" name="author_edit_posts" value="<?php echo $wp_restrictions['author']['edit_post']; ?>" /> <span style="margin-left: 5px;">Specify a Number of Days. A post published on 8-16-2011 would be editable by the <strong>AUTHOR</strong> until 8-20-2011 if the timeframe was 4 Days.</span><br />
    322     <label>Max Number of Posts a Day: </label><input type="text" name="author_max_posts" value="<?php echo $wp_restrictions['author']['max_posts']; ?>" /> <span style="margin-left: 5px;">Specify a Number. If you input a number of '5', then each <strong>AUTHOR</strong> can make up to 5 posts within 24 hours.</span><br />
    323 
    324     <h3>Exclude Restrictions</h3>
    325     <label>User IDs (Comma [,] Separated): </label><input type="text" name="excluded_user_ids" value="<?php echo $wp_restrictions['excluded']['user_ids']; ?>" /><span style="margin-left: 5px;">Specify User IDs, <strong>COMMA SEPARATED</strong>. I have provided a list of Users and corresponding IDs for your convenience.</span><br />
    326     <label>Post IDs (Comma [,] Separated): </label><input type="text" name="excluded_post_ids" value="<?php echo $wp_restrictions['excluded']['post_ids']; ?>" /><span style="margin-left: 5px;">Specify User IDs, <strong>COMMA SEPARATED</strong>. I have provided a list of Posts and corresponding IDs for your convenience.</span><br />
    327     <label>Page IDs (Comma [,] Separated): </label><input type="text" name="excluded_page_ids" value="<?php echo $wp_restrictions['excluded']['page_ids']; ?>" /><span style="margin-left: 5px;">Specify User IDs, <strong>COMMA SEPARATED</strong>. I have provided a list of Pages and corresponding IDs for your convenience.</span><br />
    328 
    329     <table style="margin: 10px 0 10px 0; background: #DFDFDF; padding: 20px; width: 100%; ">
    330     <tr>
    331     <th style="text-align: left;">User ID</th>
    332     <th style="text-align: left;">Login Name</th>
    333     <th style="text-align: left;">User Role</th>
    334     <th style="text-align: left;">Display Name</th>
    335     <th style="text-align: left;">Email Address</th>
    336     </tr>
    337     <?php wp_restrictions_listusers(); ?>
    338     </table>
    339 
    340     <input type="submit" name="wp_restrictions_submit" value="Update Options">
    341     <input type="submit" name="wp_restrictions_uninstall" value="Uninstall">
    342 
    343     </form>
    344 <?php }
    345 
    346 function wp_restrictions_menu() {
    347     add_options_page('WordPress Restrictions', 'Restrictions', 8, basename(__FILE__), 'wp_restrictions_admin');
    348 }
    349 
    350 function wp_restrictions_max_posts() {
    351     global $wp_query;
    352     if (WP_REST_ROLE == 'editor' || WP_REST_ROLE == 'author' && !wp_restrictions_excluded_user(WP_REST_UID)) {
    353         $wp_query = new WP_Query(array('author' => WP_REST_UID, 'monthnum' => WP_REST_CURR_MONTH, 'day' => WP_REST_CURR_DAY, 'year' => WP_REST_CURR_YEAR));
    354         while($wp_query->have_posts()) : $wp_query->the_post();
    355             $post_count = $wp_query->post_count;
    356         endwhile;
    357         wp_reset_postdata();
    358 
    359         $wp_restrictions = get_option('wp_restrictions');
    360         $max_posts = $wp_restrictions[WP_REST_ROLE]['max_posts'];
    361 
    362         if ($max_posts != '' && $post_count >= $wp_restrictions[WP_REST_ROLE]['max_posts']) {
    363             remove_submenu_page('edit.php', 'post-new.php');
    364 
    365             if (strpos($_SERVER['REQUEST_URI'], 'post-new.php')) {
    366                     wp_die("You're only allowed to publish $max_posts posts within 24 hours. Please try again tomorrow.");
    367             }
    368         }
    369     }
    370 }
    371 
    372 function wp_restrictions_uninstall() {
    373     delete_option('wp_restrictions');
    374     echo '<div style="background-color: lightYellow; border: 1px solid #E6DB55; margin: 10px 10px 10px 0; padding: 6px;">WordPress Restrictions Options have been removed from your WordPress Install. If you\'d like to completely remove WordPress Restrictions, please do so through the Plugin Admin.</div>';
    375 }
    376 
    377 // WordPress Restrictions Actions
    378 add_action('admin_init', 'wp_restrictions');
    379 add_action('admin_menu', 'wp_restrictions_menu');
    380 
    381 // WordPress Restrictions Filters
    382 add_filter('map_meta_cap', 'wp_restrictions_mmc', 10, 4);
     23add_action('admin_init', 'wp_restrictions::load'); // Load Admin Actions
     24add_action('admin_menu', 'wp_rest_admin::menu'); // Load Restrictions Menu
     25add_filter('map_meta_cap', 'wp_restrictions::mmc', 10, 4); // Filter MMC
    38326
    38427?>
  • wordpress-restrictions/trunk/readme.txt

    r426019 r426390  
    55Requires at least: 3.1
    66Tested up to: 3.2.1
    7 Stable tag: 0.1.3.2
     7Stable tag: 0.2
    88
    99WordPress Restrictions allows you to set restrictions on when and what content can be edited/deleted on your WordPress Install.
     
    1313WordPress Restrictions allows you to set restrictions on when and what content can be edited/deleted on your WordPress Install.
    1414
    15 Supported Features:
     15<strong>Supported Features:</strong>
    1616
    1717<ul>
  • wordpress-restrictions/trunk/wp-restrictions.php

    r426019 r426390  
    44Plugin URI: http://sonicedges.com/plugins/wordpress-restrictions/
    55Description: With WordPress Restrictions, you can specify when and what content may be edited/deleted by Editors and/or Authors.
    6 Version: 0.1.3.2
     6Version: 0.2
    77Author: Brandon Smith
    88Author URI: http://sonicedges.com/
    99*/
    1010
    11 define('WP_REST_VERSION', '0.1.3.2');
    12 define('WP_REST_CURR_DAY', date("j"));
    13 define('WP_REST_CURR_MONTH', date("n"));
    14 define('WP_REST_CURR_YEAR', date("Y"));
     11define('WP_REST_VERSION', '0.2'); // Define WP Restrictions Version
     12define('WP_REST_CURR_DAY', date("j")); // Define Current Day of Month
     13define('WP_REST_CURR_MONTH', date("n")); // Define Current Month (Numeric)
     14define('WP_REST_CURR_YEAR', date("Y")); // Define Current Year (YYYY)
     15define('WP_REST_URL', plugin_dir_url(__FILE__)); // Define WP Plugin URL
     16define('WP_REST_PATH', plugin_dir_path(__FILE__)); // Define WP Plugin Path
    1517
    16 function wp_restrictions() {
    17     wp_restrictions::get_user_role(); // Define Current User's Role
    18     wp_restrictions::get_user_id(); // Define Current User's ID
    19     wp_restrictions_max_posts(); // Restricts # of Posts per Day
    20 }
     18require WP_REST_PATH.'inc/define.class.php'; // Load Define Class
     19require WP_REST_PATH.'inc/excluded.class.php'; // Load Excluded Class
     20require WP_REST_PATH.'inc/restrictions.class.php'; // Load Restrictions Class
     21require WP_REST_PATH.'inc/admin.class.php'; // Load Restrictions Admin
    2122
    22 class wp_restrictions {
    23     public function get_user_role() {
    24         if (current_user_can('editor') || current_user_can('author')) {
    25             if (current_user_can('editor')) {
    26                 $role = 'editor';
    27             } else {
    28                 $role = 'author';
    29             }
    30         }
    31         define('WP_REST_ROLE', $role);
    32     }
    33     public function get_user_id() {
    34         global $current_user;
    35         get_currentuserinfo();
    36         $user_id = $current_user->ID;
    37         define('WP_REST_UID', $user_id);
    38     }
    39 }
    40 
    41 function wp_restrictions_excluded_user($user_id) {
    42     $wp_restrictions = get_option('wp_restrictions');
    43     $ids = explode(",", $wp_restrictions['excluded']['user_ids']);
    44 
    45     if (in_array($user_id,$ids)) {
    46         return true;
    47     } else {
    48         return false;
    49     }
    50 }
    51 
    52 function wp_restrictions_excluded_post($post_id) {
    53     $wp_restrictions = get_option('wp_restrictions');
    54     $ids = explode(",", $wp_restrictions['excluded']['post_ids']);
    55 
    56     if (in_array($post_id,$ids)) {
    57         return true;
    58     } else {
    59         return false;
    60     }
    61 }
    62 
    63 function wp_restrictions_excluded_page($page_id) {
    64     $wp_restrictions = get_option('wp_restrictions');
    65     $ids = explode(",", $wp_restrictions['excluded']['page_ids']);
    66 
    67     if (in_array($page_id,$ids)) {
    68         return true;
    69     } else {
    70         return false;
    71     }
    72 }
    73 
    74 function wp_restrictions_listusers() {
    75     $args = array('orderby' => 'user_id', 'role' => 'Editor');
    76     $wp_user_query = new WP_User_Query($args);
    77     $editors = $wp_user_query->get_results();
    78 
    79     foreach ($editors as $editor) {
    80         $editor = get_userdata($editor->ID);
    81 
    82         if (wp_restrictions_excluded_user($editor->ID)) {
    83             $excluded = "(excluded)";
    84         } else  {
    85             $excluded = "(not excluded)";
    86         }
    87 
    88         echo "<tr>";
    89         echo "<td>" . $editor->ID . " " . "<span style=\"font-weight: 700; color: #8B0000;\">" . $excluded . "</span></td>";
    90         echo "<td>" . $editor->user_login . "</td>";
    91         echo "<td>Editor</td>";
    92         echo "<td>" . $editor->display_name . "</td>";
    93         echo "<td>" . $editor->user_email . "</td>";
    94     }
    95 
    96     $args = array('orderby' => 'user_id', 'role' => 'Author');
    97     $wp_user_query = new WP_User_Query($args);
    98     $authors = $wp_user_query->get_results();
    99 
    100     foreach ($authors as $author) {
    101             $author = get_userdata($author->ID);
    102 
    103         if (wp_restrictions_excluded_user($author->ID)) {
    104             $excluded = "(excluded)";
    105         } else  {
    106             $excluded = "(not excluded)";
    107         }
    108 
    109         echo "<tr>";
    110         echo "<td>" . $author->ID . " " . "<span style=\"font-weight: 700; color: #8B0000;\">" . $excluded . "</span></td>";
    111         echo "<td>" . $author->user_login . "</td>";
    112         echo "<td>Author</td>";
    113         echo "<td>" . $author->display_name . "</td>";
    114         echo "<td>" . $author->user_email . "</td>";
    115         echo "</tr>";
    116     }
    117 }
    118 
    119 function wp_restrictions_user_role() {
    120     if (current_user_can('editor') || current_user_can('author')) {
    121         if (current_user_can('editor')) {
    122             $role = 'editor';
    123         } else {
    124             $role = 'author';
    125         }
    126     }
    127     define('WP_REST_ROLE', $role);
    128 }
    129 
    130 function wp_restrictions_mmc($caps, $cap, $user_id, $args) {
    131     global $current_user;
    132     get_currentuserinfo();
    133 
    134     $wp_restrictions = get_option('wp_restrictions');
    135 
    136     if (WP_REST_ROLE == 'author' && !wp_restrictions_excluded_user($current_user->ID)) {
    137         if ($cap == 'delete_post') {
    138             $posts = get_posts($args[0]);
    139             foreach ($posts as $post) {
    140                 setup_postdata($post);
    141                 if ($wp_restrictions['author']['delete_post'] == '0' || $wp_restrictions['author']['delete_post'] == '' || wp_restrictions_excluded_post(get_the_ID())) {
    142                     return;
    143                 } elseif ($wp_restrictions['author']['delete_post'] == 1) {
    144                     if (get_the_date() != date("F j, Y")) {
    145                         $caps[] = 'delete_post';
    146                     }
    147                 } else {
    148                     $post_date = get_the_date('F j, Y');
    149                     $num_days = "+" . $wp_restrictions['author']['delete_post'] . " " . "days";
    150                     $delete_until = strtotime(date("F j, Y", strtotime($post_date)) . " $num_days");
    151 
    152                     if (strtotime("now") > $delete_until) {
    153                         $caps[] = 'delete_post';
    154                     }
    155                 }
    156             }
    157         }
    158         if ($cap == 'edit_post') {
    159             $posts = get_posts($args[0]);
    160             foreach ($posts as $post) {
    161                 setup_postdata($post);
    162                 if ($wp_restrictions['author']['edit_post'] == 0 || $wp_restrictions['author']['edit_post'] == '' || wp_restrictions_excluded_post(get_the_ID())) {
    163                     return;
    164                 } elseif ($wp_restrictions['author']['edit_post'] == 1) {
    165                     if (get_the_date() != date("F j, Y")) {
    166                         $caps[] = 'delete_post';
    167                     }
    168                 } else {
    169                     $post_date = get_the_date('F j, Y');
    170                     $num_days = "+" . $wp_restrictions['author']['edit_post'] . " " . "days";
    171                     $edit_until = strtotime(date("F j, Y", strtotime($post_date)) . " $num_days");
    172 
    173                     if (strtotime("now") > $edit_until) {
    174                         $caps[] = 'edit_post';
    175                     }
    176                 }
    177             }
    178         }
    179     }
    180 
    181     if (WP_REST_ROLE == 'editor' && !wp_restrictions_excluded_user($current_user->ID)) {
    182         if ($cap == 'delete_post') {
    183             $posts = get_posts($args[0]);
    184             foreach ($posts as $post) {
    185                 setup_postdata($post);
    186                 if ($wp_restrictions['editor']['delete_post'] == '0' || $wp_restrictions['editor']['delete_post'] == '' || wp_restrictions_excluded_post(get_the_ID())) {
    187                     return;
    188                 } elseif ($wp_restrictions['editor']['delete_post'] == 1) {
    189                     if (get_the_date() != date("F j, Y")) {
    190                         $caps[] = 'delete_post';
    191                     }
    192                 } else {
    193                     $post_date = get_the_date('F j, Y');
    194                     $num_days = "+" . $wp_restrictions['editor']['delete_post'] . " " . "days";
    195                     $delete_until = strtotime(date("F j, Y", strtotime($post_date)) . " $num_days");
    196 
    197                     if (strtotime("now") > $delete_until) {
    198                         $caps[] = 'delete_post';
    199                     }
    200                 }
    201             }
    202         }
    203         if ($cap == 'edit_post') {
    204             $posts = get_posts($args[0]);
    205             foreach ($posts as $post) {
    206                 setup_postdata($post);
    207                 if ($wp_restrictions['editor']['edit_post'] == 0 || $wp_restrictions['editor']['edit_post'] == '' || wp_restrictions_excluded_post(get_the_ID())) {
    208                     return;
    209                 } elseif ($wp_restrictions['editor']['edit_post'] == 1) {
    210                     if (get_the_date() != date("F j, Y")) {
    211                         $caps[] = 'delete_post';
    212                     }
    213                 } else {
    214                     $post_date = get_the_date('F j, Y');
    215                     $num_days = "+" . $wp_restrictions['editor']['edit_post'] . " " . "days";
    216                     $edit_until = strtotime(date("F j, Y", strtotime($post_date)) . " $num_days");
    217 
    218                     if (strtotime("now") > $edit_until) {
    219                         $caps[] = 'edit_post';
    220                     }
    221                 }
    222             }
    223         }
    224         if ($cap == 'delete_page') {
    225             $pages = get_pages($args[0]);
    226             foreach ($pages as $page) {
    227                 if ($wp_restrictions['editor']['delete_page'] == 0 || $wp_restrictions['editor']['delete_page'] == '' || wp_restrictions_excluded_page(get_the_ID())) {
    228                     return;
    229                 } elseif ($wp_restrictions['editor']['delete_page'] == 1) {
    230                     if (get_the_date() != date("F j, Y")) {
    231                         $caps[] = 'delete_post';
    232                     }
    233                 } else {
    234                     $page_date = get_the_date('F j, Y');
    235                     $num_days = "+" . $wp_restrictions['editor']['delete_page'] . " " . "days";
    236                     $delete_until = strtotime(date("F j, Y", strtotime($page_date)) . " $num_days");
    237 
    238                     if (strtotime("now") > $delete_until) {
    239                         $caps[] = 'delete_page';
    240                     }
    241                 }
    242             }
    243         }
    244         if ($cap == 'edit_page') {
    245             $pages = get_pages($args[0]);
    246             foreach ($pages as $page) {
    247                 if ($wp_restrictions['editor']['edit_page'] == 0 || $wp_restrictions['editor']['edit_page'] == '' || wp_restrictions_excluded_page(get_the_ID())) {
    248                     return;
    249                 } elseif ($wp_restrictions['editor']['edit_page'] == 1) {
    250                     if (get_the_date() != date("F j, Y")) {
    251                         $caps[] = 'edit_page';
    252                     }
    253                 } else {
    254                     $page_date = get_the_date('F j, Y');
    255                     $num_days = "+" . $wp_restrictions['editor']['edit_page'] . " " . "days";
    256                     $edit_until = strtotime(date("F j, Y", strtotime($page_date)) . " $num_days");
    257 
    258                     if (strtotime("now") > $edit_until) {
    259                         $caps[] = 'edit_page';
    260                     }
    261                 }
    262             }
    263         }
    264     }
    265     return $caps;
    266 }
    267 
    268 function wp_restrictions_admin() {
    269     if (!current_user_can('manage_options')) {
    270         wp_die( __('You do not have sufficient permissions to access this page.') );
    271     }
    272 
    273     if (isset($_POST['wp_restrictions_submit'])) {
    274         $wp_restrictions = array(
    275                 'editor' => array(
    276                     'delete_post' => $_POST['editor_delete_posts'],
    277                     'edit_post' => $_POST['editor_edit_posts'],
    278                     'delete_page' => $_POST['editor_delete_pages'],
    279                     'edit_page' => $_POST['editor_edit_pages'],
    280                     'max_posts' => $_POST['editor_max_posts']
    281                 ),
    282                 'author' => array(
    283                     'delete_post' => $_POST['author_delete_posts'],
    284                     'edit_post' => $_POST['author_edit_posts'],
    285                     'max_posts' => $_POST['author_max_posts']
    286                 ),
    287                 'excluded' => array(
    288                     'user_ids' => $_POST['excluded_user_ids'],
    289                     'post_ids' => $_POST['excluded_post_ids'],
    290                     'page_ids' => $_POST['excluded_page_ids']
    291                 )
    292         );
    293 
    294         foreach($wp_restrictions as $var => $key) {
    295             $wp_restrictions[$var] = preg_replace('/[^0-9,]/', '', $key);
    296         }
    297 
    298         update_option('wp_restrictions', $wp_restrictions);
    299     }
    300 
    301     if (isset($_POST['wp_restrictions_uninstall'])) {
    302         if ($_POST['wp_restrictions_uninstall'] == 'Uninstall') {
    303             wp_restrictions_uninstall();
    304         }
    305     }
    306 
    307     $wp_restrictions = get_option('wp_restrictions');
    308 ?>
    309     <h3>WordPress Restrictions</h3>
    310     <p>I (Brandon Smith) developed WordPress Restrictions to offer an easier and more practicable solution to control what can be edited/deleted on your WordPress Install. While several other plugins already allow you to modify WordPress User Roles and Capabilities, they require that you tweak the default settings of each user role or create a new user role (which can get a bit confusing for the average webmaster). With WordPress Restrictions, you can specify when and what content may be edited by Editors and/or Authors. If you need help or assistance, or would like to submit a feature request, please contact me at btks1995@gmail.com.</p>
    311     <form name="wordpress_restrictions" method="POST" action="">
    312     <h3>Restrictions for Editors</h3>
    313     <label>Delete Posts Timeframe (In Days): </label><input type="text" name="editor_delete_posts" value="<?php echo $wp_restrictions['editor']['delete_post']; ?>" /><span style="margin-left: 5px;">Specify a Number of Days. A post published on 8-16-2011 would be deletable by <strong>EDITORS</strong> until 8-20-2011 if the timeframe was 4 Days.</span><br />
    314     <label>Delete Pages Timeframe (In Days): </label><input type="text" name="editor_delete_pages" value="<?php echo $wp_restrictions['editor']['delete_page']; ?>" /> <span style="margin-left: 5px;">Specify a Number of Days. A page published on 8-16-2011 would be deletable by <strong>EDITORS</strong> until 8-20-2011 if the timeframe was 4 Days.</span><br />
    315     <label>Edit Posts Timeframe (In Days): </label><input type="text" name="editor_edit_posts" value="<?php echo $wp_restrictions['editor']['edit_post']; ?>" /> <span style="margin-left: 5px;">Specify a Number of Days. A post published on 8-16-2011 would be editable by <strong>EDITORS</strong> until 8-20-2011 if the timeframe was 4 Days.</span><br />
    316     <label>Edit Pages Timeframe (In Days): </label><input type="text" name="editor_edit_pages" value="<?php echo $wp_restrictions['editor']['edit_page']; ?>" /> <span style="margin-left: 5px;">Specify a Number of Days. A page published on 8-16-2011 would be deletable by <strong>EDITORS</strong> until 8-20-2011 if the timeframe was 4 Days.</span><br />
    317     <label>Max Number of Posts a Day: </label><input type="text" name="editor_max_posts" value="<?php echo $wp_restrictions['editor']['max_posts']; ?>" /> <span style="margin-left: 5px;">Specify a Number. If you input a number of '5', then each <strong>EDITOR</strong> can make up to 5 posts within 24 hours.</span><br />
    318 
    319     <h3>Restrictions for Authors</h3>
    320     <label>Delete Posts Timeframe (In Days): </label><input type="text" name="author_delete_posts" value="<?php echo $wp_restrictions['author']['delete_post']; ?>" /><span style="margin-left: 5px;">Specify a Number of Days. A post published on 8-16-2011 would be deletable by the <strong>AUTHOR</strong> until 8-20-2011 if the timeframe was 4 Days.</span><br />
    321     <label>Edit Posts Timeframe (In Days): </label><input type="text" name="author_edit_posts" value="<?php echo $wp_restrictions['author']['edit_post']; ?>" /> <span style="margin-left: 5px;">Specify a Number of Days. A post published on 8-16-2011 would be editable by the <strong>AUTHOR</strong> until 8-20-2011 if the timeframe was 4 Days.</span><br />
    322     <label>Max Number of Posts a Day: </label><input type="text" name="author_max_posts" value="<?php echo $wp_restrictions['author']['max_posts']; ?>" /> <span style="margin-left: 5px;">Specify a Number. If you input a number of '5', then each <strong>AUTHOR</strong> can make up to 5 posts within 24 hours.</span><br />
    323 
    324     <h3>Exclude Restrictions</h3>
    325     <label>User IDs (Comma [,] Separated): </label><input type="text" name="excluded_user_ids" value="<?php echo $wp_restrictions['excluded']['user_ids']; ?>" /><span style="margin-left: 5px;">Specify User IDs, <strong>COMMA SEPARATED</strong>. I have provided a list of Users and corresponding IDs for your convenience.</span><br />
    326     <label>Post IDs (Comma [,] Separated): </label><input type="text" name="excluded_post_ids" value="<?php echo $wp_restrictions['excluded']['post_ids']; ?>" /><span style="margin-left: 5px;">Specify User IDs, <strong>COMMA SEPARATED</strong>. I have provided a list of Posts and corresponding IDs for your convenience.</span><br />
    327     <label>Page IDs (Comma [,] Separated): </label><input type="text" name="excluded_page_ids" value="<?php echo $wp_restrictions['excluded']['page_ids']; ?>" /><span style="margin-left: 5px;">Specify User IDs, <strong>COMMA SEPARATED</strong>. I have provided a list of Pages and corresponding IDs for your convenience.</span><br />
    328 
    329     <table style="margin: 10px 0 10px 0; background: #DFDFDF; padding: 20px; width: 100%; ">
    330     <tr>
    331     <th style="text-align: left;">User ID</th>
    332     <th style="text-align: left;">Login Name</th>
    333     <th style="text-align: left;">User Role</th>
    334     <th style="text-align: left;">Display Name</th>
    335     <th style="text-align: left;">Email Address</th>
    336     </tr>
    337     <?php wp_restrictions_listusers(); ?>
    338     </table>
    339 
    340     <input type="submit" name="wp_restrictions_submit" value="Update Options">
    341     <input type="submit" name="wp_restrictions_uninstall" value="Uninstall">
    342 
    343     </form>
    344 <?php }
    345 
    346 function wp_restrictions_menu() {
    347     add_options_page('WordPress Restrictions', 'Restrictions', 8, basename(__FILE__), 'wp_restrictions_admin');
    348 }
    349 
    350 function wp_restrictions_max_posts() {
    351     global $wp_query;
    352     if (WP_REST_ROLE == 'editor' || WP_REST_ROLE == 'author' && !wp_restrictions_excluded_user(WP_REST_UID)) {
    353         $wp_query = new WP_Query(array('author' => WP_REST_UID, 'monthnum' => WP_REST_CURR_MONTH, 'day' => WP_REST_CURR_DAY, 'year' => WP_REST_CURR_YEAR));
    354         while($wp_query->have_posts()) : $wp_query->the_post();
    355             $post_count = $wp_query->post_count;
    356         endwhile;
    357         wp_reset_postdata();
    358 
    359         $wp_restrictions = get_option('wp_restrictions');
    360         $max_posts = $wp_restrictions[WP_REST_ROLE]['max_posts'];
    361 
    362         if ($max_posts != '' && $post_count >= $wp_restrictions[WP_REST_ROLE]['max_posts']) {
    363             remove_submenu_page('edit.php', 'post-new.php');
    364 
    365             if (strpos($_SERVER['REQUEST_URI'], 'post-new.php')) {
    366                     wp_die("You're only allowed to publish $max_posts posts within 24 hours. Please try again tomorrow.");
    367             }
    368         }
    369     }
    370 }
    371 
    372 function wp_restrictions_uninstall() {
    373     delete_option('wp_restrictions');
    374     echo '<div style="background-color: lightYellow; border: 1px solid #E6DB55; margin: 10px 10px 10px 0; padding: 6px;">WordPress Restrictions Options have been removed from your WordPress Install. If you\'d like to completely remove WordPress Restrictions, please do so through the Plugin Admin.</div>';
    375 }
    376 
    377 // WordPress Restrictions Actions
    378 add_action('admin_init', 'wp_restrictions');
    379 add_action('admin_menu', 'wp_restrictions_menu');
    380 
    381 // WordPress Restrictions Filters
    382 add_filter('map_meta_cap', 'wp_restrictions_mmc', 10, 4);
     23add_action('admin_init', 'wp_restrictions::load'); // Load Admin Actions
     24add_action('admin_menu', 'wp_rest_admin::menu'); // Load Restrictions Menu
     25add_filter('map_meta_cap', 'wp_restrictions::mmc', 10, 4); // Filter MMC
    38326
    38427?>
Note: See TracChangeset for help on using the changeset viewer.