Custom_JS.php
1 year ago
Hover_Effect.php
1 year ago
Post_Duplicator.php
1 year ago
Promotion.php
1 year ago
Reading_Progress.php
3 years ago
Scroll_to_Top.php
1 year ago
Table_of_Content.php
1 year ago
Wrapper_Link.php
1 year ago
index.php
3 years ago
Post_Duplicator.php
171 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Essential_Addons_Elementor\Extensions; |
| 4 | |
| 5 | if ( ! defined( 'ABSPATH' ) ) { |
| 6 | exit; |
| 7 | } |
| 8 | |
| 9 | class Post_Duplicator { |
| 10 | public function __construct() { |
| 11 | |
| 12 | add_filter( 'admin_action_eae_duplicate', array( $this, 'duplicate' ) ); |
| 13 | add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu' ), 10000 ); |
| 14 | add_filter( 'post_row_actions', array( $this, 'row_actions' ), 10, 2 ); |
| 15 | add_filter( 'page_row_actions', array( $this, 'row_actions' ), 10, 2 ); |
| 16 | |
| 17 | } |
| 18 | |
| 19 | public function admin_bar_menu( $wp_admin_bar ) { |
| 20 | |
| 21 | global $pagenow; |
| 22 | global $post; |
| 23 | |
| 24 | $enabled_on = get_option( 'eael_save_post_duplicator_post_type', 'all' ); |
| 25 | |
| 26 | if ( ! is_admin() || $pagenow !== 'post.php' || ( $enabled_on != 'all' || $post->post_type != $enabled_on ) ) { |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | $duplicate_url = admin_url( 'admin.php?action=eae_duplicate&post=' . $post->ID ); |
| 31 | $duplicate_url = wp_nonce_url( $duplicate_url, 'ea_duplicator' ); |
| 32 | $wp_admin_bar->add_menu( |
| 33 | array( |
| 34 | 'id' => 'eae-duplicator', |
| 35 | 'title' => __( 'EA Duplicator', 'essential-addons-for-elementor-lite' ), |
| 36 | 'href' => $duplicate_url |
| 37 | ) |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * EA Duplicator Button added in table row |
| 43 | * |
| 44 | * @param array $actions |
| 45 | * @param WP_Post $post |
| 46 | * |
| 47 | * @return array |
| 48 | */ |
| 49 | public function row_actions( $actions, $post ) { |
| 50 | |
| 51 | $enabled_on = get_option( 'eael_save_post_duplicator_post_type', 'all' ); |
| 52 | |
| 53 | if ( current_user_can( 'edit_posts' ) && ( $enabled_on == 'all' || $post->post_type == $enabled_on ) ) { |
| 54 | $duplicate_url = admin_url( 'admin.php?action=eae_duplicate&post=' . $post->ID ); |
| 55 | $duplicate_url = wp_nonce_url( $duplicate_url, 'ea_duplicator' ); |
| 56 | $actions['eae_duplicate'] = sprintf( '<a href="%s" title="%s">%s</a>', $duplicate_url, __( 'Duplicate ' . esc_attr( $post->post_title ), 'essential-addons-for-elementor-lite' ), __( 'EA Duplicator', 'essential-addons-for-elementor-lite' ) ); |
| 57 | } |
| 58 | |
| 59 | return $actions; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Duplicate a post |
| 64 | * @return void |
| 65 | */ |
| 66 | public function duplicate() { |
| 67 | |
| 68 | $nonce = isset( $_REQUEST['_wpnonce'] ) && ! empty( $_REQUEST['_wpnonce'] ) ? $_REQUEST['_wpnonce'] : null; |
| 69 | $post_id = isset( $_REQUEST['post'] ) && ! empty( $_REQUEST['post'] ) ? intval( $_REQUEST['post'] ) : null; |
| 70 | $action = isset( $_REQUEST['action'] ) && ! empty( $_REQUEST['action'] ) ? trim( sanitize_text_field( $_REQUEST['action'] ) ) : null; |
| 71 | |
| 72 | if ( is_null( $nonce ) || is_null( $post_id ) || $action !== 'eae_duplicate' ) { |
| 73 | return; // Return if action is not eae_duplicate |
| 74 | } |
| 75 | |
| 76 | if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'ea_duplicator' ) ) { |
| 77 | return; // Return if nonce is not valid |
| 78 | } |
| 79 | |
| 80 | $post = sanitize_post( get_post( $post_id ), 'db' ); |
| 81 | |
| 82 | if ( is_null( $post ) ) { |
| 83 | return; // Return if post is not there. |
| 84 | } |
| 85 | |
| 86 | $current_user = wp_get_current_user(); |
| 87 | $allowed_roles = array('editor', 'administrator', 'author'); |
| 88 | $redirect_url = admin_url( 'edit.php?post_type=' . $post->post_type ); |
| 89 | |
| 90 | if ( ! array_intersect( $allowed_roles, $current_user->roles ) ) { |
| 91 | switch ( $post->post_type ) { |
| 92 | case 'post': |
| 93 | $can_edit_others_posts = current_user_can('edit_others_posts'); |
| 94 | break; |
| 95 | case 'page': |
| 96 | $can_edit_others_posts = current_user_can('edit_others_pages'); |
| 97 | break; |
| 98 | default : |
| 99 | $can_edit_others_posts = current_user_can('edit_others_posts'); |
| 100 | break; |
| 101 | } |
| 102 | |
| 103 | if ( $current_user->ID !== $post->post_author && ! $can_edit_others_posts ){ |
| 104 | wp_safe_redirect( $redirect_url ); |
| 105 | return; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | $duplicate_post_args = array( |
| 110 | 'post_author' => $current_user->ID, |
| 111 | 'post_title' => $post->post_title . ' - Copy', |
| 112 | 'post_content' => $post->post_content, |
| 113 | 'post_excerpt' => $post->post_excerpt, |
| 114 | 'post_parent' => $post->post_parent, |
| 115 | 'post_status' => 'draft', |
| 116 | 'ping_status' => $post->ping_status, |
| 117 | 'comment_status' => $post->comment_status, |
| 118 | 'post_password' => $post->post_password, |
| 119 | 'post_type' => $post->post_type, |
| 120 | 'to_ping' => $post->to_ping, |
| 121 | 'menu_order' => $post->menu_order, |
| 122 | ); |
| 123 | $duplicated_id = wp_insert_post( $duplicate_post_args ); |
| 124 | |
| 125 | if ( ! is_wp_error( $duplicated_id ) ) { |
| 126 | $taxonomies = get_object_taxonomies( $post->post_type ); |
| 127 | if ( ! empty( $taxonomies ) && is_array( $taxonomies ) ) { |
| 128 | foreach ( $taxonomies as $taxonomy ) { |
| 129 | $post_terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'slugs' ) ); |
| 130 | wp_set_object_terms( $duplicated_id, $post_terms, $taxonomy, false ); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | global $wpdb; |
| 135 | $post_meta = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id = %d", $post_id ) ); |
| 136 | |
| 137 | if ( ! empty( $post_meta ) && is_array( $post_meta ) ) { |
| 138 | |
| 139 | $duplicate_insert_query = "INSERT INTO $wpdb->postmeta ( post_id, meta_key, meta_value ) VALUES "; |
| 140 | $insert = ''; |
| 141 | |
| 142 | foreach ( $post_meta as $meta_info ) { |
| 143 | |
| 144 | $meta_key = sanitize_text_field( $meta_info->meta_key ); |
| 145 | $meta_value = $meta_info->meta_value; |
| 146 | |
| 147 | $exclude_meta_keys = [ '_wc_average_rating', '_wc_review_count', '_wc_rating_count', '_elementor_css' ]; |
| 148 | |
| 149 | if( in_array($meta_key, $exclude_meta_keys) ){ |
| 150 | continue; |
| 151 | } |
| 152 | |
| 153 | if ( $meta_key === '_elementor_template_type' ) { |
| 154 | delete_post_meta( $duplicated_id, '_elementor_template_type' ); |
| 155 | } |
| 156 | |
| 157 | if ( ! empty( $insert ) ) { |
| 158 | $insert .= ', '; |
| 159 | } |
| 160 | |
| 161 | $insert .= $wpdb->prepare( '(%d, %s, %s)', $duplicated_id, $meta_key, $meta_value ); |
| 162 | } |
| 163 | |
| 164 | $wpdb->query( $duplicate_insert_query . $insert ); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | wp_safe_redirect( $redirect_url ); |
| 169 | } |
| 170 | } |
| 171 |