C:\xampp\htdocs\landing\wp-content\plugins\duplicate-post\src\admin\class-options-page.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
 * Duplicate Post plugin file.
 *
 * @package Yoast\WP\Duplicate_Post\Admin
 */

namespace Yoast\WP\Duplicate_Post\Admin;

use 
Yoast\WP\Duplicate_Post\UI\Asset_Manager;
use 
Yoast\WP\Duplicate_Post\Utils;

/**
 * Class Options_Page
 */
class Options_Page {
    
/**
     * The Options instance.
     *
     * @var Options
     */
    
protected $options;

    
/**
     * The Options_Form_Generator instance.
     *
     * @var Options_Form_Generator
     */
    
protected $generator;

    
/**
     * Holds the asset manager.
     *
     * @var Asset_Manager
     */
    
protected $asset_manager;

    
/**
     * Options_Page constructor.
     *
     * @param Options                $options       The Options class instance.
     * @param Options_Form_Generator $generator     The Options_Form_Generator class instance.
     * @param Asset_Manager          $asset_manager The Asset_Manager class instance.
     */
    
public function __constructOptions $optionsOptions_Form_Generator $generatorAsset_Manager $asset_manager ) {
        
$this->options       $options;
        
$this->generator     $generator;
        
$this->asset_manager $asset_manager;
    }

    
/**
     * Registers the necessary hooks.
     *
     * @return void
     */
    
public function register_hooks() {
        if ( \
is_admin() ) {
            \
add_action'admin_menu', [ $this'register_menu' ] );
            \
add_action'admin_init', [ $this->options'register_settings' ] );
        }
    }

    
/**
     * Enqueues the assets.
     *
     * @return void
     */
    
public function enqueue_assets() {
        
$this->asset_manager->enqueue_options_styles();
        
$this->asset_manager->enqueue_options_script();
    }

    
/**
     * Registers the menu item.
     *
     * @return void
     */
    
public function register_menu() {
        
$page_hook = \add_options_page(
            \
__'Duplicate Post Options''duplicate-post' ),
            \
__'Duplicate Post''duplicate-post' ),
            
'manage_options',
            
'duplicatepost',
            [ 
$this'generate_page' ]
        );

        \
add_action$page_hook, [ $this'enqueue_assets' ] );
    }

    
/**
     * Generates the inputs for the specified tab / fieldset.
     *
     * @param string $tab      The tab to get the configuration for.
     * @param string $fieldset The fieldset to get the configuration for. Optional.
     *
     * @return string The HTML output for the controls present on the tab / fieldset.
     * @codeCoverageIgnore As this is a simple wrapper for two functions that are already tested elsewhere, we can skip testing.
     */
    
public function generate_tab_inputs$tab$fieldset '' ) {
        
$options $this->options->get_options_for_tab$tab$fieldset );

        return 
$this->generator->generate_options_input$options );
    }

    
/**
     * Generates an input for a single option.
     *
     * @param string $option The option configuration to base the input on.
     *
     * @return string The input HTML.
     * @codeCoverageIgnore As this is a simple wrapper for two functions that are already tested elsewhere, we can skip testing.
     */
    
public function generate_input$option ) {
        return 
$this->generator->generate_options_input$this->options->get_option$option ) );
    }

    
/**
     * Registers the proper capabilities.
     *
     * @return void
     */
    
public function register_capabilities() {
        if ( ! \
current_user_can'promote_users' ) || ! $this->settings_updated() ) {
            return;
        }

        
$roles $this->get_duplicate_post_roles();

        foreach ( 
Utils::get_roles() as $name => $display_name ) {
            
$role = \get_role$name );

            if ( ! 
$role->has_cap'copy_posts' ) && \in_array$name$rolestrue ) ) {
                
/* If the role doesn't have the capability and it was selected, add it. */
                
$role->add_cap'copy_posts' );
            }

            if ( 
$role->has_cap'copy_posts' ) && ! \in_array$name$rolestrue ) ) {
                
/* If the role has the capability and it wasn't selected, remove it. */
                
$role->remove_cap'copy_posts' );
            }
        }
    }

    
/**
     * Generates the options page.
     *
     * @return void
     * @codeCoverageIgnore
     */
    
public function generate_page() {
        
$this->register_capabilities();

        require_once 
DUPLICATE_POST_PATH 'src/admin/views/options.php';
    }

    
/**
     * Checks whether settings have been updated.
     *
     * @return bool Whether or not the settings have been updated.
     */
    
protected function settings_updated() {
        return isset( 
$_GET['settings-updated'] ) && $_GET['settings-updated'] === 'true'// phpcs:ignore WordPress.Security.NonceVerification
    
}

    
/**
     * Gets the registered custom roles.
     *
     * @return array The roles. Returns an empty array if there are none.
     */
    
protected function get_duplicate_post_roles() {
        
$roles = \get_option'duplicate_post_roles' );

        if ( empty( 
$roles ) ) {
            
$roles = [];
        }

        return 
$roles;
    }
}
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586