Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/jetpack/modules
File: copy-post.php
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
[0] Fix | Delete
/**
[1] Fix | Delete
* Module Name: Copy Post
[2] Fix | Delete
* Module Description: Duplicate any post or page in one click to speed up content creation.
[3] Fix | Delete
* Sort Order: 15
[4] Fix | Delete
* First Introduced: 7.0
[5] Fix | Delete
* Requires Connection: No
[6] Fix | Delete
* Auto Activate: No
[7] Fix | Delete
* Module Tags: Writing
[8] Fix | Delete
* Feature: Writing
[9] Fix | Delete
* Additional Search Queries: copy, duplicate
[10] Fix | Delete
*
[11] Fix | Delete
* @package automattic/jetpack
[12] Fix | Delete
*/
[13] Fix | Delete
[14] Fix | Delete
use Automattic\Jetpack\Assets\Logo;
[15] Fix | Delete
[16] Fix | Delete
if ( ! defined( 'ABSPATH' ) ) {
[17] Fix | Delete
exit( 0 );
[18] Fix | Delete
}
[19] Fix | Delete
[20] Fix | Delete
// phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files.
[21] Fix | Delete
[22] Fix | Delete
/**
[23] Fix | Delete
* Copy Post class.
[24] Fix | Delete
*
[25] Fix | Delete
* @phan-constructor-used-for-side-effects
[26] Fix | Delete
*/
[27] Fix | Delete
class Jetpack_Copy_Post {
[28] Fix | Delete
/**
[29] Fix | Delete
* Jetpack_Copy_Post_By_Param constructor.
[30] Fix | Delete
* Add row actions to post/page/CPT listing screens.
[31] Fix | Delete
* Process any `?copy` param if on a create new post/page/CPT screen.
[32] Fix | Delete
*
[33] Fix | Delete
* @return void
[34] Fix | Delete
*/
[35] Fix | Delete
public function __construct() {
[36] Fix | Delete
if ( 'edit.php' === $GLOBALS['pagenow'] || ( 'admin-ajax.php' === $GLOBALS['pagenow'] && ! empty( $_POST['post_view'] ) && 'list' === $_POST['post_view'] && ! empty( $_POST['action'] ) && 'inline-save' === $_POST['action'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- update_post_data() handles access check.
[37] Fix | Delete
add_action( 'admin_head', array( $this, 'print_inline_styles' ) );
[38] Fix | Delete
add_filter( 'post_row_actions', array( $this, 'add_row_action' ), 10, 2 );
[39] Fix | Delete
add_filter( 'page_row_actions', array( $this, 'add_row_action' ), 10, 2 );
[40] Fix | Delete
return;
[41] Fix | Delete
}
[42] Fix | Delete
[43] Fix | Delete
if ( ! empty( $_GET['jetpack-copy'] ) && 'post-new.php' === $GLOBALS['pagenow'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- update_post_data() handles access check.
[44] Fix | Delete
add_action( 'wp_insert_post', array( $this, 'update_post_data' ), 10, 3 );
[45] Fix | Delete
add_filter( 'pre_option_default_post_format', '__return_empty_string' );
[46] Fix | Delete
}
[47] Fix | Delete
}
[48] Fix | Delete
[49] Fix | Delete
/**
[50] Fix | Delete
* Echos inline styles for the Jetpack logo.
[51] Fix | Delete
*
[52] Fix | Delete
* @return void
[53] Fix | Delete
*/
[54] Fix | Delete
public function print_inline_styles() {
[55] Fix | Delete
echo '
[56] Fix | Delete
<style id="jetpack-copy-post-styles">
[57] Fix | Delete
#jetpack-logo__icon {
[58] Fix | Delete
height: 14px;
[59] Fix | Delete
width: 14px;
[60] Fix | Delete
vertical-align: text-bottom;
[61] Fix | Delete
}
[62] Fix | Delete
#jetpack-logo__icon path {
[63] Fix | Delete
fill: inherit;
[64] Fix | Delete
}
[65] Fix | Delete
</style>
[66] Fix | Delete
';
[67] Fix | Delete
}
[68] Fix | Delete
[69] Fix | Delete
/**
[70] Fix | Delete
* Update the new (target) post data with the source post data.
[71] Fix | Delete
*
[72] Fix | Delete
* @param int $target_post_id Target post ID.
[73] Fix | Delete
* @param WP_Post $post Target post object (not used).
[74] Fix | Delete
* @param bool $update Whether this is an existing post being updated or not.
[75] Fix | Delete
* @return void
[76] Fix | Delete
*/
[77] Fix | Delete
public function update_post_data( $target_post_id, $post, $update ) {
[78] Fix | Delete
// This `$update` check avoids infinite loops of trying to update our updated post.
[79] Fix | Delete
if ( $update ) {
[80] Fix | Delete
return;
[81] Fix | Delete
}
[82] Fix | Delete
[83] Fix | Delete
// Shouldn't happen, since this filter is only added when the value isn't empty, but check anyway.
[84] Fix | Delete
if ( empty( $_GET['jetpack-copy'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
[85] Fix | Delete
return;
[86] Fix | Delete
}
[87] Fix | Delete
[88] Fix | Delete
$source_post = get_post( intval( $_GET['jetpack-copy'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
[89] Fix | Delete
if ( ! $source_post instanceof WP_Post ||
[90] Fix | Delete
! $this->user_can_access_post( $source_post->ID ) ||
[91] Fix | Delete
! $this->validate_post_type( $source_post ) ) {
[92] Fix | Delete
return;
[93] Fix | Delete
}
[94] Fix | Delete
[95] Fix | Delete
$update_results = array(
[96] Fix | Delete
'update_content' => $this->update_content( $source_post, $target_post_id ),
[97] Fix | Delete
'update_featured_image' => $this->update_featured_image( $source_post, $target_post_id ),
[98] Fix | Delete
'update_post_format' => $this->update_post_format( $source_post, $target_post_id ),
[99] Fix | Delete
'update_likes_sharing' => $this->update_likes_sharing( $source_post, $target_post_id ),
[100] Fix | Delete
'update_post_type_terms' => $this->update_post_type_terms( $source_post, $target_post_id ),
[101] Fix | Delete
);
[102] Fix | Delete
[103] Fix | Delete
// Required to satisfy get_default_post_to_edit(), which has these filters after post creation.
[104] Fix | Delete
add_filter( 'default_title', array( $this, 'filter_title' ), 10, 2 );
[105] Fix | Delete
add_filter( 'default_content', array( $this, 'filter_content' ), 10, 2 );
[106] Fix | Delete
add_filter( 'default_excerpt', array( $this, 'filter_excerpt' ), 10, 2 );
[107] Fix | Delete
[108] Fix | Delete
// Required to avoid the block editor from adding default blocks according to post format.
[109] Fix | Delete
add_filter( 'block_editor_settings_all', array( $this, 'remove_post_format_template' ) );
[110] Fix | Delete
[111] Fix | Delete
/**
[112] Fix | Delete
* Fires after all updates have been performed, and default content filters have been added.
[113] Fix | Delete
* Allows for any cleanup or post operations, and default content filters can be removed or modified.
[114] Fix | Delete
*
[115] Fix | Delete
* @module copy-post
[116] Fix | Delete
*
[117] Fix | Delete
* @since 7.0.0
[118] Fix | Delete
*
[119] Fix | Delete
* @param WP_Post $source_post Post object that was copied.
[120] Fix | Delete
* @param int $target_post_id Target post ID.
[121] Fix | Delete
* @param array $update_results Results of all update operations, allowing action to be taken.
[122] Fix | Delete
*/
[123] Fix | Delete
do_action( 'jetpack_copy_post', $source_post, $target_post_id, $update_results );
[124] Fix | Delete
}
[125] Fix | Delete
[126] Fix | Delete
/**
[127] Fix | Delete
* Determine if the current user has edit access to the source post.
[128] Fix | Delete
*
[129] Fix | Delete
* @param int $post_id Source post ID (the post being copied).
[130] Fix | Delete
* @return bool True if user has the meta cap of `edit_post` for the given post ID, false otherwise.
[131] Fix | Delete
*/
[132] Fix | Delete
protected function user_can_access_post( $post_id ) {
[133] Fix | Delete
return current_user_can( 'edit_post', $post_id );
[134] Fix | Delete
}
[135] Fix | Delete
[136] Fix | Delete
/**
[137] Fix | Delete
* Update the target post's title, content, excerpt, categories, and tags.
[138] Fix | Delete
*
[139] Fix | Delete
* @param WP_Post $source_post Post object to be copied.
[140] Fix | Delete
* @param int $target_post_id Target post ID.
[141] Fix | Delete
* @return int 0 on failure, or the updated post ID on success.
[142] Fix | Delete
*/
[143] Fix | Delete
protected function update_content( $source_post, $target_post_id ) {
[144] Fix | Delete
$data = array(
[145] Fix | Delete
'ID' => $target_post_id,
[146] Fix | Delete
'post_title' => $source_post->post_title,
[147] Fix | Delete
'post_content' => $source_post->post_content,
[148] Fix | Delete
'post_excerpt' => $source_post->post_excerpt,
[149] Fix | Delete
'comment_status' => $source_post->comment_status,
[150] Fix | Delete
'ping_status' => $source_post->ping_status,
[151] Fix | Delete
'post_category' => wp_get_post_categories( $source_post->ID ),
[152] Fix | Delete
'post_password' => $source_post->post_password,
[153] Fix | Delete
'tags_input' => $source_post->tags_input,
[154] Fix | Delete
);
[155] Fix | Delete
[156] Fix | Delete
/**
[157] Fix | Delete
* Fires just before the target post is updated with its new data.
[158] Fix | Delete
* Allows for final data adjustments before updating the target post.
[159] Fix | Delete
*
[160] Fix | Delete
* @module copy-post
[161] Fix | Delete
*
[162] Fix | Delete
* @since 7.0.0
[163] Fix | Delete
*
[164] Fix | Delete
* @param array $data Post data with which to update the target (new) post.
[165] Fix | Delete
* @param WP_Post $source_post Post object being copied.
[166] Fix | Delete
* @param int $target_post_id Target post ID.
[167] Fix | Delete
*/
[168] Fix | Delete
$data = apply_filters( 'jetpack_copy_post_data', $data, $source_post, $target_post_id );
[169] Fix | Delete
return wp_update_post( $data );
[170] Fix | Delete
}
[171] Fix | Delete
[172] Fix | Delete
/**
[173] Fix | Delete
* Update terms for post types.
[174] Fix | Delete
*
[175] Fix | Delete
* @param WP_Post $source_post Post object to be copied.
[176] Fix | Delete
* @param int $target_post_id Target post ID.
[177] Fix | Delete
* @return array Results of attempts to set each term to the target (new) post.
[178] Fix | Delete
*/
[179] Fix | Delete
protected function update_post_type_terms( $source_post, $target_post_id ) {
[180] Fix | Delete
$results = array();
[181] Fix | Delete
[182] Fix | Delete
$bypassed_post_types = apply_filters( 'jetpack_copy_post_bypassed_post_types', array( 'post', 'page' ), $source_post, $target_post_id );
[183] Fix | Delete
if ( in_array( $source_post->post_type, $bypassed_post_types, true ) ) {
[184] Fix | Delete
return $results;
[185] Fix | Delete
}
[186] Fix | Delete
[187] Fix | Delete
$taxonomies = get_object_taxonomies( $source_post, 'objects' );
[188] Fix | Delete
foreach ( $taxonomies as $taxonomy ) {
[189] Fix | Delete
$terms = wp_get_post_terms( $source_post->ID, $taxonomy->name, array( 'fields' => 'ids' ) );
[190] Fix | Delete
$results[] = wp_set_post_terms( $target_post_id, $terms, $taxonomy->name );
[191] Fix | Delete
}
[192] Fix | Delete
[193] Fix | Delete
return $results;
[194] Fix | Delete
}
[195] Fix | Delete
[196] Fix | Delete
/**
[197] Fix | Delete
* Update the target post's featured image.
[198] Fix | Delete
*
[199] Fix | Delete
* @param WP_Post $source_post Post object to be copied.
[200] Fix | Delete
* @param int $target_post_id Target post ID.
[201] Fix | Delete
* @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure.
[202] Fix | Delete
*/
[203] Fix | Delete
protected function update_featured_image( $source_post, $target_post_id ) {
[204] Fix | Delete
$featured_image_id = get_post_thumbnail_id( $source_post );
[205] Fix | Delete
return update_post_meta( $target_post_id, '_thumbnail_id', $featured_image_id );
[206] Fix | Delete
}
[207] Fix | Delete
[208] Fix | Delete
/**
[209] Fix | Delete
* Update the target post's post format.
[210] Fix | Delete
*
[211] Fix | Delete
* @param WP_Post $source_post Post object to be copied.
[212] Fix | Delete
* @param int $target_post_id Target post ID.
[213] Fix | Delete
* @return array|WP_Error|false WP_Error on error, array of affected term IDs on success.
[214] Fix | Delete
*/
[215] Fix | Delete
protected function update_post_format( $source_post, $target_post_id ) {
[216] Fix | Delete
$post_format = get_post_format( $source_post );
[217] Fix | Delete
return set_post_format( $target_post_id, $post_format );
[218] Fix | Delete
}
[219] Fix | Delete
[220] Fix | Delete
/**
[221] Fix | Delete
* Ensure the block editor doesn't modify the source post content for non-standard post formats.
[222] Fix | Delete
*
[223] Fix | Delete
* @param array $settings Settings to be passed into the block editor.
[224] Fix | Delete
* @return array Settings with any `template` key removed.
[225] Fix | Delete
*/
[226] Fix | Delete
public function remove_post_format_template( $settings ) {
[227] Fix | Delete
unset( $settings['template'] );
[228] Fix | Delete
return $settings;
[229] Fix | Delete
}
[230] Fix | Delete
[231] Fix | Delete
/**
[232] Fix | Delete
* Update the target post's Likes and Sharing statuses.
[233] Fix | Delete
*
[234] Fix | Delete
* @param WP_Post $source_post Post object to be copied.
[235] Fix | Delete
* @param int $target_post_id Target post ID.
[236] Fix | Delete
* @return array Array with the results of each update action.
[237] Fix | Delete
*/
[238] Fix | Delete
protected function update_likes_sharing( $source_post, $target_post_id ) {
[239] Fix | Delete
$likes = get_post_meta( $source_post->ID, 'switch_like_status', true );
[240] Fix | Delete
$sharing = get_post_meta( $source_post->ID, 'sharing_disabled', true );
[241] Fix | Delete
[242] Fix | Delete
if ( '' !== $likes ) {
[243] Fix | Delete
$likes_result = update_post_meta( $target_post_id, 'switch_like_status', $likes );
[244] Fix | Delete
} else {
[245] Fix | Delete
$likes_result = null;
[246] Fix | Delete
}
[247] Fix | Delete
[248] Fix | Delete
if ( '' !== $sharing ) {
[249] Fix | Delete
$sharing_result = update_post_meta( $target_post_id, 'sharing_disabled', $sharing );
[250] Fix | Delete
} else {
[251] Fix | Delete
$sharing_result = null;
[252] Fix | Delete
}
[253] Fix | Delete
[254] Fix | Delete
return array(
[255] Fix | Delete
'likes' => $likes_result,
[256] Fix | Delete
'sharing' => $sharing_result,
[257] Fix | Delete
);
[258] Fix | Delete
}
[259] Fix | Delete
[260] Fix | Delete
/**
[261] Fix | Delete
* Update the target post's title.
[262] Fix | Delete
*
[263] Fix | Delete
* @param string $post_title Post title determined by `get_default_post_to_edit()`.
[264] Fix | Delete
* @param WP_Post $post Post object of newly-inserted post.
[265] Fix | Delete
* @return string Updated post title from source post.
[266] Fix | Delete
*/
[267] Fix | Delete
public function filter_title( $post_title, $post ) {
[268] Fix | Delete
return $post->post_title;
[269] Fix | Delete
}
[270] Fix | Delete
[271] Fix | Delete
/**
[272] Fix | Delete
* Update the target post's content (`post_content`).
[273] Fix | Delete
*
[274] Fix | Delete
* @param string $post_content Post content determined by `get_default_post_to_edit()`.
[275] Fix | Delete
* @param WP_Post $post Post object of newly-inserted post.
[276] Fix | Delete
* @return string Updated post content from source post.
[277] Fix | Delete
*/
[278] Fix | Delete
public function filter_content( $post_content, $post ) {
[279] Fix | Delete
return $post->post_content;
[280] Fix | Delete
}
[281] Fix | Delete
[282] Fix | Delete
/**
[283] Fix | Delete
* Update the target post's excerpt.
[284] Fix | Delete
*
[285] Fix | Delete
* @param string $post_excerpt Post excerpt determined by `get_default_post_to_edit()`.
[286] Fix | Delete
* @param WP_Post $post Post object of newly-inserted post.
[287] Fix | Delete
* @return string Updated post excerpt from source post.
[288] Fix | Delete
*/
[289] Fix | Delete
public function filter_excerpt( $post_excerpt, $post ) {
[290] Fix | Delete
return $post->post_excerpt;
[291] Fix | Delete
}
[292] Fix | Delete
[293] Fix | Delete
/**
[294] Fix | Delete
* Validate the post type to be used for the target post.
[295] Fix | Delete
*
[296] Fix | Delete
* @param WP_Post $post Post object of current post in listing.
[297] Fix | Delete
* @return bool True if the post type is in a list of supported psot types; false otherwise.
[298] Fix | Delete
*/
[299] Fix | Delete
protected function validate_post_type( $post ) {
[300] Fix | Delete
/**
[301] Fix | Delete
* Fires when determining if the "Copy" row action should be made available.
[302] Fix | Delete
* Allows overriding supported post types.
[303] Fix | Delete
*
[304] Fix | Delete
* @module copy-post
[305] Fix | Delete
*
[306] Fix | Delete
* @since 7.0.0
[307] Fix | Delete
*
[308] Fix | Delete
* @param array Post types supported by default.
[309] Fix | Delete
* @param WP_Post $post Post object of current post in listing.
[310] Fix | Delete
*/
[311] Fix | Delete
$valid_post_types = apply_filters(
[312] Fix | Delete
'jetpack_copy_post_post_types',
[313] Fix | Delete
array(
[314] Fix | Delete
'post',
[315] Fix | Delete
'page',
[316] Fix | Delete
'jetpack-testimonial',
[317] Fix | Delete
'jetpack-portfolio',
[318] Fix | Delete
),
[319] Fix | Delete
$post
[320] Fix | Delete
);
[321] Fix | Delete
return in_array( $post->post_type, $valid_post_types, true );
[322] Fix | Delete
}
[323] Fix | Delete
[324] Fix | Delete
/**
[325] Fix | Delete
* Add a "Copy" row action to supported posts/pages/CPTs on list views.
[326] Fix | Delete
*
[327] Fix | Delete
* @param array $actions Existing actions.
[328] Fix | Delete
* @param WP_Post $post Post object of current post in list.
[329] Fix | Delete
* @return array Array of updated row actions.
[330] Fix | Delete
*/
[331] Fix | Delete
public function add_row_action( $actions, $post ) {
[332] Fix | Delete
if ( ! $this->user_can_access_post( $post->ID ) ||
[333] Fix | Delete
! $post instanceof WP_Post ||
[334] Fix | Delete
! $this->validate_post_type( $post ) ) {
[335] Fix | Delete
return $actions;
[336] Fix | Delete
}
[337] Fix | Delete
[338] Fix | Delete
$edit_url = add_query_arg(
[339] Fix | Delete
array(
[340] Fix | Delete
'post_type' => $post->post_type,
[341] Fix | Delete
'jetpack-copy' => $post->ID,
[342] Fix | Delete
),
[343] Fix | Delete
admin_url( 'post-new.php' )
[344] Fix | Delete
);
[345] Fix | Delete
[346] Fix | Delete
$jetpack_logo = new Logo();
[347] Fix | Delete
$edit_action = array(
[348] Fix | Delete
'jetpack-copy' => sprintf(
[349] Fix | Delete
'<a href="%1$s" aria-label="%2$s">%3$s %4$s</a>',
[350] Fix | Delete
esc_url( $edit_url ),
[351] Fix | Delete
esc_attr__( 'Duplicate this post with Jetpack.', 'jetpack' ),
[352] Fix | Delete
esc_html__( 'Duplicate', 'jetpack' ),
[353] Fix | Delete
$jetpack_logo->get_jp_emblem()
[354] Fix | Delete
),
[355] Fix | Delete
);
[356] Fix | Delete
[357] Fix | Delete
// Insert the Copy action before the Trash action.
[358] Fix | Delete
$edit_offset = array_search( 'trash', array_keys( $actions ), true );
[359] Fix | Delete
$updated_actions = array_merge(
[360] Fix | Delete
array_slice( $actions, 0, $edit_offset ),
[361] Fix | Delete
$edit_action,
[362] Fix | Delete
array_slice( $actions, $edit_offset )
[363] Fix | Delete
);
[364] Fix | Delete
[365] Fix | Delete
/**
[366] Fix | Delete
* Fires after the new Copy action has been added to the row actions.
[367] Fix | Delete
* Allows changes to the action presentation, or other final checks.
[368] Fix | Delete
*
[369] Fix | Delete
* @module copy-post
[370] Fix | Delete
*
[371] Fix | Delete
* @since 7.0.0
[372] Fix | Delete
*
[373] Fix | Delete
* @param array $updated_actions Updated row actions with the Copy Post action.
[374] Fix | Delete
* @param array $actions Original row actions passed to this filter.
[375] Fix | Delete
* @param WP_Post $post Post object of current post in listing.
[376] Fix | Delete
*/
[377] Fix | Delete
return apply_filters( 'jetpack_copy_post_row_actions', $updated_actions, $actions, $post );
[378] Fix | Delete
}
[379] Fix | Delete
}
[380] Fix | Delete
[381] Fix | Delete
/**
[382] Fix | Delete
* Instantiate an instance of Jetpack_Copy_Post on the `admin_init` hook.
[383] Fix | Delete
*/
[384] Fix | Delete
function jetpack_copy_post_init() {
[385] Fix | Delete
new Jetpack_Copy_Post();
[386] Fix | Delete
}
[387] Fix | Delete
add_action( 'admin_init', 'jetpack_copy_post_init' );
[388] Fix | Delete
[389] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function