• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / duplicate-post / 14725616456

29 Apr 2025 07:21AM UTC coverage: 45.469% (-4.7%) from 50.122%
14725616456

push

github

web-flow
Merge pull request #402 from Yoast/feature/drop-php-7.2-7.3

Drop support for Php 7.2 and 7.3

1164 of 2560 relevant lines covered (45.47%)

1.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.0
/src/ui/block-editor.php
1
<?php
2

3
namespace Yoast\WP\Duplicate_Post\UI;
4

5
use WP_Post;
6
use Yoast\WP\Duplicate_Post\Permissions_Helper;
7
use Yoast\WP\Duplicate_Post\Utils;
8

9
/**
10
 * Duplicate Post class to manage the block editor UI.
11
 */
12
class Block_Editor {
13

14
        /**
15
         * Holds the object to create the action link to duplicate.
16
         *
17
         * @var Link_Builder
18
         */
19
        protected $link_builder;
20

21
        /**
22
         * Holds the permissions helper.
23
         *
24
         * @var Permissions_Helper
25
         */
26
        protected $permissions_helper;
27

28
        /**
29
         * Holds the asset manager.
30
         *
31
         * @var Asset_Manager
32
         */
33
        protected $asset_manager;
34

35
        /**
36
         * Initializes the class.
37
         *
38
         * @param Link_Builder       $link_builder       The link builder.
39
         * @param Permissions_Helper $permissions_helper The permissions helper.
40
         * @param Asset_Manager      $asset_manager      The asset manager.
41
         */
42
        public function __construct( Link_Builder $link_builder, Permissions_Helper $permissions_helper, Asset_Manager $asset_manager ) {
2✔
43
                $this->link_builder       = $link_builder;
2✔
44
                $this->permissions_helper = $permissions_helper;
2✔
45
                $this->asset_manager      = $asset_manager;
2✔
46
        }
47

48
        /**
49
         * Adds hooks to integrate with WordPress.
50
         *
51
         * @return void
52
         */
53
        public function register_hooks() {
2✔
54
                \add_action( 'elementor/editor/after_enqueue_styles', [ $this, 'hide_elementor_post_status' ] );
2✔
55
                \add_action( 'elementor/editor/before_enqueue_scripts', [ $this, 'enqueue_elementor_script' ], 9 );
2✔
56
                \add_action( 'admin_enqueue_scripts', [ $this, 'should_previously_used_keyword_assessment_run' ], 9 );
2✔
57
                \add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_block_editor_scripts' ] );
2✔
58
                \add_filter( 'wpseo_link_suggestions_indexables', [ $this, 'remove_original_from_wpseo_link_suggestions' ], 10, 3 );
2✔
59
        }
60

61
        /**
62
         * Enqueues the necessary Elementor script for the current post.
63
         *
64
         * @return void
65
         */
66
        public function enqueue_elementor_script() {
×
67
                $post = \get_post();
×
68

69
                if ( ! $post instanceof WP_Post ) {
×
70
                        return;
×
71
                }
72

73
                $edit_js_object = $this->generate_js_object( $post );
×
74
                $this->asset_manager->enqueue_elementor_script( $edit_js_object );
×
75
        }
76

77
        /**
78
         * Hides the post status control if we're working on a Rewrite and Republish post.
79
         *
80
         * @return void
81
         */
82
        public function hide_elementor_post_status() {
4✔
83
                $post = \get_post();
4✔
84

85
                if ( ! $post instanceof WP_Post || ! $this->permissions_helper->is_rewrite_and_republish_copy( $post ) ) {
4✔
86
                        return;
2✔
87
                }
88
                \wp_add_inline_style(
2✔
89
                        'elementor-editor',
2✔
90
                        '.elementor-control-post_status { display: none !important; }'
2✔
91
                );
2✔
92
        }
93

94
        /**
95
         * Disables the Yoast SEO PreviouslyUsedKeyword assessment for Rewrite & Republish original and duplicate posts.
96
         *
97
         * @return void
98
         */
99
        public function should_previously_used_keyword_assessment_run() {
12✔
100
                if ( $this->permissions_helper->is_edit_post_screen() || $this->permissions_helper->is_new_post_screen() ) {
12✔
101

102
                        $post = \get_post();
10✔
103

104
                        if (
105
                                $post instanceof WP_Post
10✔
106
                                && (
107
                                        $this->permissions_helper->is_rewrite_and_republish_copy( $post )
10✔
108
                                        || $this->permissions_helper->has_rewrite_and_republish_copy( $post )
10✔
109
                                )
110
                        ) {
111
                                \add_filter( 'wpseo_previously_used_keyword_active', '__return_false' );
6✔
112
                        }
113
                }
114
        }
115

116
        /**
117
         * Enqueues the necessary JavaScript code for the block editor.
118
         *
119
         * @return void
120
         */
121
        public function enqueue_block_editor_scripts() {
8✔
122
                if ( ! $this->permissions_helper->is_edit_post_screen() && ! $this->permissions_helper->is_new_post_screen() ) {
8✔
123
                        return;
2✔
124
                }
125

126
                $post = \get_post();
6✔
127

128
                if ( ! $post instanceof WP_Post ) {
6✔
129
                        return;
2✔
130
                }
131

132
                $edit_js_object = $this->generate_js_object( $post );
4✔
133
                $this->asset_manager->enqueue_edit_script( $edit_js_object );
4✔
134

135
                if ( $this->permissions_helper->is_rewrite_and_republish_copy( $post ) ) {
4✔
136
                        $string_js_object = [
2✔
137
                                'checkLink' => $this->get_check_permalink(),
2✔
138
                        ];
2✔
139
                        $this->asset_manager->enqueue_strings_script( $string_js_object );
2✔
140
                }
141
        }
142

143
        /**
144
         * Generates a New Draft permalink for the current post.
145
         *
146
         * @return string The permalink. Returns empty if the post can't be copied.
147
         */
148
        public function get_new_draft_permalink() {
4✔
149
                $post = \get_post();
4✔
150

151
                if ( ! $post instanceof WP_Post || ! $this->permissions_helper->should_links_be_displayed( $post ) ) {
4✔
152
                        return '';
2✔
153
                }
154

155
                return $this->link_builder->build_new_draft_link( $post );
2✔
156
        }
157

158
        /**
159
         * Generates a Rewrite & Republish permalink for the current post.
160
         *
161
         * @return string The permalink. Returns empty if the post cannot be copied for Rewrite & Republish.
162
         */
163
        public function get_rewrite_republish_permalink() {
8✔
164
                $post = \get_post();
8✔
165

166
                if (
167
                        ! $post instanceof WP_Post
8✔
168
                        || $this->permissions_helper->is_rewrite_and_republish_copy( $post )
8✔
169
                        || $this->permissions_helper->has_rewrite_and_republish_copy( $post )
6✔
170
                        || ! $this->permissions_helper->should_links_be_displayed( $post )
8✔
171
                ) {
172
                        return '';
6✔
173
                }
174

175
                return $this->link_builder->build_rewrite_and_republish_link( $post );
2✔
176
        }
177

178
        /**
179
         * Generates a Check Changes permalink for the current post, if it's intended for Rewrite & Republish.
180
         *
181
         * @return string The permalink. Returns empty if the post does not exist or it's not a Rewrite & Republish copy.
182
         */
183
        public function get_check_permalink() {
4✔
184
                $post = \get_post();
4✔
185

186
                if ( ! $post instanceof WP_Post || ! $this->permissions_helper->is_rewrite_and_republish_copy( $post ) ) {
4✔
187
                        return '';
2✔
188
                }
189

190
                return $this->link_builder->build_check_link( $post );
2✔
191
        }
192

193
        /**
194
         * Generates a URL to the original post edit screen.
195
         *
196
         * @return string The URL. Empty if the copy post doesn't have an original.
197
         */
198
        public function get_original_post_edit_url() {
8✔
199
                $post = \get_post();
8✔
200

201
                if ( ! $post instanceof WP_Post || ! $this->permissions_helper->is_rewrite_and_republish_copy( $post ) ) {
8✔
202
                        return '';
4✔
203
                }
204

205
                $original_post_id = Utils::get_original_post_id( $post->ID );
4✔
206

207
                if ( ! $original_post_id ) {
4✔
208
                        return '';
2✔
209
                }
210

211
                return \add_query_arg(
2✔
212
                        [
2✔
213
                                'dprepublished' => 1,
2✔
214
                                'dpcopy'        => $post->ID,
2✔
215
                                'dpnonce'       => \wp_create_nonce( 'dp-republish' ),
2✔
216
                        ],
2✔
217
                        \admin_url( 'post.php?action=edit&post=' . $original_post_id )
2✔
218
                );
2✔
219
        }
220

221
        /**
222
         * Generates an array of data to be passed as a localization object to JavaScript.
223
         *
224
         * @param WP_Post $post The current post object.
225
         *
226
         * @return array<string, mixed> The data to pass to JavaScript.
227
         */
228
        protected function generate_js_object( WP_Post $post ) {
×
229
                $is_rewrite_and_republish_copy = $this->permissions_helper->is_rewrite_and_republish_copy( $post );
×
230

231
                return [
×
232
                        'newDraftLink'            => $this->get_new_draft_permalink(),
×
233
                        'rewriteAndRepublishLink' => $this->get_rewrite_republish_permalink(),
×
234
                        'showLinks'               => Utils::get_option( 'duplicate_post_show_link' ),
×
235
                        'showLinksIn'             => Utils::get_option( 'duplicate_post_show_link_in' ),
×
236
                        'rewriting'               => ( $is_rewrite_and_republish_copy ) ? 1 : 0,
×
237
                        'originalEditURL'         => $this->get_original_post_edit_url(),
×
238
                ];
×
239
        }
240

241
        /**
242
         * Filters the Yoast SEO Premium link suggestions.
243
         *
244
         * Removes the original post from the Yoast SEO Premium link suggestions
245
         * displayed on the Rewrite & Republish copy.
246
         *
247
         * @param array  $suggestions An array of suggestion indexables that can be filtered.
248
         * @param int    $object_id   The object id for the current indexable.
249
         * @param string $object_type The object type for the current indexable.
250
         *
251
         * @return array The filtered array of suggestion indexables.
252
         */
253
        public function remove_original_from_wpseo_link_suggestions( $suggestions, $object_id, $object_type ) {
×
254
                if ( $object_type !== 'post' ) {
×
255
                        return $suggestions;
×
256
                }
257

258
                // WordPress get_post already checks if the passed ID is valid and returns null if it's not.
259
                $post = \get_post( $object_id );
×
260

261
                if ( ! $post instanceof WP_Post || ! $this->permissions_helper->is_rewrite_and_republish_copy( $post ) ) {
×
262
                        return $suggestions;
×
263
                }
264

265
                $original_post_id = Utils::get_original_post_id( $post->ID );
×
266

267
                return \array_filter(
×
268
                        $suggestions,
×
269
                        static function ( $suggestion ) use ( $original_post_id ) {
×
270
                                return $suggestion->object_id !== $original_post_id;
×
271
                        }
×
272
                );
×
273
        }
274
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc