array('_edit_lock', '_edit_last', '_wp_old_slug', '_wp_old_date'), 'aieo_popup' => array( '_aieo_pop_surface', '_aieo_pop_test', ), 'aieo_competition' => array( '_aieo_comp_tnp_list_id', '_aieo_comp_winners_announcement', '_aieo_comp_gf_form_id', ), 'aieo_event' => array( '_aieo_event_gf_form_id', ), 'aieo_store' => array( '_aieo_grv_location_id', ), ); } public static function skip_keys($post_type) { $map = self::skip_map(); $keys = array_merge( isset($map['*']) ? $map['*'] : array(), isset($map[$post_type]) ? $map[$post_type] : array() ); return (array) apply_filters('aieo_duplicate_skip_meta', $keys, $post_type); } public static function register() { static $done = false; if ($done) { return; } $done = true; add_action('admin_post_' . self::ACTION, array(__CLASS__, 'handle')); add_filter('post_row_actions', array(__CLASS__, 'row_action'), 10, 2); add_filter('page_row_actions', array(__CLASS__, 'row_action'), 10, 2); } public static function row_action($actions, $post) { if (!($post instanceof WP_Post) || !in_array($post->post_type, self::supported_types(), true)) { return $actions; } $type = get_post_type_object($post->post_type); $cap = ($type && isset($type->cap->edit_posts)) ? $type->cap->edit_posts : 'edit_posts'; if (!current_user_can($cap)) { return $actions; } $actions['aieo_duplicate'] = '' . esc_html__('Duplicate', 'ai-eshop-optimizer') . ''; return $actions; } public static function url($post_id) { return wp_nonce_url( add_query_arg( array('action' => self::ACTION, 'post' => (int) $post_id), admin_url('admin-post.php') ), self::ACTION . '_' . (int) $post_id ); } public static function duplicate($post_id) { $post = get_post((int) $post_id); if (!$post) { return new WP_Error('aieo_dup_missing', __('That item no longer exists.', 'ai-eshop-optimizer')); } $title = trim((string) $post->post_title); if ($title === '') { $title = __('(no title)', 'ai-eshop-optimizer'); } $new_id = wp_insert_post(array( 'post_type' => $post->post_type, 'post_status' => 'draft', 'post_title' => sprintf( __('%s (copy)', 'ai-eshop-optimizer'), $title ), 'post_content' => $post->post_content, 'post_excerpt' => $post->post_excerpt, 'post_parent' => $post->post_parent, 'menu_order' => $post->menu_order, 'comment_status' => $post->comment_status, 'ping_status' => $post->ping_status, 'post_author' => get_current_user_id(), 'post_name' => '', ), true); if (is_wp_error($new_id)) { return $new_id; } $skip = self::skip_keys($post->post_type); foreach (get_post_meta($post->ID) as $key => $values) { if (in_array($key, $skip, true)) { continue; } foreach ((array) $values as $v) { add_post_meta($new_id, $key, maybe_unserialize($v)); } } foreach (get_object_taxonomies($post->post_type) as $tax) { $terms = wp_get_object_terms($post->ID, $tax, array('fields' => 'ids')); if (!is_wp_error($terms) && $terms) { wp_set_object_terms($new_id, $terms, $tax); } } do_action('aieo_post_duplicated', $new_id, $post->ID, $post->post_type); return $new_id; } public static function handle() { $pid = isset($_GET['post']) ? (int) $_GET['post'] : 0; check_admin_referer(self::ACTION . '_' . $pid); $post = $pid > 0 ? get_post($pid) : null; if (!$post || !in_array($post->post_type, self::supported_types(), true)) { wp_die(esc_html__('That item cannot be duplicated.', 'ai-eshop-optimizer'), '', array('response' => 400)); } $cap = defined('AIEO_CAPABILITY') ? AIEO_CAPABILITY : 'manage_options'; if (!current_user_can($cap) || !current_user_can('edit_post', $pid)) { wp_die(esc_html__('You are not allowed to duplicate this item.', 'ai-eshop-optimizer'), '', array('response' => 403)); } $new_id = self::duplicate($pid); if (is_wp_error($new_id)) { wp_die(esc_html($new_id->get_error_message()), '', array('response' => 500)); } wp_safe_redirect(get_edit_post_link($new_id, '')); exit; } } }