C:\xampp\htdocs\landing\wp-content\plugins\duplicate-post\src\ui\class-classic-editor.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
/**
 * Duplicate Post class to manage the classic editor UI.
 *
 * @package Duplicate_Post
 */

namespace Yoast\WP\Duplicate_Post\UI;

use 
WP_Post;
use 
Yoast\WP\Duplicate_Post\Permissions_Helper;
use 
Yoast\WP\Duplicate_Post\Utils;

/**
 * Represents the Classic_Editor class.
 */
class Classic_Editor {

    
/**
     * Holds the object to create the action link to duplicate.
     *
     * @var Link_Builder
     */
    
protected $link_builder;

    
/**
     * Holds the permissions helper.
     *
     * @var Permissions_Helper
     */
    
protected $permissions_helper;

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

    
/**
     * Initializes the class.
     *
     * @param Link_Builder       $link_builder       The link builder.
     * @param Permissions_Helper $permissions_helper The permissions helper.
     * @param Asset_Manager      $asset_manager      The asset manager.
     */
    
public function __constructLink_Builder $link_builderPermissions_Helper $permissions_helperAsset_Manager $asset_manager ) {
        
$this->link_builder       $link_builder;
        
$this->permissions_helper $permissions_helper;
        
$this->asset_manager      $asset_manager;
    }

    
/**
     * Adds hooks to integrate with WordPress.
     *
     * @return void
     */
    
public function register_hooks() {
        \
add_action'post_submitbox_misc_actions', [ $this'add_check_changes_link' ], 90 );

        if ( \
intvalUtils::get_option'duplicate_post_show_link_in''submitbox' ) ) === ) {
            if ( \
intvalUtils::get_option'duplicate_post_show_link''new_draft' ) ) === ) {
                \
add_action'post_submitbox_start', [ $this'add_new_draft_post_button' ] );
            }

            if ( \
intvalUtils::get_option'duplicate_post_show_link''rewrite_republish' ) ) === ) {
                \
add_action'post_submitbox_start', [ $this'add_rewrite_and_republish_post_button' ] );
            }
        }

        \
add_filter'gettext', [ $this'change_republish_strings_classic_editor' ], 10);
        \
add_filter'gettext_with_context', [ $this'change_schedule_strings_classic_editor' ], 10);
        \
add_filter'post_updated_messages', [ $this'change_scheduled_notice_classic_editor' ], 10);

        \
add_action'admin_enqueue_scripts', [ $this'enqueue_classic_editor_scripts' ] );
        \
add_action'admin_enqueue_scripts', [ $this'enqueue_classic_editor_styles' ] );

        
// Remove slug editing from Classic Editor.
        
\add_action'add_meta_boxes', [ $this'remove_slug_meta_box' ], 10);
        \
add_filter'get_sample_permalink_html', [ $this'remove_sample_permalink_slug_editor' ], 10);
    }

    
/**
     * Enqueues the necessary JavaScript code for the Classic editor.
     *
     * @return void
     */
    
public function enqueue_classic_editor_scripts() {
        if ( 
$this->permissions_helper->is_classic_editor() && isset( $_GET['post'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
            
$id   = \intval( \wp_unslash$_GET['post'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
            
$post = \get_post$id );

            if ( ! \
is_null$post ) && $this->permissions_helper->is_rewrite_and_republish_copy$post ) ) {
                
$this->asset_manager->enqueue_strings_script();
            }
        }
    }

    
/**
     * Enqueues the necessary styles for the Classic editor.
     *
     * @return void
     */
    
public function enqueue_classic_editor_styles() {
        if ( 
$this->permissions_helper->is_classic_editor()
            && isset( 
$_GET['post'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
            
$id   = \intval( \wp_unslash$_GET['post'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
            
$post = \get_post$id );

            if ( ! \
is_null$post ) && $this->permissions_helper->is_rewrite_and_republish_copy$post ) ) {
                
$this->asset_manager->enqueue_styles();
            }
        }
    }

    
/**
     * Adds a button in the post/page edit screen to create a clone
     *
     * @param \WP_Post|null $post The post object that's being edited.
     *
     * @return void
     */
    
public function add_new_draft_post_button$post null ) {
        if ( \
is_null$post ) ) {
            if ( isset( 
$_GET['post'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
                
$id   = \intval( \wp_unslash$_GET['post'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
                
$post = \get_post$id );
            }
        }

        if ( 
$post instanceof WP_Post && $this->permissions_helper->should_links_be_displayed$post ) ) {
            
?>
            <div id="duplicate-action">
                <a class="submitduplicate duplication"
                    href="<?php echo \esc_url$this->link_builder->build_new_draft_link$post ) ); ?>"><?php \esc_html_e'Copy to a new draft''duplicate-post' ); ?>
                </a>
            </div>
            <?php
        
}
    }

    
/**
     * Adds a button in the post/page edit screen to create a clone for Rewrite & Republish.
     *
     * @param \WP_Post|null $post The post object that's being edited.
     *
     * @return void
     */
    
public function add_rewrite_and_republish_post_button$post null ) {
        if ( \
is_null$post ) ) {
            if ( isset( 
$_GET['post'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
                
$id   = \intval( \wp_unslash$_GET['post'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
                
$post = \get_post$id );
            }
        }

        if (
            
$post instanceof WP_Post
            
&& $this->permissions_helper->should_rewrite_and_republish_be_allowed$post )
            && 
$this->permissions_helper->should_links_be_displayed$post )
        ) {
            
?>
            <div id="rewrite-republish-action">
                <a class="submitduplicate duplication" href="<?php echo \esc_url$this->link_builder->build_rewrite_and_republish_link$post ) ); ?>"><?php \esc_html_e'Rewrite & Republish''duplicate-post' ); ?>
                </a>
            </div>
            <?php
        
}
    }

    
/**
     * Adds a message in the post/page edit screen to create a clone for Rewrite & Republish.
     *
     * @param \WP_Post|null $post The post object that's being edited.
     *
     * @return void
     */
    
public function add_check_changes_link$post null ) {
        if ( \
is_null$post ) ) {
            if ( isset( 
$_GET['post'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
                
$id   = \intval( \wp_unslash$_GET['post'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
                
$post = \get_post$id );
            }
        }

        if ( 
$post instanceof WP_Post && $this->permissions_helper->is_rewrite_and_republish_copy$post ) ) {
            
?>
                <div id="check-changes-action">
                    <?php \esc_html_e'Do you want to compare your changes with the original version before merging? Please save any changes first.''duplicate-post' ); ?>
                    <br><br>
                    <a class='button' href=<?php echo \esc_url$this->link_builder->build_check_link$post ) ); ?>>
                        <?php \esc_html_e'Compare''duplicate-post' ); ?>
                    </a>
                </div>
                <?php
        
}
    }

    
/**
     * Changes the 'Publish' copies in the submitbox to 'Republish' if a post is intended for republishing.
     *
     * @param string $translation The translated text.
     * @param string $text        The text to translate.
     *
     * @return string The to-be-used copy of the text.
     */
    
public function change_republish_strings_classic_editor$translation$text ) {
        if ( 
$this->should_change_rewrite_republish_copy( \get_post() ) ) {
            if ( 
$text === 'Publish' ) {
                return \
__'Republish''duplicate-post' );
            }

            if ( 
$text === 'Publish on: %s' ) {
                
/* translators: %s: Date on which the post is to be republished. */
                
return \__'Republish on: %s''duplicate-post' );
            }
        }

        return 
$translation;
    }

    
/**
     * Changes the 'Schedule' copy in the submitbox to 'Schedule republish' if a post is intended for republishing.
     *
     * @param string $translation The translated text.
     * @param string $text        The text to translate.
     *
     * @return string The to-be-used copy of the text.
     */
    
public function change_schedule_strings_classic_editor$translation$text ) {
        if ( 
$this->should_change_rewrite_republish_copy( \get_post() ) && $text === 'Schedule' ) {
            return \
__'Schedule republish''duplicate-post' );
        }

        return 
$translation;
    }

    
/**
     * Changes the post-scheduled notice when a post or page intended for republishing is scheduled.
     *
     * @param array[] $messages Post updated messaged.
     *
     * @return array[] The to-be-used messages.
     */
    
public function change_scheduled_notice_classic_editor$messages ) {
        
$post = \get_post();
        if ( ! 
$this->should_change_rewrite_republish_copy$post ) ) {
            return 
$messages;
        }

        
$permalink      = \get_permalink$post->ID );
        
$scheduled_date = \get_the_time( \get_option'date_format' ), $post );
        
$scheduled_time = \get_the_time( \get_option'time_format' ), $post );

        if ( 
$post->post_type === 'post' ) {
            
$messages['post'][9] = \sprintf(
            
/* translators: 1: The post title with a link to the frontend page, 2: The scheduled date and time. */
                
\esc_html__(
                    
'This rewritten post %1$s is now scheduled to replace the original post. It will be published on %2$s.',
                    
'duplicate-post'
                
),
                
'<a href="' $permalink '">' $post->post_title '</a>',
                
'<strong>' $scheduled_date ' ' $scheduled_time '</strong>'
            
);
            return 
$messages;
        }

        if ( 
$post->post_type === 'page' ) {
            
$messages['page'][9] = \sprintf(
                    
/* translators: 1: The page title with a link to the frontend page, 2: The scheduled date and time. */
                
\esc_html__(
                    
'This rewritten page %1$s is now scheduled to replace the original page. It will be published on %2$s.',
                    
'duplicate-post'
                
),
                
'<a href="' $permalink '">' $post->post_title '</a>',
                
'<strong>' $scheduled_date ' ' $scheduled_time '</strong>'
            
);
        }

        return 
$messages;
    }

    
/**
     * Determines if the Rewrite & Republish copies for the post should be used.
     *
     * @param \WP_Post $post The current post object.
     *
     * @return bool True if the Rewrite & Republish copies should be used.
     */
    
public function should_change_rewrite_republish_copy$post ) {
        global 
$pagenow;
        if ( ! \
in_array$pagenow, [ 'post.php''post-new.php' ], true ) ) {
            return 
false;
        }

        if ( ! 
$post instanceof WP_Post ) {
            return 
false;
        }

        return 
$this->permissions_helper->is_rewrite_and_republish_copy$post );
    }

    
/**
     * Removes the slug meta box in the Classic Editor when the post is a Rewrite & Republish copy.
     *
     * @param string   $post_type Post type.
     * @param \WP_Post $post      Post object.
     *
     * @return void
     */
    
public function remove_slug_meta_box$post_type$post ) {
        if ( 
$post instanceof WP_Post && $this->permissions_helper->is_rewrite_and_republish_copy$post ) ) {
            \
remove_meta_box'slugdiv'$post_type'normal' );
        }
    }

    
/**
     * Removes the sample permalink slug editor in the Classic Editor when the post is a Rewrite & Republish copy.
     *
     * @param string   $return    Sample permalink HTML markup.
     * @param int      $post_id   Post ID.
     * @param string   $new_title New sample permalink title.
     * @param string   $new_slug  New sample permalink slug.
     * @param \WP_Post $post      Post object.
     *
     * @return string The filtered HTML of the sample permalink slug editor.
     */
    
public function remove_sample_permalink_slug_editor$return$post_id$new_title$new_slug$post ) {
        if ( ! 
$post instanceof WP_Post ) {
            return 
$return;
        }

        if ( 
$this->permissions_helper->is_rewrite_and_republish_copy$post ) ) {
            return 
'';
        }

        return 
$return;
    }
}
x

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