• 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

8.43
/src/utils.php
1
<?php
2

3
namespace Yoast\WP\Duplicate_Post;
4

5
use WP_Post;
6

7
/**
8
 * Utility methods for Duplicate Post.
9
 *
10
 * @since 4.0
11
 */
12
class Utils {
13

14
        /**
15
         * Flattens a version number for use in a filename.
16
         *
17
         * @param string $version The original version number.
18
         *
19
         * @return string The flattened version number.
20
         */
21
        public static function flatten_version( $version ) {
12✔
22
                $parts = \explode( '.', $version );
12✔
23

24
                if ( \count( $parts ) === 2 && \preg_match( '/^\d+$/', $parts[1] ) === 1 ) {
12✔
25
                        $parts[] = '0';
4✔
26
                }
27

28
                return \implode( '', $parts );
12✔
29
        }
30

31
        /**
32
         * Adds slashes only to strings.
33
         *
34
         * @param mixed $value Value to slash only if string.
35
         *
36
         * @return string|mixed
37
         */
38
        public static function addslashes_to_strings_only( $value ) {
16✔
39
                return \is_string( $value ) ? \addslashes( $value ) : $value;
16✔
40
        }
41

42
        /**
43
         * Replaces faulty core wp_slash().
44
         *
45
         * Until WP 5.5 wp_slash() recursively added slashes not just to strings in array/objects, leading to errors.
46
         *
47
         * @param mixed $value What to add slashes to.
48
         *
49
         * @return mixed
50
         */
51
        public static function recursively_slash_strings( $value ) {
×
52
                return \map_deep( $value, [ self::class, 'addslashes_to_strings_only' ] );
×
53
        }
54

55
        /**
56
         * Gets the original post.
57
         *
58
         * @param int|WP_Post|null $post   Optional. Post ID or Post object.
59
         * @param string           $output Optional, default is Object. Either OBJECT, ARRAY_A, or ARRAY_N.
60
         *
61
         * @return WP_Post|null Post data if successful, null otherwise.
62
         */
63
        public static function get_original( $post = null, $output = \OBJECT ) {
×
64
                $post = \get_post( $post );
×
65
                if ( ! $post ) {
×
66
                        return null;
×
67
                }
68

69
                $original_id = self::get_original_post_id( $post->ID );
×
70

71
                if ( empty( $original_id ) ) {
×
72
                        return null;
×
73
                }
74

75
                return \get_post( $original_id, $output );
×
76
        }
77

78
        /**
79
         * Determines if the post has ancestors marked for copy.
80
         *
81
         * If we are copying children, and the post has already an ancestor marked for copy, we have to filter it out.
82
         *
83
         * @param WP_Post $post     The post object.
84
         * @param array   $post_ids The array of marked post IDs.
85
         *
86
         * @return bool Whether the post has ancestors marked for copy.
87
         */
88
        public static function has_ancestors_marked( $post, $post_ids ) {
×
89
                $ancestors_in_array = 0;
×
90
                $parent             = \wp_get_post_parent_id( $post->ID );
×
91
                while ( $parent ) {
×
92
                        if ( \in_array( $parent, $post_ids, true ) ) {
×
93
                                ++$ancestors_in_array;
×
94
                        }
95
                        $parent = \wp_get_post_parent_id( $parent );
×
96
                }
97
                return ( $ancestors_in_array !== 0 );
×
98
        }
99

100
        /**
101
         * Returns a link to edit, preview or view a post, in accordance to user capabilities.
102
         *
103
         * @param WP_Post $post Post ID or Post object.
104
         *
105
         * @return string|null The link to edit, preview or view a post.
106
         */
107
        public static function get_edit_or_view_link( $post ) {
×
108
                $post = \get_post( $post );
×
109
                if ( ! $post ) {
×
110
                        return null;
×
111
                }
112

113
                $can_edit_post    = \current_user_can( 'edit_post', $post->ID );
×
114
                $title            = \_draft_or_post_title( $post );
×
115
                $post_type_object = \get_post_type_object( $post->post_type );
×
116

117
                if ( $can_edit_post && $post->post_status !== 'trash' ) {
×
118
                        return \sprintf(
×
119
                                '<a href="%s" aria-label="%s">%s</a>',
×
120
                                \esc_url( \get_edit_post_link( $post->ID ) ),
×
121
                                /* translators: Hidden accessibility text; %s: post title */
122
                                \esc_attr( \sprintf( \__( 'Edit &#8220;%s&#8221;', 'duplicate-post' ), $title ) ),
×
123
                                $title
×
124
                        );
×
125
                }
126
                elseif ( \is_post_type_viewable( $post_type_object ) ) {
×
127
                        if ( \in_array( $post->post_status, [ 'pending', 'draft', 'future' ], true ) ) {
×
128
                                if ( $can_edit_post ) {
×
129
                                        $preview_link = \get_preview_post_link( $post );
×
130
                                        return \sprintf(
×
131
                                                '<a href="%s" rel="bookmark" aria-label="%s">%s</a>',
×
132
                                                \esc_url( $preview_link ),
×
133
                                                /* translators: Hidden accessibility text; %s: post title */
134
                                                \esc_attr( \sprintf( \__( 'Preview &#8220;%s&#8221;', 'duplicate-post' ), $title ) ),
×
135
                                                $title
×
136
                                        );
×
137
                                }
138
                        }
139
                        elseif ( $post->post_status !== 'trash' ) {
×
140
                                return \sprintf(
×
141
                                        '<a href="%s" rel="bookmark" aria-label="%s">%s</a>',
×
142
                                        \esc_url( \get_permalink( $post->ID ) ),
×
143
                                        /* translators: Hidden accessibility text; %s: post title */
144
                                        \esc_attr( \sprintf( \__( 'View &#8220;%s&#8221;', 'duplicate-post' ), $title ) ),
×
145
                                        $title
×
146
                                );
×
147
                        }
148
                }
149

150
                return $title;
×
151
        }
152

153
        /**
154
         * Gets the ID of the original post intended to be rewritten with the copy for Rewrite & Republish.
155
         *
156
         * @param int $post_id The copy post ID.
157
         *
158
         * @return int The original post id of a copy for Rewrite & Republish.
159
         */
160
        public static function get_original_post_id( $post_id ) {
×
161
                return (int) \get_post_meta( $post_id, '_dp_original', true );
×
162
        }
163

164
        /**
165
         * Gets the registered WordPress roles.
166
         *
167
         * @codeCoverageIgnore As this is a simple wrapper method for a built-in WordPress method, we don't have to test it.
168
         *
169
         * @return array The roles.
170
         */
171
        public static function get_roles() {
172
                global $wp_roles;
173

174
                return $wp_roles->get_names();
175
        }
176

177
        /**
178
         * Gets the default meta field names to be filtered out.
179
         *
180
         * @return array<string> The names of the meta fields to filter out by default.
181
         */
182
        public static function get_default_filtered_meta_names() {
×
183
                return [
×
184
                        '_edit_lock',
×
185
                        '_edit_last',
×
186
                        '_dp_original',
×
187
                        '_dp_is_rewrite_republish_copy',
×
188
                        '_dp_has_rewrite_republish_copy',
×
189
                        '_dp_has_been_republished',
×
190
                        '_dp_creation_date_gmt',
×
191
                ];
×
192
        }
193

194
        /**
195
         * Gets a Duplicate Post option from the database.
196
         *
197
         * @param string $option The option to get.
198
         * @param string $key    The key to retrieve, if the option is an array.
199
         *
200
         * @return mixed The option.
201
         */
202
        public static function get_option( $option, $key = '' ) {
×
203
                $option = \get_option( $option );
×
204

205
                if ( ! \is_array( $option ) || empty( $key ) ) {
×
206
                        return $option;
×
207
                }
208

209
                if ( ! \array_key_exists( $key, $option ) ) {
×
210
                        return '';
×
211
                }
212

213
                return $option[ $key ];
×
214
        }
215

216
        /**
217
         * Determines if a plugin is active.
218
         *
219
         * We can't use is_plugin_active because this must work on the frontend too.
220
         *
221
         * @param string $plugin Path to the plugin file relative to the plugins directory.
222
         *
223
         * @return bool Whether a plugin is currently active.
224
         */
225
        public static function is_plugin_active( $plugin ) {
×
226
                if ( \in_array( $plugin, (array) \get_option( 'active_plugins', [] ), true ) ) {
×
227
                        return true;
×
228
                }
229

230
                if ( ! \is_multisite() ) {
×
231
                        return false;
×
232
                }
233

234
                $plugins = \get_site_option( 'active_sitewide_plugins' );
×
235
                return isset( $plugins[ $plugin ] );
×
236
        }
237
}
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