Edit File by line
/home/barbar84/www/wp-conte.../plugins/duplicat.../src
File: class-utils.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Utility methods for Duplicate Post.
[2] Fix | Delete
*
[3] Fix | Delete
* @package Duplicate_Post
[4] Fix | Delete
* @since 4.0
[5] Fix | Delete
*/
[6] Fix | Delete
[7] Fix | Delete
namespace Yoast\WP\Duplicate_Post;
[8] Fix | Delete
[9] Fix | Delete
/**
[10] Fix | Delete
* Represents the Utils class.
[11] Fix | Delete
*/
[12] Fix | Delete
class Utils {
[13] Fix | Delete
[14] Fix | Delete
/**
[15] Fix | Delete
* Flattens a version number for use in a filename.
[16] Fix | Delete
*
[17] Fix | Delete
* @param string $version The original version number.
[18] Fix | Delete
*
[19] Fix | Delete
* @return string The flattened version number.
[20] Fix | Delete
*/
[21] Fix | Delete
public static function flatten_version( $version ) {
[22] Fix | Delete
$parts = \explode( '.', $version );
[23] Fix | Delete
[24] Fix | Delete
if ( \count( $parts ) === 2 && \preg_match( '/^\d+$/', $parts[1] ) === 1 ) {
[25] Fix | Delete
$parts[] = '0';
[26] Fix | Delete
}
[27] Fix | Delete
[28] Fix | Delete
return \implode( '', $parts );
[29] Fix | Delete
}
[30] Fix | Delete
[31] Fix | Delete
/**
[32] Fix | Delete
* Adds slashes only to strings.
[33] Fix | Delete
*
[34] Fix | Delete
* @param mixed $value Value to slash only if string.
[35] Fix | Delete
*
[36] Fix | Delete
* @return string|mixed
[37] Fix | Delete
*/
[38] Fix | Delete
public static function addslashes_to_strings_only( $value ) {
[39] Fix | Delete
return \is_string( $value ) ? \addslashes( $value ) : $value;
[40] Fix | Delete
}
[41] Fix | Delete
[42] Fix | Delete
/**
[43] Fix | Delete
* Replaces faulty core wp_slash().
[44] Fix | Delete
*
[45] Fix | Delete
* Until WP 5.5 wp_slash() recursively added slashes not just to strings in array/objects, leading to errors.
[46] Fix | Delete
*
[47] Fix | Delete
* @param mixed $value What to add slashes to.
[48] Fix | Delete
*
[49] Fix | Delete
* @return mixed
[50] Fix | Delete
*/
[51] Fix | Delete
public static function recursively_slash_strings( $value ) {
[52] Fix | Delete
return \map_deep( $value, [ self::class, 'addslashes_to_strings_only' ] );
[53] Fix | Delete
}
[54] Fix | Delete
[55] Fix | Delete
/**
[56] Fix | Delete
* Gets the original post.
[57] Fix | Delete
*
[58] Fix | Delete
* @param int|\WP_Post|null $post Optional. Post ID or Post object.
[59] Fix | Delete
* @param string $output Optional, default is Object. Either OBJECT, ARRAY_A, or ARRAY_N.
[60] Fix | Delete
*
[61] Fix | Delete
* @return \WP_Post|null Post data if successful, null otherwise.
[62] Fix | Delete
*/
[63] Fix | Delete
public static function get_original( $post = null, $output = \OBJECT ) {
[64] Fix | Delete
$post = \get_post( $post );
[65] Fix | Delete
if ( ! $post ) {
[66] Fix | Delete
return null;
[67] Fix | Delete
}
[68] Fix | Delete
[69] Fix | Delete
$original_id = self::get_original_post_id( $post->ID );
[70] Fix | Delete
[71] Fix | Delete
if ( empty( $original_id ) ) {
[72] Fix | Delete
return null;
[73] Fix | Delete
}
[74] Fix | Delete
[75] Fix | Delete
return \get_post( $original_id, $output );
[76] Fix | Delete
}
[77] Fix | Delete
[78] Fix | Delete
/**
[79] Fix | Delete
* Determines if the post has ancestors marked for copy.
[80] Fix | Delete
*
[81] Fix | Delete
* If we are copying children, and the post has already an ancestor marked for copy, we have to filter it out.
[82] Fix | Delete
*
[83] Fix | Delete
* @param \WP_Post $post The post object.
[84] Fix | Delete
* @param array $post_ids The array of marked post IDs.
[85] Fix | Delete
*
[86] Fix | Delete
* @return bool Whether the post has ancestors marked for copy.
[87] Fix | Delete
*/
[88] Fix | Delete
public static function has_ancestors_marked( $post, $post_ids ) {
[89] Fix | Delete
$ancestors_in_array = 0;
[90] Fix | Delete
$parent = \wp_get_post_parent_id( $post->ID );
[91] Fix | Delete
while ( $parent ) {
[92] Fix | Delete
if ( \in_array( $parent, $post_ids, true ) ) {
[93] Fix | Delete
$ancestors_in_array++;
[94] Fix | Delete
}
[95] Fix | Delete
$parent = \wp_get_post_parent_id( $parent );
[96] Fix | Delete
}
[97] Fix | Delete
return ( $ancestors_in_array !== 0 );
[98] Fix | Delete
}
[99] Fix | Delete
[100] Fix | Delete
/**
[101] Fix | Delete
* Returns a link to edit, preview or view a post, in accordance to user capabilities.
[102] Fix | Delete
*
[103] Fix | Delete
* @param \WP_Post $post Post ID or Post object.
[104] Fix | Delete
*
[105] Fix | Delete
* @return string|null The link to edit, preview or view a post.
[106] Fix | Delete
*/
[107] Fix | Delete
public static function get_edit_or_view_link( $post ) {
[108] Fix | Delete
$post = \get_post( $post );
[109] Fix | Delete
if ( ! $post ) {
[110] Fix | Delete
return null;
[111] Fix | Delete
}
[112] Fix | Delete
[113] Fix | Delete
$can_edit_post = \current_user_can( 'edit_post', $post->ID );
[114] Fix | Delete
$title = \_draft_or_post_title( $post );
[115] Fix | Delete
$post_type_object = \get_post_type_object( $post->post_type );
[116] Fix | Delete
[117] Fix | Delete
if ( $can_edit_post && $post->post_status !== 'trash' ) {
[118] Fix | Delete
return \sprintf(
[119] Fix | Delete
'<a href="%s" aria-label="%s">%s</a>',
[120] Fix | Delete
\get_edit_post_link( $post->ID ),
[121] Fix | Delete
/* translators: %s: post title */
[122] Fix | Delete
\esc_attr( \sprintf( \__( 'Edit &#8220;%s&#8221;', 'default' ), $title ) ),
[123] Fix | Delete
$title
[124] Fix | Delete
);
[125] Fix | Delete
} elseif ( \is_post_type_viewable( $post_type_object ) ) {
[126] Fix | Delete
if ( \in_array( $post->post_status, [ 'pending', 'draft', 'future' ], true ) ) {
[127] Fix | Delete
if ( $can_edit_post ) {
[128] Fix | Delete
$preview_link = \get_preview_post_link( $post );
[129] Fix | Delete
return \sprintf(
[130] Fix | Delete
'<a href="%s" rel="bookmark" aria-label="%s">%s</a>',
[131] Fix | Delete
\esc_url( $preview_link ),
[132] Fix | Delete
/* translators: %s: post title */
[133] Fix | Delete
\esc_attr( \sprintf( \__( 'Preview &#8220;%s&#8221;', 'default' ), $title ) ),
[134] Fix | Delete
$title
[135] Fix | Delete
);
[136] Fix | Delete
}
[137] Fix | Delete
} elseif ( $post->post_status !== 'trash' ) {
[138] Fix | Delete
return \sprintf(
[139] Fix | Delete
'<a href="%s" rel="bookmark" aria-label="%s">%s</a>',
[140] Fix | Delete
\get_permalink( $post->ID ),
[141] Fix | Delete
/* translators: %s: post title */
[142] Fix | Delete
\esc_attr( \sprintf( \__( 'View &#8220;%s&#8221;', 'default' ), $title ) ),
[143] Fix | Delete
$title
[144] Fix | Delete
);
[145] Fix | Delete
}
[146] Fix | Delete
}
[147] Fix | Delete
return $title;
[148] Fix | Delete
}
[149] Fix | Delete
[150] Fix | Delete
/**
[151] Fix | Delete
* Gets the ID of the original post intended to be rewritten with the copy for Rewrite & Republish.
[152] Fix | Delete
*
[153] Fix | Delete
* @param int $post_id The copy post ID.
[154] Fix | Delete
*
[155] Fix | Delete
* @return int The original post id of a copy for Rewrite & Republish.
[156] Fix | Delete
*/
[157] Fix | Delete
public static function get_original_post_id( $post_id ) {
[158] Fix | Delete
return (int) \get_post_meta( $post_id, '_dp_original', true );
[159] Fix | Delete
}
[160] Fix | Delete
[161] Fix | Delete
/**
[162] Fix | Delete
* Gets the registered WordPress roles.
[163] Fix | Delete
*
[164] Fix | Delete
* @return array The roles.
[165] Fix | Delete
* @codeCoverageIgnore As this is a simple wrapper method for a built-in WordPress method, we don't have to test it.
[166] Fix | Delete
*/
[167] Fix | Delete
public static function get_roles() {
[168] Fix | Delete
global $wp_roles;
[169] Fix | Delete
[170] Fix | Delete
return $wp_roles->get_names();
[171] Fix | Delete
}
[172] Fix | Delete
[173] Fix | Delete
/**
[174] Fix | Delete
* Gets a Duplicate Post option from the database.
[175] Fix | Delete
*
[176] Fix | Delete
* @param string $option The option to get.
[177] Fix | Delete
* @param string $key The key to retrieve, if the option is an array.
[178] Fix | Delete
*
[179] Fix | Delete
* @return mixed The option.
[180] Fix | Delete
*/
[181] Fix | Delete
public static function get_option( $option, $key = '' ) {
[182] Fix | Delete
$option = \get_option( $option );
[183] Fix | Delete
[184] Fix | Delete
if ( ! \is_array( $option ) || empty( $key ) ) {
[185] Fix | Delete
return $option;
[186] Fix | Delete
}
[187] Fix | Delete
[188] Fix | Delete
if ( ! \array_key_exists( $key, $option ) ) {
[189] Fix | Delete
return '';
[190] Fix | Delete
}
[191] Fix | Delete
[192] Fix | Delete
return $option[ $key ];
[193] Fix | Delete
}
[194] Fix | Delete
}
[195] Fix | Delete
[196] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function