class-gallery-api.php
4 months ago
class-gallery-base.php
4 months ago
class-gallery-config.php
4 months ago
class-gallery-design.php
4 months ago
class-gallery-field-provider.php
4 months ago
class-gallery-images.php
4 months ago
class-gallery-lightbox.php
4 months ago
class-gallery-misc.php
4 months ago
class-gallery-paging.php
4 months ago
trait-gallery-ajax.php
4 months ago
trait-gallery-duplicate.php
4 months ago
trait-gallery-image-methods.php
2 weeks ago
trait-gallery-preview.php
4 months ago
trait-gallery-sanitize.php
4 months ago
trait-gallery-duplicate.php
197 lines
| 1 | <?php |
| 2 | // exit if accessed directly |
| 3 | if ( ! defined( 'ABSPATH' ) ) |
| 4 | exit; |
| 5 | |
| 6 | /** |
| 7 | * Responsive Lightbox Gallery Duplication Trait. |
| 8 | * |
| 9 | * Handles gallery duplication functionality. |
| 10 | * |
| 11 | * @trait Responsive_Lightbox_Gallery_Duplicate |
| 12 | */ |
| 13 | trait Responsive_Lightbox_Gallery_Duplicate { |
| 14 | |
| 15 | /** |
| 16 | * Duplicate gallery action in admin. |
| 17 | * |
| 18 | * @return void |
| 19 | */ |
| 20 | public function duplicate_gallery() { |
| 21 | if ( ! ( isset( $_GET['post'] ) || isset( $_POST['post'] ) ) || ! isset( $_REQUEST['action'] ) || ! isset( $_REQUEST['rl_gallery_nonce'] ) || ( isset( $_REQUEST['rl_gallery_nonce'] ) && ! wp_verify_nonce( $_REQUEST['rl_gallery_nonce'], 'responsive-lightbox-duplicate-gallery' ) ) ) |
| 22 | wp_die( esc_html__( 'No gallery to duplicate has been supplied!', 'responsive-lightbox' ) ); |
| 23 | |
| 24 | // get the original post |
| 25 | $post_id = isset( $_GET['post'] ) ? (int) $_GET['post'] : ( isset( $_POST['post'] ) ? (int) $_POST['post'] : 0 ); |
| 26 | |
| 27 | if ( empty( $post_id ) ) |
| 28 | wp_die( esc_html__( 'No gallery to duplicate has been supplied!', 'responsive-lightbox' ) ); |
| 29 | |
| 30 | if ( ! current_user_can( 'edit_post', $post_id ) ) |
| 31 | wp_die( esc_html__( 'You do not have permission to copy this gallery.', 'responsive-lightbox' ) ); |
| 32 | |
| 33 | $post = get_post( $post_id ); |
| 34 | |
| 35 | // copy the post and insert it |
| 36 | if ( isset( $post ) && $post !== null ) { |
| 37 | $this->create_gallery_duplicate( $post ); |
| 38 | |
| 39 | // redirect to the post list screen |
| 40 | wp_redirect( admin_url( 'edit.php?post_type=' . $post->post_type ) ); |
| 41 | exit; |
| 42 | } else |
| 43 | wp_die( esc_html__( 'Copy creation failed, could not find original gallery:', 'responsive-lightbox' ) . ' ' . (int) $post_id ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Add duplicate link to gallery listing. |
| 48 | * |
| 49 | * @global string $pagenow |
| 50 | * |
| 51 | * @param array $actions Link actions |
| 52 | * @param object $post Post object |
| 53 | * @return array |
| 54 | */ |
| 55 | public function post_row_actions_duplicate( $actions, $post ) { |
| 56 | global $pagenow; |
| 57 | |
| 58 | if ( $post->post_type !== 'rl_gallery' ) |
| 59 | return $actions; |
| 60 | |
| 61 | if ( ! current_user_can( 'edit_post', $post->ID ) ) |
| 62 | return $actions; |
| 63 | |
| 64 | // duplicate link |
| 65 | $actions['duplicate_gallery'] = '<a class="duplicate-gallery" title="' . esc_attr__( 'Duplicate this item', 'responsive-lightbox' ) . '" href="' . esc_url( wp_nonce_url( admin_url( $pagenow . '?post=' . $post->ID . '&action=duplicate_gallery' ), 'responsive-lightbox-duplicate-gallery', 'rl_gallery_nonce' ) ) . '">' . esc_html__( 'Duplicate', 'responsive-lightbox' ) . '</a>'; |
| 66 | |
| 67 | return $actions; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Create a gallery duplicate. |
| 72 | * |
| 73 | * @param $post object Post object |
| 74 | * @return void|int |
| 75 | */ |
| 76 | public function create_gallery_duplicate( $post ) { |
| 77 | // skip revisions |
| 78 | if ( $post->post_type === 'revision' ) |
| 79 | return; |
| 80 | |
| 81 | $new_post = apply_filters( |
| 82 | 'rl_duplicate_gallery_args', |
| 83 | [ |
| 84 | 'menu_order' => $post->menu_order, |
| 85 | 'comment_status' => $post->comment_status, |
| 86 | 'ping_status' => $post->ping_status, |
| 87 | 'post_author' => $post->post_author, |
| 88 | 'post_content' => $post->post_content, |
| 89 | 'post_excerpt' => $post->post_excerpt, |
| 90 | 'post_mime_type' => $post->post_mime_type, |
| 91 | 'post_parent' => $post->post_parent, |
| 92 | 'post_password' => $post->post_password, |
| 93 | 'post_status' => $post->post_status, |
| 94 | 'post_title' => $post->post_title, |
| 95 | 'post_type' => $post->post_type, |
| 96 | 'post_date' => current_time( 'mysql' ), |
| 97 | 'post_date_gmt' => get_gmt_from_date( current_time( 'mysql' ) ) |
| 98 | ], |
| 99 | $post |
| 100 | ); |
| 101 | |
| 102 | $new_post_id = wp_insert_post( $new_post ); |
| 103 | |
| 104 | // if the copy is published or scheduled, we have to set a proper slug |
| 105 | if ( $new_post['post_status'] === 'publish' || $new_post['post_status'] === 'future' ) { |
| 106 | $post_name = wp_unique_post_slug( $post->post_name, $new_post_id, $new_post['post_status'], $post->post_type, $new_post['post_parent'] ); |
| 107 | |
| 108 | $new_post = []; |
| 109 | $new_post['ID'] = $new_post_id; |
| 110 | $new_post['post_name'] = $post_name; |
| 111 | |
| 112 | // update the post into the database |
| 113 | wp_update_post( $new_post ); |
| 114 | } |
| 115 | |
| 116 | // create metadata for the duplicated gallery |
| 117 | $this->create_gallery_duplicate_metadata( $new_post_id, $post ); |
| 118 | |
| 119 | // copy taxonomies |
| 120 | $this->duplicate_gallery_taxonomies( $new_post_id, $post ); |
| 121 | |
| 122 | // action hook for developers |
| 123 | do_action( 'rl_after_duplicate_gallery', $new_post_id, $post ); |
| 124 | |
| 125 | return $new_post_id; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Create a gallery duplicate metadata. |
| 130 | * |
| 131 | * @param int $new_post_id Post ID |
| 132 | * @param object $post Post object |
| 133 | * @return void |
| 134 | */ |
| 135 | public function create_gallery_duplicate_metadata( $new_post_id, $post ) { |
| 136 | if ( empty( $post ) || $post == null ) |
| 137 | return; |
| 138 | |
| 139 | // meta keys to be copied |
| 140 | $meta_keys = apply_filters( 'rl_duplicate_gallery_meta_keys', get_post_custom_keys( $post->ID ) ); |
| 141 | |
| 142 | if ( empty( $meta_keys ) ) |
| 143 | return; |
| 144 | |
| 145 | foreach ( $meta_keys as $meta_key ) { |
| 146 | // meta values to be copied |
| 147 | $meta_values = apply_filters( 'rl_duplicate_gallery_meta_values', get_post_custom_values( $meta_key, $post->ID ) ); |
| 148 | |
| 149 | foreach ( $meta_values as $meta_value ) { |
| 150 | $meta_value = maybe_unserialize( $meta_value ); |
| 151 | |
| 152 | // add metadata to duplicated post |
| 153 | add_post_meta( $new_post_id, $meta_key, $meta_value ); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Copy the taxonomies of a gallery to another gallery. |
| 160 | * |
| 161 | * @global object $wpdb |
| 162 | * |
| 163 | * @param int $new_post_id Post ID |
| 164 | * @param object $post Post object |
| 165 | * @return void |
| 166 | */ |
| 167 | function duplicate_gallery_taxonomies( $new_post_id, $post ) { |
| 168 | global $wpdb; |
| 169 | |
| 170 | if ( isset( $wpdb->terms ) ) { |
| 171 | // clear default category |
| 172 | wp_set_object_terms( $new_post_id, null, 'category' ); |
| 173 | |
| 174 | // get gallery taxonomies |
| 175 | $gallery_taxonomies = get_object_taxonomies( $post->post_type ); |
| 176 | |
| 177 | if ( ! empty( $gallery_taxonomies ) ) { |
| 178 | foreach ( $gallery_taxonomies as $taxonomy ) { |
| 179 | $terms = []; |
| 180 | |
| 181 | // get taxonomy terms |
| 182 | $post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'orderby' => 'term_order' ) ); |
| 183 | |
| 184 | if ( ! empty( $post_terms ) ) { |
| 185 | foreach ( $post_terms as $term ) { |
| 186 | $terms[] = $term->slug; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // copy taxonomy terms |
| 191 | wp_set_object_terms( $new_post_id, $terms, $taxonomy ); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 |