Plugin Directory

Changeset 1709117


Ignore:
Timestamp:
08/06/2017 12:33:39 PM (9 years ago)
Author:
dangub86
Message:

Added Target Groups feature and Display only in dashboard and other code optimizations - class admin

File:
1 edited

Legend:

Unmodified
Added
Removed
  • admin-notices-for-team/trunk/admin/class-ant_admin_notices_team-admin.php

    r1692756 r1709117  
    159159            'query_var'          => true,
    160160            'rewrite'            => array( 'slug' => 'notices' ),
    161             'capability_type'    => 'post',
     161            'capability_type'    => 'notice',
     162            'map_meta_cap'  => true,
    162163            'has_archive'        => true,
    163164            'hierarchical'       => false,
     
    173174        );
    174175
     176
     177    }
     178
     179    public function add_users_caps() {
     180
     181        // gets the Author role
     182        $author = get_role( 'author' );
     183
     184        // Allow Authors to
     185        $author->add_cap( 'read_private_notices' );
     186        $author->add_cap('publish_notices');
     187        $author->add_cap('delete_notices');
     188        $author->add_cap('edit_notices');
     189        //$author->remove_cap('edit_publi_notices');
     190
     191        // gets the Editor role
     192        $editor = get_role( 'editor' );
     193
     194        // Allow Editors to
     195        $editor->add_cap( 'read_private_notices' );
     196        $editor->add_cap( 'edit_private_notices' );
     197        $editor->add_cap('delete_notices');
     198        $editor->add_cap('edit_notices');
     199        $editor->add_cap('publish_notices');
     200        $editor->add_cap('edit_published_notices');
     201        $editor->add_cap('edit_others_notices');
    175202
    176203    }
     
    185212        }
    186213    }
     214
     215
     216
     217    /**
     218     * Retrieve Users by Role
     219     */
     220    public function get_users_by_role() {
     221
     222        //ARGS
     223        $argsAdmin = array(
     224            'role'         => 'Administrator',
     225            'orderby'     => 'display_name',
     226            'order' => 'ASC'
     227            //'role__in'     => array(),
     228        );
     229        $argsEditor = array(
     230            'role'         => 'Editor',
     231            'orderby'     => 'display_name',
     232            'order' => 'ASC'
     233        );
     234        $argsAuthor = array(
     235            'role'         => 'Author',
     236            'orderby'     => 'display_name',
     237            'order' => 'ASC'
     238        );
     239        $argsContributor = array(
     240            'role'         => 'Contributor',
     241            'orderby'     => 'display_name',
     242            'order' => 'ASC'
     243        );
     244
     245        $adminGroup = get_users($argsAdmin);
     246        $editorGroup = get_users($argsEditor);
     247        $authorGroup = get_users($argsAuthor);
     248        $contributorGroup = get_users($argsContributor);
     249
     250
     251        foreach ($adminGroup as $admin) {
     252            $admins[] = $admin->display_name;
     253            update_option('admin_groups', $admins );
     254        }
     255        foreach ($editorGroup as $editor) {
     256            $editors[] = $editor->display_name;
     257            update_option('editor_groups', $editors );
     258        }
     259        foreach ($authorGroup as $author) {
     260            $authors[] = $author->display_name;
     261            update_option('author_groups', $authors );
     262        }
     263        foreach ($contributorGroup as $contributor) {
     264            $contributors[] = $contributor->display_name;
     265            update_option('contributor_groups', $contributors );
     266        }
     267
     268        $groups     = array(
     269            'Admin Users' => get_option('admin_groups'),
     270            'Editors' => get_option('editor_groups'),
     271            'Authors' => get_option('author_groups'),
     272            'Contributors' => get_option('contributor_groups')
     273        );
     274        update_option( 'ant_groups', $groups );
     275
     276    }
     277
    187278
    188279
     
    219310        $value        = get_post_meta( $post->ID, '_target_author_key', true );
    220311        $value_groups = get_post_meta( $post->ID, '_target_author_groups', true );
     312        $value_custom_groups = get_post_meta( $post->ID, '_author_groups', true );
    221313        $value_date   = get_post_meta( $post->ID, '_notices_expire_date', true );
    222314
    223315        // Display the form, using the current value.
    224316        $options = get_option( $this->plugin_name . '-settings' );
    225 
    226         $args  = array(
    227             'role__in' => array(
    228                 'administrator',
    229                 'author',
    230                 'editor',
    231                 'contributor'
    232             )
    233         );
    234         $users = get_users( $args );
    235 
    236 
    237         //$groups     = array();
    238         //$group_name = $_POST['group_name'];
    239 
    240 
    241         //$groups[] = $group_name;
     317        $users = get_users();
     318
     319        $groups     = array(
     320            'Admin Users' => get_option('admin_groups'),
     321            'Editors' => get_option('editor_groups'),
     322            'Authors' => get_option('author_groups'),
     323            'Contributors' => get_option('contributor_groups')
     324        );
     325        update_option( 'ant_groups', $groups );
     326
     327        $groups = get_option('ant_groups');
    242328
    243329
     
    251337                <option value="">All Users</option>
    252338                <?php foreach ( $users as $user ) { ?>
    253                     <option value="<?php echo $user->user_nicename; ?>"<?php selected( $user->user_nicename, $value, true ); ?>><?php echo $user->user_nicename; ?></option>
     339                    <option value="<?php echo $user->display_name; ?>"<?php selected( $user->display_name, $value, true ); ?>><?php echo $user->display_name; ?></option>
    254340                <?php } ?>
    255341            </select><br>
    256342            <?php
     343
    257344        }
    258345
    259346        // Groups Dropdown Form and Add Group
    260347        if ( $options['toggle-target-author-groups'] == 'enabled' ) {
    261             ?>
    262             <label for="target_author_field">
    263                 <?php _e( 'Target Author\'s Groups', 'your-plugin-textdomain' ); ?>
    264             </label>
    265             <select name="target_author_groups">
    266                 <option value="">No Groups</option>
    267                 <?php foreach ( $groups as $group ) { ?>
    268                     <option value="<?php echo $group; ?>"<?php selected( $group, $value_groups, true ); ?>><?php echo $group; ?></option>
    269                 <?php } ?>
    270             </select><br>
    271             <a href="#" id="add_group">Add Group</a><br>
    272             <label for="group_name" id="groupForm" class="meta_hidden">
    273                 <?php _e( 'Group Name', 'your-plugin-textdomain' ); ?>
    274             </label>
    275             <input type="text" name="group_name" id="group_name" class="meta_hidden" value=""><br>
    276             <label for="add_to_group" class="meta_hidden">
    277                 <?php _e( 'Add Authors to Group', 'your-plugin-textdomain' ); ?>
    278             </label>
    279             <input type="text" name="add_to_group" id="add_to_group" class="meta_hidden"><br>
    280             <?php
    281         }
     348
     349            if ( in_array("", $value_custom_groups) ) {
     350                ?>
     351                <label for="target_author_field" id="target_label">
     352                    <?php _e( 'Target Author\'s Groups', 'your-plugin-textdomain' ); ?>
     353                </label>
     354                <select name="target_author_groups" id="target_groups">
     355
     356                    <option value="">No Groups Selected</option>
     357                    <?php foreach ($groups as $name => $val) { ?>
     358                        <option value="<?php echo $name; ?>"<?php selected( $name, $value_groups, true ); ?>><?php echo $name;?></option>
     359                    <?php } ?>
     360
     361                </select><br>
     362                <a href="#" id="return_group">Choose one of the deafult Groups</a><br>
     363                <a href="#" id="add_group">Are you looking for custom Groups?</a><br>
     364
     365                <label for="add_to_group" class="meta_hidden">
     366                    <?php _e( 'Select Users', 'your-plugin-textdomain' ); ?>
     367                </label>
     368                <select multiple="multiple" name="add_to_group[]" id="add_to_group" class="meta_hidden">
     369                    <option value="">No Users Selected</option>
     370                    <?php foreach ( $users as $user ) { ?>
     371                        <option value="<?php echo $user->display_name; ?>"<?php selected( $user->display_name, $value_custom_groups, true ); ?>><?php echo $user->display_name; ?></option>
     372                    <?php } ?>
     373                </select><br>
     374
     375                <?php } else { ?>
     376
     377                <label for="target_author_field" id="target_label_alt">
     378                    <?php _e( 'Target Author\'s Groups', 'your-plugin-textdomain' ); ?>
     379                </label>
     380                <select name="target_author_groups" id="target_groups_alt">
     381
     382                    <option value="">No Groups Selected</option>
     383                    <?php foreach ($groups as $name => $val) { ?>
     384                        <option value="<?php echo $name; ?>"<?php selected( $name, $value_groups, true ); ?>><?php echo $name;?></option>
     385                    <?php } ?>
     386
     387                </select><br>
     388            <a href="#" id="return_group_alt">Choose one of the deafult Groups</a><br>
     389            <a href="#" id="add_group_alt">Are you looking for custom Groups?</a><br>
     390
     391                <label for="add_to_group" class="meta_hidden_alt">
     392                    <?php _e( 'Select Users', 'your-plugin-textdomain' ); ?>
     393                </label>
     394                <select multiple="multiple" name="add_to_group[]" id="add_to_group" class="meta_hidden_alt">
     395                    <option value="">No Users Selected</option>
     396                    <?php foreach ( $users as $user ) { ?>
     397                        <option value="<?php echo $user->display_name; ?>"<?php selected( $user->display_name, $value_custom_groups, true ); ?>><?php echo $user->display_name; ?></option>
     398                    <?php } ?>
     399                </select><br>
     400            <div class="meta_hidden_alt author_tags_container">
     401                <?php
     402                foreach ($value_custom_groups as $k => $v) {
     403                    //echo implode( ", ", $value_custom_groups );
     404                    ?>
     405                    <div class="author_tags">
     406                    <?php
     407                    echo $v;
     408                    ?>
     409                    </div><br>
     410                    <?php
     411                }
     412                ?>
     413            </div><br>
     414                <?php } ?>
     415            <?php
     416
     417        }
     418
    282419        // Expiration Date Form
    283420        ?>
     
    285422            <?php _e( 'Set Expiration Date', 'your-plugin-textdomain' ); ?>
    286423        </label>
    287         <input type="text" name="expiration_date" id="ant_expires" class="ant-date-picker" value="<?php echo $value_date; ?>">
     424        <input type="text" name="expiration_date" id="ant_expires" class="ant-date-picker" value="<?php echo $value_date; ?>"><br>
    288425        <?php
    289 
    290 
    291         // now get selected user id from $_POST to use in your next function
    292         //$user_id  = $_POST['target_author_field'];
    293         //$group_id = $_POST['target_author_groups'];
    294         //$date_id  = $_POST['expiration_date'];
    295 
    296         // get user data
    297         //$user_data        = get_user_by( 'id', $user_id );
    298         //$user_data_groups = get_user_by( 'id', $group_id );
    299         //$date_data        = get_user_by( 'id', $date_id );
    300 
    301         //echo $user_data->display_name;
    302         //echo $user_data->user_nicename;
    303         //echo $user_data->user_email;
    304 
    305         // do your stuff using $user_id
    306426
    307427    }
     
    354474        // Sanitize the user input.
    355475        $mydata     = esc_attr__( $_POST['target_author_field'] );
    356         $groupsdata = esc_attr__( $_POST['target_author_groups'] );
     476
     477
     478        $groupsData = esc_attr__( $_POST['target_author_groups'] );
     479        //$groupsName = esc_attr__( $_POST['group_name'] );
     480        //$groupsAuthors = esc_attr__( $_POST['add_to_group'] );
     481
    357482        $expiredata = esc_attr__( $_POST['expiration_date'] );
    358         // Update the meta field.
     483
     484        if ( isset( $_POST['add_to_group'] ) ) {
     485
     486            //$groupsAuthors = array();
     487
     488            $groupsAuthors = (array) $_POST['add_to_group'];
     489
     490            update_post_meta( $post_id, '_author_groups', $groupsAuthors );
     491        }
     492
     493            // Update the meta field.
    359494        update_post_meta( $post_id, '_target_author_key', $mydata );
    360         update_post_meta( $post_id, '_target_author_groups', $groupsdata );
     495        update_post_meta( $post_id, '_target_author_groups', $groupsData );
     496        //update_post_meta( $post_id, '_author_groups', $groupsAuthors );
    361497        update_post_meta( $post_id, '_notices_expire_date', $expiredata );
    362498    }
     
    443579        add_settings_field(
    444580            'notice-number',
    445             __( '<div class="woo_box_title">Notice Number</div>', 'your-plugin-textdomain' ),
     581            __( '<div class="woo_box_title">Notices Number</div>', 'your-plugin-textdomain' ),
    446582            array( $this, 'sandbox_add_settings_field_input_text' ),
    447583            $this->plugin_name . '-settings&tab=general',
     
    450586                'label_for'   => __( 'notice-number', 'your-plugin-textdomain' ),
    451587                'description' => __( 'Set the number of Notices you want to display', 'your-plugin-textdomain' )
     588            )
     589        );
     590        add_settings_field(
     591            'display-dashboard-only',
     592            __( '<div class="woo_box_title">Display Notices only in Dashboard</div>', 'your-plugin-textdomain' ),
     593            array( $this, 'sandbox_add_settings_field_single_checkbox' ),
     594            $this->plugin_name . '-settings&tab=general',
     595            $this->plugin_name . '-general-section',
     596            array(
     597                'label_for'   => __( 'display-dashboard-only', 'your-plugin-textdomain' ),
     598                'description' => __( 'If enabled, Notices will be displayed only when in Dashboard and in its subpages.', 'your-plugin-textdomain' )
    452599            )
    453600        );
     
    489636            )
    490637        );
    491         /* !Future update function
    492          * add_settings_field(
     638         add_settings_field(
    493639            'toggle-target-author-groups',
    494640            __( '<div class="woo_box_title">LET targetting Author\'s Groups</div>', 'your-plugin-textdomain' ),
     
    500646                'description' => __( 'Enable a meta box in Notice Custom Post for targetting specific author\'s groups', 'your-plugin-textdomain' )
    501647            )
    502         );*/
     648        );
    503649
    504650        // DESIGN SETTINGS FIELDS
     
    537683            array(
    538684                'label_for'   => __( 'notice-type', 'your-plugin-textdomain' ),
    539                 'description' => __( '', 'your-plugin-textdomain' ),
     685                'description' => __( 'Choose the Type of Notices', 'your-plugin-textdomain' ),
    540686                'option_one'  => __( 'Thin', 'your-plugin-textdomain' ),
    541687                'option_two'  => __( 'Large ', 'your-plugin-textdomain' )
     
    655801            $options_array['notice-number'] = sanitize_text_field( $input['notice-number'] );
    656802        }
    657         if ( isset( $input['let-create'] ) ) {
     803        /*if ( isset( $input['let-create'] ) ) {
    658804            $options_array['let-create'] = filter_var( $input['let-create'], FILTER_SANITIZE_NUMBER_INT );
    659805        }
    660806        if ( isset( $input['let-edit'] ) ) {
    661807            $options_array['let-edit'] = filter_var( $input['let-edit'], FILTER_SANITIZE_NUMBER_INT );
    662         }
    663 
    664         /*if ( isset( $input['toggle-target-authors'] ) ) {
    665             $options_array['toggle-target-authors'] = filter_var( $input['toggle-target-authors'], FILTER_SANITIZE_NUMBER_INT );
    666         } else $options_array['toggle-target-authors'] = 0;
    667 
    668 
    669         if ( isset( $input['toggle-target-author-groups'] ) ) {
    670             $options_array['toggle-target-author-groups'] = filter_var( $input['toggle-target-author-groups'], FILTER_SANITIZE_NUMBER_INT );
    671         } else $options_array['toggle-target-author-groups'] = 0;*/
     808        }*/
     809
     810        if ( isset( $input['display-dashboard-only'] ) ) {
     811            $options_array['display-dashboard-only'] = esc_attr( $input['display-dashboard-only'] );
     812        } else {
     813            $options_array['display-dashboard-only'] = 'disabled';
     814        }
     815
    672816        if ( isset( $input['toggle-target-authors'] ) ) {
    673817            $options_array['toggle-target-authors'] = esc_attr( $input['toggle-target-authors'] );
     
    675819            $options_array['toggle-target-authors'] = 'disabled';
    676820        }
    677 
    678821
    679822        if ( isset( $input['toggle-target-author-groups'] ) ) {
     
    12821425    public function notices_showing( ) {
    12831426        global $current_user;
     1427        global $pagenow;
     1428
    12841429        $options        = get_option( $this->plugin_name . '-settings' );
    12851430        $options_design = get_option( $this->plugin_name . '-design-settings' );
     
    13021447        } else if ( $options_design['notice-style'] == 5 ) {
    13031448            $noticeClasses[] = 'notice-error';
    1304 
    1305         }
    1306 
    1307 
    1308         if ( ! current_user_can( 'read' ) ) {
     1449        }
     1450
     1451        /**
     1452         * Since 1.0.2
     1453         * Show Notices only when in Dashboard or in Dashboard subpages (index.php)
     1454         * */
     1455        if ( ($options['display-dashboard-only'] == 'enabled') && ($pagenow != 'index.php') ) {
     1456            $noticeClasses[] = 'hidden';
     1457        }
     1458
     1459
     1460        if ( ! current_user_can( 'read_private_notices' ) ) {
    13091461            return false;
    13101462        }
     
    13191471        $query = new WP_Query( $args );
    13201472
     1473        /**
     1474         * Since 1.0.2
     1475         * Show Notices targetting based on default roles groups
     1476         * */
     1477        //ARGS
     1478        $argsAdmin = array(
     1479            'role'         => 'Administrator',
     1480            'orderby'     => 'display_name',
     1481            'order' => 'ASC'
     1482            //'role__in'     => array(),
     1483        );
     1484        $argsEditor = array(
     1485            'role'         => 'Editor',
     1486            'orderby'     => 'display_name',
     1487            'order' => 'ASC'
     1488        );
     1489        $argsAuthor = array(
     1490            'role'         => 'Author',
     1491            'orderby'     => 'display_name',
     1492            'order' => 'ASC'
     1493        );
     1494        $argsContributor = array(
     1495            'role'         => 'Contributor',
     1496            'orderby'     => 'display_name',
     1497            'order' => 'ASC'
     1498        );
     1499
     1500        $adminGroup = get_users($argsAdmin);
     1501        $editorGroup = get_users($argsEditor);
     1502        $authorGroup = get_users($argsAuthor);
     1503        $contributorGroup = get_users($argsContributor);
     1504
    13211505        ?>
    13221506
     
    13321516                $expires = get_post_meta( $notice_id, '_notices_expire_date', true );
    13331517                $authorOptions = get_post_meta( $notice->ID, '_target_author_key', true );
     1518                $authorOptionGroups = get_post_meta( $notice->ID, '_target_author_groups', true );
     1519                $authorCustomGroups = get_post_meta( $notice->ID, '_author_groups', true );
     1520                if (
     1521                    $authorOptionGroups == 'Admin Users'
     1522                ) {
     1523                    foreach ($adminGroup as $admin) {
     1524                        $admins[]           = $admin->display_name;
     1525                        $authorOptionGroups = $admins;
     1526                    }
     1527                } elseif ( $authorOptionGroups == 'Editors' ) {
     1528                    foreach ($editorGroup as $editor) {
     1529                        $editors[] = $editor->display_name;
     1530                        $authorOptionGroups = $editors;
     1531                    }
     1532                } elseif ( $authorOptionGroups == 'Authors' ) {
     1533                    foreach ($authorGroup as $author) {
     1534                        $authors[]          = $author->display_name;
     1535                        $authorOptionGroups = $authors;
     1536                    }
     1537                }  elseif ( $authorOptionGroups == 'Contributors' ) {
     1538                    foreach ($contributorGroup as $contributor) {
     1539                        $contributors[]     = $contributor->display_name;
     1540                        $authorOptionGroups = $contributors;
     1541                    }
     1542                }
    13341543                $ant_dismiss = get_user_option("ant-dismiss-$notice_id", $user_id);
    13351544
    13361545                if (
    1337                         ($authorOptions == '' || $authorOptions == $current_user->user_login)
    1338                         && $ant_dismiss != "dismissed"
    1339                         && ( $current_time < $expires || empty($expires) )
     1546                 ($authorOptions == '' && $authorOptionGroups == '' && in_array("", $authorCustomGroups)) ||
     1547                 ($authorOptions == $current_user->display_name) ||
     1548                 ( isset($authorOptionGroups) && in_array($current_user->display_name, $authorOptionGroups) ) ||
     1549                 ( isset($authorCustomGroups) && in_array($current_user->display_name, $authorCustomGroups) ) &&
     1550                 $ant_dismiss != "dismissed" &&
     1551                 ( $current_time < $expires || empty($expires) )
    13401552                ) {
    13411553                        ?>
Note: See TracChangeset for help on using the changeset viewer.