class-activation.php
2 years ago
class-admin-columns-manager.php
2 years ago
class-admin-menu-organizer.php
2 years ago
class-auto-publish-posts-with-missed-schedule.php
2 years ago
class-avif-upload.php
2 years ago
class-change-login-url.php
2 years ago
class-cleanup-admin-bar.php
2 years ago
class-code-snippets-manager.php
2 years ago
class-common-methods.php
2 years ago
class-content-duplication.php
2 years ago
class-content-order.php
2 years ago
class-custom-admin-footer-text.php
2 years ago
class-custom-body-class.php
2 years ago
class-custom-content-types.php
2 years ago
class-custom-css.php
2 years ago
class-custom-nav-menu-items-in-new-tab.php
2 years ago
class-deactivation.php
2 years ago
class-disable-comments.php
2 years ago
class-disable-dashboard-widgets.php
2 years ago
class-disable-feeds.php
2 years ago
class-disable-gutenberg.php
2 years ago
class-disable-rest-api.php
2 years ago
class-disable-smaller-components.php
2 years ago
class-disable-updates.php
2 years ago
class-disable-xml-rpc.php
2 years ago
class-display-system-summary.php
2 years ago
class-email-address-obfuscator.php
2 years ago
class-email-delivery.php
2 years ago
class-enhance-list-tables.php
2 years ago
class-external-permalinks.php
2 years ago
class-heartbeat-control.php
2 years ago
class-hide-admin-bar.php
2 years ago
class-hide-admin-notices.php
2 years ago
class-image-sizes-panel.php
2 years ago
class-image-upload-control.php
2 years ago
class-insert-head-body-footer-code.php
2 years ago
class-last-login-column.php
2 years ago
class-limit-login-attempts.php
2 years ago
class-login-id-type.php
2 years ago
class-login-logout-menu.php
2 years ago
class-maintenance-mode.php
2 years ago
class-manage-ads-appads-txt.php
2 years ago
class-manage-robots-txt.php
2 years ago
class-media-replacement.php
2 years ago
class-multiple-user-roles.php
2 years ago
class-obfuscate-author-slugs.php
2 years ago
class-open-external-links-in-new-tab.php
2 years ago
class-password-protection.php
2 years ago
class-redirect-after-login.php
2 years ago
class-redirect-after-logout.php
2 years ago
class-redirect-fourofour.php
2 years ago
class-revisions-control.php
2 years ago
class-search-engines-visibility.php
2 years ago
class-settings-fields-render.php
2 years ago
class-settings-sanitization.php
2 years ago
class-settings-sections-fields.php
2 years ago
class-show-custom-taxonomy-filters.php
2 years ago
class-site-identity-on-login-page.php
2 years ago
class-svg-upload.php
2 years ago
class-various-admin-ui-enhancements.php
2 years ago
class-view-admin-as-role.php
2 years ago
class-wider-admin-menu.php
2 years ago
class-content-duplication.php
217 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ASENHA\Classes; |
| 4 | |
| 5 | use WP_Admin_Bar; |
| 6 | use WC_Admin_Duplicate_Product; |
| 7 | /** |
| 8 | * Class for Content Duplication module |
| 9 | * |
| 10 | * @since 6.9.5 |
| 11 | */ |
| 12 | class Content_Duplication { |
| 13 | /** |
| 14 | * Enable duplication of pages, posts and custom posts |
| 15 | * |
| 16 | * @since 1.0.0 |
| 17 | */ |
| 18 | public function duplicate_content() { |
| 19 | $allow_duplication = false; |
| 20 | if ( current_user_can( 'edit_posts' ) ) { |
| 21 | $allow_duplication = true; |
| 22 | } |
| 23 | $original_post_id = intval( sanitize_text_field( $_REQUEST['post'] ) ); |
| 24 | $nonce = sanitize_text_field( $_REQUEST['nonce'] ); |
| 25 | if ( wp_verify_nonce( $nonce, 'asenha-duplicate-' . $original_post_id ) && $allow_duplication ) { |
| 26 | $original_post = get_post( $original_post_id ); |
| 27 | $post_type = $original_post->post_type; |
| 28 | $common_methods = new Common_Methods(); |
| 29 | $is_woocommerce_active = $common_methods->is_woocommerce_active(); |
| 30 | if ( 'product' != $post_type || 'product' == $post_type && !$is_woocommerce_active ) { |
| 31 | // Set some attributes for the duplicate post |
| 32 | $new_post_title_suffix = __( 'DUPLICATE', 'admin-site-enhancements' ); |
| 33 | $new_post_status = 'draft'; |
| 34 | $current_user = wp_get_current_user(); |
| 35 | $new_post_author_id = $current_user->ID; |
| 36 | // Create the duplicate post and store the ID |
| 37 | $args = array( |
| 38 | 'comment_status' => $original_post->comment_status, |
| 39 | 'ping_status' => $original_post->ping_status, |
| 40 | 'post_author' => $new_post_author_id, |
| 41 | 'post_content' => str_replace( '\\', "\\\\", $original_post->post_content ), |
| 42 | 'post_excerpt' => $original_post->post_excerpt, |
| 43 | 'post_parent' => $original_post->post_parent, |
| 44 | 'post_password' => $original_post->post_password, |
| 45 | 'post_status' => $new_post_status, |
| 46 | 'post_title' => $original_post->post_title . ' (' . $new_post_title_suffix . ')', |
| 47 | 'post_type' => $original_post->post_type, |
| 48 | 'to_ping' => $original_post->to_ping, |
| 49 | 'menu_order' => $original_post->menu_order, |
| 50 | ); |
| 51 | $new_post_id = wp_insert_post( $args ); |
| 52 | // Copy over the taxonomies |
| 53 | $original_taxonomies = get_object_taxonomies( $original_post->post_type ); |
| 54 | if ( !empty( $original_taxonomies ) && is_array( $original_taxonomies ) ) { |
| 55 | foreach ( $original_taxonomies as $taxonomy ) { |
| 56 | $original_post_terms = wp_get_object_terms( $original_post_id, $taxonomy, array( |
| 57 | 'fields' => 'slugs', |
| 58 | ) ); |
| 59 | wp_set_object_terms( |
| 60 | $new_post_id, |
| 61 | $original_post_terms, |
| 62 | $taxonomy, |
| 63 | false |
| 64 | ); |
| 65 | } |
| 66 | } |
| 67 | // Copy over the post meta |
| 68 | $original_post_metas = get_post_meta( $original_post_id ); |
| 69 | // all meta keys and the corresponding values |
| 70 | if ( !empty( $original_post_metas ) ) { |
| 71 | foreach ( $original_post_metas as $meta_key => $meta_values ) { |
| 72 | foreach ( $meta_values as $meta_value ) { |
| 73 | update_post_meta( $new_post_id, $meta_key, wp_slash( maybe_unserialize( $meta_value ) ) ); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 79 | $duplication_redirect_destination = ( isset( $options['duplication_redirect_destination'] ) ? $options['duplication_redirect_destination'] : 'edit' ); |
| 80 | switch ( $duplication_redirect_destination ) { |
| 81 | case 'edit': |
| 82 | // Redirect to edit screen of the duplicate post |
| 83 | wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) ); |
| 84 | break; |
| 85 | case 'list': |
| 86 | // Redirect to list table of the corresponding post type of original post |
| 87 | if ( 'post' == $post_type ) { |
| 88 | wp_redirect( admin_url( 'edit.php' ) ); |
| 89 | } else { |
| 90 | wp_redirect( admin_url( 'edit.php?post_type=' . $post_type ) ); |
| 91 | } |
| 92 | break; |
| 93 | } |
| 94 | } else { |
| 95 | wp_die( 'You do not have permission to perform this action.' ); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Add row action link to perform duplication in page/post list tables |
| 101 | * |
| 102 | * @since 1.0.0 |
| 103 | */ |
| 104 | public function add_duplication_action_link( $actions, $post ) { |
| 105 | $duplication_link_locations = $this->get_duplication_link_locations(); |
| 106 | $allow_duplication = $this->is_user_allowed_to_duplicate_content(); |
| 107 | $post_type = $post->post_type; |
| 108 | $post_type_is_duplicable = $this->is_post_type_duplicable( $post_type ); |
| 109 | if ( $allow_duplication && $post_type_is_duplicable ) { |
| 110 | // Not WooCommerce product |
| 111 | if ( in_array( 'post-action', $duplication_link_locations ) ) { |
| 112 | $actions['asenha-duplicate'] = '<a href="admin.php?action=duplicate_content&post=' . $post->ID . '&nonce=' . wp_create_nonce( 'asenha-duplicate-' . $post->ID ) . '" title="' . __( 'Duplicate this as draft', 'admin-site-enhancements' ) . '">' . __( 'Duplicate', 'admin-site-enhancements' ) . '</a>'; |
| 113 | } |
| 114 | } |
| 115 | return $actions; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Add admin bar duplicate link |
| 120 | * |
| 121 | * @since 6.3.0 |
| 122 | */ |
| 123 | public function add_admin_bar_duplication_link( WP_Admin_Bar $wp_admin_bar ) { |
| 124 | $duplication_link_locations = $this->get_duplication_link_locations(); |
| 125 | $allow_duplication = $this->is_user_allowed_to_duplicate_content(); |
| 126 | global $pagenow, $typenow, $post; |
| 127 | $inapplicable_post_types = array('attachment'); |
| 128 | $post_type_is_duplicable = $this->is_post_type_duplicable( $typenow ); |
| 129 | if ( $allow_duplication && $post_type_is_duplicable ) { |
| 130 | if ( 'post.php' == $pagenow && !in_array( $typenow, $inapplicable_post_types ) || is_singular() ) { |
| 131 | if ( in_array( 'admin-bar', $duplication_link_locations ) ) { |
| 132 | if ( is_object( $post ) ) { |
| 133 | $common_methods = new Common_Methods(); |
| 134 | $post_type_singular_label = $common_methods->get_post_type_singular_label( $post ); |
| 135 | if ( property_exists( $post, 'ID' ) ) { |
| 136 | $wp_admin_bar->add_menu( array( |
| 137 | 'id' => 'duplicate-content', |
| 138 | 'parent' => null, |
| 139 | 'group' => null, |
| 140 | 'title' => sprintf( |
| 141 | /* translators: %s is the singular label for the post type */ |
| 142 | __( 'Duplicate %s', 'admin-site-enhancements' ), |
| 143 | $post_type_singular_label |
| 144 | ), |
| 145 | 'href' => admin_url( 'admin.php?action=duplicate_content&post=' . $post->ID . '&nonce=' . wp_create_nonce( 'asenha-duplicate-' . $post->ID ) ), |
| 146 | ) ); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Check at which locations duplication link should enabled |
| 156 | * |
| 157 | * @since 6.9.3 |
| 158 | */ |
| 159 | public function get_duplication_link_locations() { |
| 160 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 161 | $duplication_link_locations = array('post-action', 'admin-bar'); |
| 162 | return $duplication_link_locations; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Check if a user role is allowed to duplicate content |
| 167 | * |
| 168 | * @since 6.9.3 |
| 169 | */ |
| 170 | public function is_user_allowed_to_duplicate_content() { |
| 171 | $allow_duplication = false; |
| 172 | if ( current_user_can( 'edit_posts' ) ) { |
| 173 | $allow_duplication = true; |
| 174 | } |
| 175 | return $allow_duplication; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Check if the post type can be duplicated |
| 180 | * |
| 181 | * @since 6.9.7 |
| 182 | */ |
| 183 | public function is_post_type_duplicable( $post_type ) { |
| 184 | global $asenha_public_post_types; |
| 185 | $common_methods = new Common_Methods(); |
| 186 | $is_woocommerce_active = $common_methods->is_woocommerce_active(); |
| 187 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 188 | $enable_duplication_on_post_types_type = 'only-on'; |
| 189 | $asenha_public_post_types_slugs = array(); |
| 190 | if ( is_array( $asenha_public_post_types ) ) { |
| 191 | foreach ( $asenha_public_post_types as $post_type_slug => $post_type_label ) { |
| 192 | // e.g. $post_type_slug is post, |
| 193 | $asenha_public_post_types_slugs[] = $post_type_slug; |
| 194 | } |
| 195 | } |
| 196 | $enable_duplication_on_post_types = ( isset( $options['enable_duplication_on_post_types'] ) ? $options['enable_duplication_on_post_types'] : array() ); |
| 197 | $post_types_for_enable_duplication = array(); |
| 198 | if ( !empty( $enable_duplication_on_post_types ) && count( $enable_duplication_on_post_types ) > 0 ) { |
| 199 | foreach ( $enable_duplication_on_post_types as $post_type_slug => $is_duplication_enabled ) { |
| 200 | if ( $is_duplication_enabled ) { |
| 201 | $post_types_for_enable_duplication[] = $post_type_slug; |
| 202 | } |
| 203 | } |
| 204 | } else { |
| 205 | $post_types_for_enable_duplication = $asenha_public_post_types_slugs; |
| 206 | } |
| 207 | if ( 'only-on' == $enable_duplication_on_post_types_type && in_array( $post_type, $post_types_for_enable_duplication ) || 'except-on' == $enable_duplication_on_post_types_type && !in_array( $post_type, $post_types_for_enable_duplication ) ) { |
| 208 | if ( 'product' != $post_type || 'product' == $post_type && !$is_woocommerce_active ) { |
| 209 | return true; |
| 210 | } |
| 211 | } else { |
| 212 | return false; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | } |
| 217 |