| @@ -1,40 +1,48 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Export snippet | |
| 4 | - * | |
| 5 | - * @author Webcraftic <[email protected]> | |
| 6 | - * @copyright (c) 16.11.2018, Webcraftic | |
| 7 | - * @version 1.0 | |
| 3 | + * Export/clone snippet | |
| 4 | + | |
| 5 | + * @package Woody_Code_Snippets | |
| 8 | 6 | */ |
| 9 | 7 | |
| 10 | -// Exit if accessed directly | |
| 8 | +// Exit if accessed directly. | |
| 11 | 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | 10 | exit; |
| 13 | 11 | } |
| 14 | 12 | |
| 15 | -class WINP_Export_Snippet { | |
| 13 | +/** | |
| 14 | + * Class WINP_Actions_Snippet | |
| 15 | + */ | |
| 16 | +class WINP_Actions_Snippet { | |
| 16 | 17 | |
| 17 | 18 | /** |
| 18 | - * WINP_Export_Snippet constructor. | |
| 19 | + * WINP_Actions_Snippet constructor. | |
| 19 | 20 | */ |
| 20 | 21 | public function __construct() { |
| 21 | - $this->registerHooks(); | |
| 22 | + $this->register_hooks(); | |
| 22 | 23 | } |
| 23 | 24 | |
| 24 | 25 | /** |
| 25 | 26 | * Register hooks |
| 27 | + * | |
| 28 | + * @return void | |
| 26 | 29 | */ |
| 27 | - public function registerHooks() { | |
| 28 | - add_filter( 'post_row_actions', [ $this, 'postRowActions' ], 10, 2 ); | |
| 29 | - add_filter( 'bulk_actions-edit-' . WINP_SNIPPETS_POST_TYPE, [ $this, 'actionBulkEditPost' ] ); | |
| 30 | - add_filter( 'handle_bulk_actions-edit-' . WINP_SNIPPETS_POST_TYPE, [ | |
| 31 | - $this, | |
| 32 | - 'handleActionBulkEditPost', | |
| 33 | - ], 10, 3 ); | |
| 30 | + public function register_hooks() { | |
| 31 | + add_filter( 'post_row_actions', [ $this, 'post_row_actions' ], 10, 2 ); | |
| 32 | + add_filter( 'bulk_actions-edit-' . WINP_SNIPPETS_POST_TYPE, [ $this, 'action_bulk_edit_post' ] ); | |
| 33 | + add_filter( | |
| 34 | + 'handle_bulk_actions-edit-' . WINP_SNIPPETS_POST_TYPE, | |
| 35 | + [ | |
| 36 | + $this, | |
| 37 | + 'handle_action_bulk_edit_post', | |
| 38 | + ], | |
| 39 | + 10, | |
| 40 | + 3 | |
| 41 | + ); | |
| 34 | 42 | |
| 35 | - add_action( 'post_submitbox_start', [ $this, 'postSubmitboxStart' ] ); | |
| 36 | - add_action( 'admin_init', [ $this, 'adminInit' ] ); | |
| 43 | + add_action( 'post_submitbox_start', [ $this, 'post_submitbox_start' ] ); | |
| 44 | + add_action( 'admin_init', [ $this, 'admin_init' ] ); | |
| 37 | 45 | add_action( 'current_screen', [ $this, 'current_screen' ] ); |
| 38 | 46 | add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] ); |
| 39 | 47 | } |
| 40 | 48 | |
| @@ -40,9 +48,9 @@ | ||
| 40 | 48 | |
| 41 | 49 | /** |
| 42 | 50 | * Get export url |
| 43 | 51 | * |
| 44 | - * @param int $post_id | |
| 52 | + * @param int $post_id Post ID. | |
| 45 | 53 | * |
| 46 | 54 | * @return string |
| 47 | 55 | */ |
| 48 | 56 | private function get_export_url( $post_id ) { |
| @@ -47,18 +55,21 @@ | ||
| 47 | 55 | */ |
| 48 | 56 | private function get_export_url( $post_id ) { |
| 49 | 57 | $url = admin_url( 'post.php?post=' . $post_id ); |
| 50 | 58 | |
| 51 | - return add_query_arg( [ | |
| 52 | - 'action' => 'export', | |
| 53 | - '_wpnonce' => wp_create_nonce( 'winp_export_snippet_' . $post_id ), | |
| 54 | - ], $url ); | |
| 59 | + return add_query_arg( | |
| 60 | + [ | |
| 61 | + 'action' => 'export', | |
| 62 | + '_wpnonce' => wp_create_nonce( 'WINP_Actions_Snippet_' . $post_id ), | |
| 63 | + ], | |
| 64 | + $url | |
| 65 | + ); | |
| 55 | 66 | } |
| 56 | 67 | |
| 57 | 68 | /** |
| 58 | 69 | * Get close url |
| 59 | 70 | * |
| 60 | - * @param int $post_id | |
| 71 | + * @param int $post_id Post ID. | |
| 61 | 72 | * |
| 62 | 73 | * @return string |
| 63 | 74 | */ |
| 64 | 75 | private function get_close_url( $post_id ) { |
| @@ -63,25 +74,48 @@ | ||
| 63 | 74 | */ |
| 64 | 75 | private function get_close_url( $post_id ) { |
| 65 | 76 | $url = admin_url( 'post.php?post=' . $post_id ); |
| 66 | 77 | |
| 67 | - return add_query_arg( [ | |
| 68 | - 'action' => 'close', | |
| 69 | - '_wpnonce' => wp_create_nonce( 'winp_close_snippet_' . $post_id ), | |
| 70 | - ], $url ); | |
| 78 | + return add_query_arg( | |
| 79 | + [ | |
| 80 | + 'action' => 'close', | |
| 81 | + '_wpnonce' => wp_create_nonce( 'winp_close_snippet_' . $post_id ), | |
| 82 | + ], | |
| 83 | + $url | |
| 84 | + ); | |
| 71 | 85 | } |
| 72 | 86 | |
| 73 | 87 | /** |
| 74 | - * postRowActions | |
| 88 | + * Get clone url | |
| 75 | 89 | * |
| 76 | - * @param $actions | |
| 77 | - * @param $post | |
| 90 | + * @param int $post_id Post ID. | |
| 78 | 91 | * |
| 92 | + * @return string | |
| 93 | + */ | |
| 94 | + private function get_clone_url( $post_id ) { | |
| 95 | + $url = admin_url( 'post.php?post=' . $post_id ); | |
| 96 | + | |
| 97 | + return add_query_arg( | |
| 98 | + [ | |
| 99 | + 'action' => 'clone', | |
| 100 | + '_wpnonce' => wp_create_nonce( 'winp_clone_snippet_' . $post_id ), | |
| 101 | + ], | |
| 102 | + $url | |
| 103 | + ); | |
| 104 | + } | |
| 105 | + | |
| 106 | + /** | |
| 107 | + * Post row actions | |
| 108 | + * | |
| 109 | + * @param array<string, string> $actions Array of row actions. | |
| 110 | + * @param WP_Post $post WP_Post object. | |
| 111 | + * | |
| 79 | 112 | * @return mixed |
| 80 | 113 | */ |
| 81 | - public function postRowActions( $actions, $post ) { | |
| 82 | - if ( $post->post_type == WINP_SNIPPETS_POST_TYPE ) { | |
| 114 | + public function post_row_actions( $actions, $post ) { | |
| 115 | + if ( WINP_SNIPPETS_POST_TYPE === $post->post_type ) { | |
| 83 | 116 | $export_link = $this->get_export_url( $post->ID ); |
| 117 | + $clone_link = $this->get_clone_url( $post->ID ); | |
| 84 | 118 | |
| 85 | 119 | if ( isset( $actions['trash'] ) ) { |
| 86 | 120 | $trash = $actions['trash']; |
| 87 | 121 | unset( $actions['trash'] ); |
| @@ -87,8 +121,9 @@ | ||
| 87 | 121 | unset( $actions['trash'] ); |
| 88 | 122 | } |
| 89 | 123 | |
| 90 | 124 | $actions['export'] = sprintf( '<a href="%1$s">%2$s</a>', esc_url( $export_link ), esc_html( __( 'Export', 'insert-php' ) ) ); |
| 125 | + $actions['clone'] = sprintf( '<a href="%1$s">%2$s</a>', esc_url( $clone_link ), esc_html( __( 'Clone', 'insert-php' ) ) ); | |
| 91 | 126 | |
| 92 | 127 | if ( isset( $trash ) ) { |
| 93 | 128 | $actions['trash'] = $trash; |
| 94 | 129 | } |
| @@ -97,36 +132,40 @@ | ||
| 97 | 132 | return $actions; |
| 98 | 133 | } |
| 99 | 134 | |
| 100 | 135 | /** |
| 101 | - * actionBulkEditPost | |
| 136 | + * Action bulk_edit_post | |
| 102 | 137 | * |
| 103 | - * @param $bulk_actions | |
| 138 | + * @param array<string, string> $bulk_actions Array of bulk actions. | |
| 104 | 139 | * |
| 105 | 140 | * @return mixed |
| 106 | 141 | */ |
| 107 | - public function actionBulkEditPost( $bulk_actions ) { | |
| 108 | - $pro = WINP_Plugin::app()->get_api_object()->is_key() ? '' : ' (PRO)'; | |
| 142 | + public function action_bulk_edit_post( $bulk_actions ) { | |
| 143 | + $bulk_actions['activate'] = __( 'Activate', 'insert-php' ); | |
| 144 | + $bulk_actions['deactivate'] = __( 'Deactivate', 'insert-php' ); | |
| 145 | + $bulk_actions['exportsnp'] = __( 'Export', 'insert-php' ); | |
| 109 | 146 | |
| 110 | - $bulk_actions['exportsnp'] = __( 'Export', 'insert-php' ) . $pro; | |
| 111 | - $bulk_actions['deletesnp'] = __( 'Delete', 'insert-php' ); | |
| 112 | - $bulk_actions['deactivate'] = __( 'Deactivate', 'insert-php' ); | |
| 113 | - $bulk_actions['activate'] = __( 'Activate', 'insert-php' ); | |
| 147 | + // Don't show delete action for posts already in trash. | |
| 148 | + $current_screen = get_current_screen(); | |
| 149 | + if ( ! $current_screen || ( isset( $_GET['post_status'] ) && 'trash' !== $_GET['post_status'] ) || ( ! isset( $_GET['post_status'] ) ) ) { | |
| 150 | + $bulk_actions['deletesnp'] = __( 'Delete', 'insert-php' ); | |
| 151 | + $bulk_actions['clonenp'] = __( 'Clone', 'insert-php' ); | |
| 152 | + } | |
| 114 | 153 | |
| 115 | 154 | return $bulk_actions; |
| 116 | 155 | } |
| 117 | 156 | |
| 118 | 157 | /** |
| 119 | - * handleActionBulkEditPost | |
| 158 | + * Handle action bulk edit post | |
| 120 | 159 | * |
| 121 | - * @param $redirect_to | |
| 122 | - * @param $doaction | |
| 123 | - * @param $post_ids | |
| 160 | + * @param string $redirect_to URL to redirect to. | |
| 161 | + * @param string $doaction Do action. | |
| 162 | + * @param array<int> $post_ids Array of post IDs. | |
| 124 | 163 | * |
| 125 | 164 | * @return mixed |
| 126 | 165 | */ |
| 127 | - public function handleActionBulkEditPost( $redirect_to, $doaction, $post_ids ) { | |
| 128 | - if ( ! WINP_Plugin::app()->currentUserCan() ) { | |
| 166 | + public function handle_action_bulk_edit_post( $redirect_to, $doaction, $post_ids ) { | |
| 167 | + if ( ! WINP_Plugin::app()->current_user_car() ) { | |
| 129 | 168 | return false; |
| 130 | 169 | } |
| 131 | 170 | |
| 132 | 171 | check_admin_referer( 'bulk-posts' ); |
| @@ -134,9 +173,10 @@ | ||
| 134 | 173 | $actions = [ |
| 135 | 174 | 'exportsnp' => 1, |
| 136 | 175 | 'deletesnp' => 1, |
| 137 | 176 | 'deactivate' => 1, |
| 138 | - 'activate' => 1 | |
| 177 | + 'activate' => 1, | |
| 178 | + 'clonenp' => 1, | |
| 139 | 179 | ]; |
| 140 | 180 | |
| 141 | 181 | if ( ! isset( $actions[ $doaction ] ) ) { |
| 142 | 182 | return $redirect_to; |
| @@ -144,20 +184,25 @@ | ||
| 144 | 184 | |
| 145 | 185 | if ( count( $post_ids ) ) { |
| 146 | 186 | switch ( $doaction ) { |
| 147 | 187 | case 'exportsnp': |
| 148 | - if ( WINP_Plugin::app()->get_api_object()->is_key() ) { | |
| 149 | - $this->exportSnippets( $post_ids ); | |
| 150 | - } | |
| 188 | + $this->quick_export_snippets( $post_ids ); | |
| 151 | 189 | break; |
| 190 | + | |
| 191 | + case 'clonenp': | |
| 192 | + $this->clone_snippets( $post_ids ); | |
| 193 | + break; | |
| 194 | + | |
| 152 | 195 | case 'deletesnp': |
| 153 | - $this->deleteSnippets( $post_ids ); | |
| 196 | + $this->delete_snippets( $post_ids ); | |
| 154 | 197 | break; |
| 198 | + | |
| 155 | 199 | case 'deactivate': |
| 156 | - $this->deactivateSnippets( $post_ids ); | |
| 200 | + $this->deactivate_snippets( $post_ids ); | |
| 157 | 201 | break; |
| 202 | + | |
| 158 | 203 | case 'activate': |
| 159 | - $this->activateSnippets( $post_ids ); | |
| 204 | + $this->activate_snippets( $post_ids ); | |
| 160 | 205 | break; |
| 161 | 206 | } |
| 162 | 207 | } |
| 163 | 208 | |
| @@ -164,56 +209,78 @@ | ||
| 164 | 209 | return $redirect_to; |
| 165 | 210 | } |
| 166 | 211 | |
| 167 | 212 | /** |
| 168 | - * Выводим кнопки в форме редактирования сниппета, возле кнопки публикации / обновления | |
| 213 | + * Post submitbox start | |
| 214 | + * | |
| 215 | + * @return void | |
| 169 | 216 | */ |
| 170 | - public function postSubmitboxStart() { | |
| 217 | + public function post_submitbox_start() { | |
| 171 | 218 | global $post; |
| 172 | 219 | |
| 173 | 220 | if ( $post && WINP_SNIPPETS_POST_TYPE == $post->post_type ) { |
| 174 | 221 | if ( WINP_Helper::getMetaOption( $post->ID, 'snippet_draft', false ) ) { |
| 175 | 222 | $close_link = $this->get_close_url( $post->ID ); |
| 176 | - echo "<div id='winp-close-action'>" . sprintf( '<a href="%1$s" class="button button-large">%2$s</a>', esc_url( $close_link ), esc_html( __( 'Close', 'insert-php' ) ) ) . "</div>"; | |
| 223 | + echo "<div id='winp-close-action'>" . sprintf( '<a href="%1$s" class="button button-large">%2$s</a>', esc_url( $close_link ), esc_html( __( 'Close', 'insert-php' ) ) ) . '</div>'; | |
| 177 | 224 | } else { |
| 178 | 225 | $export_link = $this->get_export_url( $post->ID ); |
| 179 | - echo "<div id='winp-export-action'>" . sprintf( '<a href="%1$s">%2$s</a>', esc_url( $export_link ), esc_html( __( 'Export', 'insert-php' ) ) ) . "</div>"; | |
| 226 | + echo "<div id='winp-export-action'>" . sprintf( '<a href="%1$s">%2$s</a>', esc_url( $export_link ), esc_html( __( 'Export', 'insert-php' ) ) ) . '</div>'; | |
| 180 | 227 | } |
| 181 | 228 | } |
| 182 | 229 | } |
| 183 | 230 | |
| 184 | 231 | /** |
| 185 | - * Set up the current page to act like a downloadable file instead of being shown in the browser | |
| 232 | + * Prepare data | |
| 186 | 233 | * |
| 187 | - * @param string $format | |
| 188 | - * @param array $ids | |
| 189 | - * @param string $mime_type | |
| 234 | + * @param array<int> $ids Snippet IDs. | |
| 190 | 235 | * |
| 191 | - * @return array | |
| 236 | + * @return array<mixed> | |
| 192 | 237 | */ |
| 193 | - public function prepareExport( $format, $ids, $mime_type = '' ) { | |
| 238 | + private function prepare_data( $ids ) { | |
| 194 | 239 | $snippets = []; |
| 195 | 240 | |
| 196 | 241 | if ( count( $ids ) ) { |
| 197 | 242 | foreach ( $ids as $id ) { |
| 198 | - $post = get_post( $id ); | |
| 199 | - $snippets[] = [ | |
| 243 | + $post = get_post( $id ); | |
| 244 | + | |
| 245 | + if ( ! $post ) { | |
| 246 | + continue; | |
| 247 | + } | |
| 248 | + | |
| 249 | + $snippet = [ | |
| 200 | 250 | 'name' => $post->post_name, |
| 201 | 251 | 'title' => $post->post_title, |
| 202 | 252 | 'content' => $post->post_content, |
| 203 | - 'location' => $this->getMeta( $id, 'snippet_location' ), | |
| 204 | - 'type' => $this->getMeta( $id, 'snippet_type' ), | |
| 205 | - 'filters' => $this->getMeta( $id, 'snippet_filters' ), | |
| 206 | - 'changed_filters' => $this->getMeta( $id, 'changed_filters' ), | |
| 207 | - 'scope' => $this->getMeta( $id, 'snippet_scope' ), | |
| 208 | - 'priority' => $this->getMeta( $id, 'snippet_priority' ), | |
| 209 | - 'description' => $this->getMeta( $id, 'snippet_description' ), | |
| 210 | - 'attributes' => $this->getMeta( $id, 'snippet_tags' ), | |
| 211 | - 'tags' => $this->getTaxonomyTags( $id ) | |
| 253 | + 'location' => $this->get_meta( $id, 'snippet_location' ), | |
| 254 | + 'type' => $this->get_meta( $id, 'snippet_type' ), | |
| 255 | + 'filters' => $this->get_meta( $id, 'snippet_filters' ), | |
| 256 | + 'changed_filters' => $this->get_meta( $id, 'changed_filters' ), | |
| 257 | + 'scope' => $this->get_meta( $id, 'snippet_scope' ), | |
| 258 | + 'priority' => $this->get_meta( $id, 'snippet_priority' ), | |
| 259 | + 'description' => $this->get_meta( $id, 'snippet_description' ), | |
| 260 | + 'attributes' => $this->get_meta( $id, 'snippet_tags' ), | |
| 261 | + 'tags' => $this->get_taxonomy_tags( $id ), | |
| 212 | 262 | ]; |
| 263 | + | |
| 264 | + $snippets[] = apply_filters( 'wbcr/inp/snippet/prepare_data', $snippet, $id, $this ); | |
| 213 | 265 | } |
| 214 | 266 | } |
| 215 | 267 | |
| 268 | + return $snippets; | |
| 269 | + } | |
| 270 | + | |
| 271 | + /** | |
| 272 | + * Get file name | |
| 273 | + * | |
| 274 | + * @param string $format File format. | |
| 275 | + * @param array<int> $ids Snippet IDs. | |
| 276 | + * @param array<mixed> $snippets Snippets data. | |
| 277 | + * | |
| 278 | + * @return string | |
| 279 | + */ | |
| 280 | + public function get_filename( $format, $ids, $snippets ) { | |
| 281 | + $snippets = empty( $snippets ) ? $this->prepare_data( $ids ) : $snippets; | |
| 282 | + | |
| 216 | 283 | /* Build the export filename */ |
| 217 | 284 | if ( 1 == count( $ids ) ) { |
| 218 | 285 | $name = $snippets[0]['title']; |
| 219 | 286 | $title = strtolower( $name ); |
| @@ -223,60 +290,185 @@ | ||
| 223 | 290 | } |
| 224 | 291 | |
| 225 | 292 | $filename = "{$title}.php-code-snippets.{$format}"; |
| 226 | 293 | |
| 227 | - /* Set HTTP headers */ | |
| 228 | - header( 'Content-Disposition: attachment; filename=' . sanitize_file_name( $filename ) ); | |
| 294 | + return $filename; | |
| 295 | + } | |
| 229 | 296 | |
| 230 | - if ( '' !== $mime_type ) { | |
| 231 | - header( "Content-Type: $mime_type; charset=" . get_bloginfo( 'charset' ) ); | |
| 297 | + /** | |
| 298 | + * Export snippets - prepare and return export data | |
| 299 | + * | |
| 300 | + * @param array<int> $ids Snippet IDs to export. | |
| 301 | + * @param bool $zip Whether to create a zip file. | |
| 302 | + * @return array<mixed> Export data with filename, data, and count. | |
| 303 | + */ | |
| 304 | + public function export_snippets( $ids, $zip = false ) { | |
| 305 | + $snippets = $this->prepare_data( $ids ); | |
| 306 | + $filename = $this->get_filename( 'json', $ids, $snippets ); | |
| 307 | + | |
| 308 | + $data = [ | |
| 309 | + 'generator' => 'PHP Code Snippets v' . WINP_PLUGIN_VERSION, | |
| 310 | + 'date_created' => gmdate( 'Y-m-d H:i' ), | |
| 311 | + 'snippets' => $snippets, | |
| 312 | + ]; | |
| 313 | + | |
| 314 | + if ( $zip ) { | |
| 315 | + $zipname = str_replace( '.json', '.zip', $filename ); | |
| 316 | + $upload_dir = wp_upload_dir(); | |
| 317 | + $zippath = $upload_dir['path'] . '/' . $zipname; | |
| 318 | + | |
| 319 | + $zip_archive = new ZipArchive(); | |
| 320 | + if ( true === $zip_archive->open( $zippath, ZipArchive::CREATE | ZipArchive::OVERWRITE ) ) { | |
| 321 | + // Add individual JSON file for each snippet. | |
| 322 | + foreach ( $snippets as $snippet ) { | |
| 323 | + $snippet_data = [ | |
| 324 | + 'generator' => 'PHP Code Snippets v' . WINP_PLUGIN_VERSION, | |
| 325 | + 'date_created' => gmdate( 'Y-m-d H:i' ), | |
| 326 | + 'snippets' => [ $snippet ], | |
| 327 | + ]; | |
| 328 | + $json_content = wp_json_encode( $snippet_data, JSON_PRETTY_PRINT ); | |
| 329 | + | |
| 330 | + if ( false === $json_content ) { | |
| 331 | + continue; | |
| 332 | + } | |
| 333 | + | |
| 334 | + $snippet_filename = sanitize_file_name( strtolower( $snippet['title'] ) ) . '.php-code-snippets.json'; | |
| 335 | + $zip_archive->addFromString( $snippet_filename, $json_content ); | |
| 336 | + } | |
| 337 | + | |
| 338 | + $zip_archive->close(); | |
| 339 | + | |
| 340 | + $wp_filesystem = WINP_Helper::get_wp_filesystem(); | |
| 341 | + | |
| 342 | + if ( false !== $wp_filesystem ) { | |
| 343 | + $zip_content = $wp_filesystem->get_contents( $zippath ); | |
| 344 | + $wp_filesystem->delete( $zippath, false, 'f' ); | |
| 345 | + | |
| 346 | + if ( false !== $zip_content && '' !== $zip_content ) { | |
| 347 | + return [ | |
| 348 | + 'filename' => $zipname, | |
| 349 | + 'data' => $zip_content, | |
| 350 | + 'count' => count( $snippets ), | |
| 351 | + 'is_zip' => true, | |
| 352 | + ]; | |
| 353 | + } | |
| 354 | + } | |
| 355 | + | |
| 356 | + wp_delete_file( $zippath ); | |
| 357 | + } | |
| 232 | 358 | } |
| 233 | 359 | |
| 234 | - return $snippets; | |
| 360 | + return [ | |
| 361 | + 'filename' => $filename, | |
| 362 | + 'data' => $data, | |
| 363 | + 'count' => count( $snippets ), | |
| 364 | + 'is_zip' => false, | |
| 365 | + ]; | |
| 235 | 366 | } |
| 236 | 367 | |
| 237 | 368 | /** |
| 238 | 369 | * Export snippets in JSON format |
| 239 | 370 | * |
| 240 | - * @param array $ids | |
| 371 | + * @param array<int> $ids Snippet IDs. | |
| 372 | + * | |
| 373 | + * @return void | |
| 241 | 374 | */ |
| 242 | - public function exportSnippets( $ids ) { | |
| 243 | - $snippets = $this->prepareExport( 'json', $ids, 'application/json' ); | |
| 375 | + public function quick_export_snippets( $ids ) { | |
| 376 | + $snippets = $this->export_snippets( $ids, count( $ids ) > 1 ); | |
| 244 | 377 | |
| 245 | - $data = [ | |
| 246 | - 'generator' => 'PHP Code Snippets v' . WINP_PLUGIN_VERSION, | |
| 247 | - 'date_created' => date( 'Y-m-d H:i' ), | |
| 248 | - 'snippets' => $snippets, | |
| 249 | - ]; | |
| 378 | + $filename = $snippets['filename']; | |
| 379 | + $data = $snippets['data']; | |
| 380 | + $is_zip = $snippets['is_zip']; | |
| 381 | + $mime_type = $is_zip ? 'application/zip' : 'application/json'; | |
| 250 | 382 | |
| 251 | - echo wp_json_encode( $data, 0 ); | |
| 383 | + /* Set HTTP headers */ | |
| 384 | + header( 'Content-Disposition: attachment; filename=' . sanitize_file_name( $filename ) ); | |
| 385 | + header( "Content-Type: $mime_type; charset=" . get_bloginfo( 'charset' ) ); | |
| 386 | + | |
| 387 | + // For zip files, output raw binary data. For JSON, encode the data. | |
| 388 | + if ( $is_zip ) { | |
| 389 | + $output = is_string( $data ) ? $data : (string) $data; | |
| 390 | + $wp_filesystem = WINP_Helper::get_wp_filesystem(); | |
| 391 | + | |
| 392 | + if ( false !== $wp_filesystem ) { | |
| 393 | + $wp_filesystem->put_contents( 'php://output', $output, false ); | |
| 394 | + } | |
| 395 | + exit; | |
| 396 | + } else { | |
| 397 | + echo wp_json_encode( $data, JSON_PRETTY_PRINT ); | |
| 398 | + } | |
| 252 | 399 | exit; |
| 253 | 400 | } |
| 254 | 401 | |
| 255 | 402 | /** |
| 403 | + * Clone snippets | |
| 404 | + * | |
| 405 | + * @param array<int> $ids Snippet IDs. | |
| 406 | + * | |
| 407 | + * @return void | |
| 408 | + */ | |
| 409 | + public function clone_snippets( $ids ) { | |
| 410 | + $snippets = $this->prepare_data( $ids ); | |
| 411 | + | |
| 412 | + if ( $snippets ) { | |
| 413 | + foreach ( $snippets as $snippet ) { | |
| 414 | + $data = [ | |
| 415 | + 'post_title' => $snippet['title'] . ' copy', | |
| 416 | + 'post_content' => $snippet['content'], | |
| 417 | + 'post_status' => 'draft', | |
| 418 | + 'post_type' => WINP_SNIPPETS_POST_TYPE, | |
| 419 | + ]; | |
| 420 | + | |
| 421 | + $snippet['id'] = wp_insert_post( $data ); | |
| 422 | + | |
| 423 | + update_post_meta( $snippet['id'], 'wbcr_inp_snippet_location', $snippet['location'] ); | |
| 424 | + update_post_meta( $snippet['id'], 'wbcr_inp_snippet_type', $snippet['type'] ); | |
| 425 | + update_post_meta( $snippet['id'], 'wbcr_inp_snippet_filters', $snippet['filters'] ); | |
| 426 | + update_post_meta( $snippet['id'], 'wbcr_inp_changed_filters', $snippet['changed_filters'] ); | |
| 427 | + update_post_meta( $snippet['id'], 'wbcr_inp_snippet_scope', $snippet['scope'] ); | |
| 428 | + update_post_meta( $snippet['id'], 'wbcr_inp_snippet_priority', $snippet['priority'] ); | |
| 429 | + update_post_meta( $snippet['id'], 'wbcr_inp_snippet_description', $snippet['description'] ); | |
| 430 | + update_post_meta( $snippet['id'], 'wbcr_inp_snippet_tags', $snippet['attributes'] ); | |
| 431 | + update_post_meta( $snippet['id'], 'wbcr_inp_snippet_activate', 0 ); | |
| 432 | + $this->update_taxonomy_tags( $snippet['id'], $snippet['tags'] ); | |
| 433 | + | |
| 434 | + do_action( 'wbcr/inp/snippet/clone_meta', $snippet['id'], $snippet, $this ); | |
| 435 | + } | |
| 436 | + } | |
| 437 | + } | |
| 438 | + | |
| 439 | + /** | |
| 256 | 440 | * Action admin_init |
| 441 | + * | |
| 442 | + * @return void | |
| 257 | 443 | */ |
| 258 | - public function adminInit() { | |
| 259 | - if ( ! WINP_Plugin::app()->currentUserCan() ) { | |
| 444 | + public function admin_init() { | |
| 445 | + if ( ! WINP_Plugin::app()->current_user_car() ) { | |
| 260 | 446 | return; |
| 261 | 447 | } |
| 262 | 448 | |
| 263 | - $post_id = WINP_Plugin::app()->request->get( 'post', 0, 'intval' ); | |
| 264 | - $action = WINP_Plugin::app()->request->get( 'action' ); | |
| 449 | + $post_id = WINP_HTTP::get( 'post', 0, 'intval' ); | |
| 450 | + $action = WINP_HTTP::get( 'action' ); | |
| 265 | 451 | |
| 266 | 452 | if ( ! empty( $action ) && ! empty( $post_id ) ) { |
| 267 | 453 | switch ( $action ) { |
| 268 | 454 | case 'export': |
| 269 | - check_admin_referer( 'winp_export_snippet_' . $post_id ); | |
| 270 | - $this->exportSnippets( [ $post_id ] ); | |
| 455 | + check_admin_referer( 'WINP_Actions_Snippet_' . $post_id ); | |
| 456 | + $this->quick_export_snippets( [ $post_id ] ); | |
| 457 | + break; | |
| 271 | 458 | |
| 272 | - break; | |
| 459 | + case 'clone': | |
| 460 | + check_admin_referer( 'winp_clone_snippet_' . $post_id ); | |
| 461 | + $this->clone_snippets( [ $post_id ] ); | |
| 462 | + wp_safe_redirect( admin_url( 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE ) ); | |
| 463 | + exit(); | |
| 464 | + | |
| 273 | 465 | case 'close': |
| 274 | 466 | check_admin_referer( 'winp_close_snippet_' . $post_id ); |
| 275 | 467 | wp_delete_post( $post_id ); |
| 276 | - wp_redirect( admin_url( 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE . '&page=snippet-library-wbcr_insert_php' ) ); | |
| 468 | + wp_safe_redirect( admin_url( 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE . '&page=snippet-library' ) ); | |
| 277 | 469 | exit(); |
| 278 | - break; | |
| 470 | + | |
| 279 | 471 | default: |
| 280 | 472 | return; |
| 281 | 473 | } |
| 282 | 474 | } |
| @@ -284,19 +476,27 @@ | ||
| 284 | 476 | |
| 285 | 477 | /** |
| 286 | 478 | * Action current_screen |
| 287 | 479 | * Add script for disabled export button |
| 480 | + * | |
| 481 | + * @return void | |
| 288 | 482 | */ |
| 289 | 483 | public function current_screen() { |
| 290 | 484 | $current_screen = get_current_screen(); |
| 291 | 485 | |
| 486 | + if ( null === $current_screen ) { | |
| 487 | + return; | |
| 488 | + } | |
| 489 | + | |
| 292 | 490 | if ( 'edit-wbcr-snippets' === $current_screen->id && WINP_SNIPPETS_POST_TYPE === $current_screen->post_type && ! WINP_Plugin::app()->get_api_object()->is_key() ) { |
| 293 | - //wp_enqueue_script( 'winp-snippet-list', WINP_PLUGIN_URL . '/admin/assets/js/snippet-list.js' ); | |
| 491 | + do_action( 'themeisle_internal_page', WINP_PLUGIN_SLUG, 'wbcr-snippets' ); | |
| 294 | 492 | } |
| 295 | 493 | } |
| 296 | 494 | |
| 297 | 495 | /** |
| 298 | 496 | * Add style for close button for auto-draft snippet (preview) |
| 497 | + * | |
| 498 | + * @return void | |
| 299 | 499 | */ |
| 300 | 500 | public function admin_enqueue_scripts() { |
| 301 | 501 | global $post; |
| 302 | 502 | |
| @@ -301,12 +501,16 @@ | ||
| 301 | 501 | global $post; |
| 302 | 502 | |
| 303 | 503 | $current_screen = get_current_screen(); |
| 304 | 504 | |
| 505 | + if ( null === $current_screen ) { | |
| 506 | + return; | |
| 507 | + } | |
| 508 | + | |
| 305 | 509 | if ( 'wbcr-snippets' === $current_screen->id && WINP_SNIPPETS_POST_TYPE === $post->post_type && WINP_Helper::getMetaOption( get_the_ID(), 'snippet_draft', false ) ) { |
| 306 | - wp_enqueue_style( 'winp-snippet-preview', WINP_PLUGIN_URL . '/admin/assets/css/snippet-preview.css' ); | |
| 510 | + wp_enqueue_style( 'winp-snippet-preview', WINP_PLUGIN_URL . '/admin/assets/css/snippet-preview.css', [], WINP_PLUGIN_VERSION ); | |
| 307 | 511 | |
| 308 | - // Remove Clear cache button for WP-Rocket plugin in preview snippet page | |
| 512 | + // Remove Clear cache button for WP-Rocket plugin in preview snippet page. | |
| 309 | 513 | remove_action( 'post_submitbox_start', 'rocket_post_submitbox_start' ); |
| 310 | 514 | } |
| 311 | 515 | } |
| 312 | 516 | |
| @@ -312,11 +516,13 @@ | ||
| 312 | 516 | |
| 313 | 517 | /** |
| 314 | 518 | * Delete snippets |
| 315 | 519 | * |
| 316 | - * @param $ids | |
| 520 | + * @param array<int> $ids Snippet IDs. | |
| 521 | + * | |
| 522 | + * @return void | |
| 317 | 523 | */ |
| 318 | - private function deleteSnippets( $ids ) { | |
| 524 | + private function delete_snippets( $ids ) { | |
| 319 | 525 | if ( count( $ids ) ) { |
| 320 | 526 | foreach ( $ids as $id ) { |
| 321 | 527 | wp_trash_post( $id ); |
| 322 | 528 | } |
| @@ -325,14 +531,16 @@ | ||
| 325 | 531 | |
| 326 | 532 | /** |
| 327 | 533 | * Deactivate snippets |
| 328 | 534 | * |
| 329 | - * @param $ids | |
| 535 | + * @param array<int> $ids Snippet IDs. | |
| 536 | + * | |
| 537 | + * @return void | |
| 330 | 538 | */ |
| 331 | - private function deactivateSnippets( $ids ) { | |
| 539 | + private function deactivate_snippets( $ids ) { | |
| 332 | 540 | if ( count( $ids ) ) { |
| 333 | 541 | foreach ( $ids as $id ) { |
| 334 | - update_post_meta( $id, WINP_Plugin::app()->getPrefix() . 'snippet_activate', 0 ); | |
| 542 | + update_post_meta( $id, 'wbcr_inp_snippet_activate', 0 ); | |
| 335 | 543 | } |
| 336 | 544 | } |
| 337 | 545 | } |
| 338 | 546 | |
| @@ -338,11 +546,13 @@ | ||
| 338 | 546 | |
| 339 | 547 | /** |
| 340 | 548 | * Activate snippets |
| 341 | 549 | * |
| 342 | - * @param $ids | |
| 550 | + * @param array<int> $ids Snippet IDs. | |
| 551 | + * | |
| 552 | + * @return void | |
| 343 | 553 | */ |
| 344 | - private function activateSnippets( $ids ) { | |
| 554 | + private function activate_snippets( $ids ) { | |
| 345 | 555 | if ( count( $ids ) ) { |
| 346 | 556 | foreach ( $ids as $id ) { |
| 347 | 557 | $is_activate = (int) WINP_Helper::getMetaOption( $id, 'snippet_activate', 0 ); |
| 348 | 558 | $snippet_scope = WINP_Helper::getMetaOption( $id, 'snippet_scope' ); |
| @@ -347,13 +557,13 @@ | ||
| 347 | 557 | $is_activate = (int) WINP_Helper::getMetaOption( $id, 'snippet_activate', 0 ); |
| 348 | 558 | $snippet_scope = WINP_Helper::getMetaOption( $id, 'snippet_scope' ); |
| 349 | 559 | $snippet_type = WINP_Helper::get_snippet_type( $id ); |
| 350 | 560 | |
| 351 | - if ( ( $snippet_scope == 'evrywhere' || $snippet_scope == 'auto' ) && ! $is_activate && $snippet_type != WINP_SNIPPET_TYPE_TEXT && $snippet_type != WINP_SNIPPET_TYPE_AD && WINP_Plugin::app()->getExecuteObject()->getSnippetError( $id ) ) { | |
| 561 | + if ( ( 'evrywhere' === $snippet_scope || 'auto' === $snippet_scope ) && ! $is_activate && WINP_SNIPPET_TYPE_TEXT !== $snippet_type && WINP_SNIPPET_TYPE_AD !== $snippet_type && WINP_Plugin::app()->get_execute_object()->getSnippetError( $id ) ) { | |
| 352 | 562 | continue; |
| 353 | 563 | } |
| 354 | 564 | |
| 355 | - update_post_meta( $id, WINP_Plugin::app()->getPrefix() . 'snippet_activate', 1 ); | |
| 565 | + update_post_meta( $id, 'wbcr_inp_snippet_activate', 1 ); | |
| 356 | 566 | } |
| 357 | 567 | } |
| 358 | 568 | } |
| 359 | 569 | |
| @@ -360,17 +570,20 @@ | ||
| 360 | 570 | |
| 361 | 571 | /** |
| 362 | 572 | * Get taxonomy tags |
| 363 | 573 | * |
| 364 | - * @param $snippet_id | |
| 574 | + * @param int $snippet_id Snippet ID. | |
| 365 | 575 | * |
| 366 | - * @return array | |
| 576 | + * @return array<string> | |
| 367 | 577 | */ |
| 368 | - private function getTaxonomyTags( $snippet_id ) { | |
| 578 | + private function get_taxonomy_tags( $snippet_id ) { | |
| 369 | 579 | $tags = []; |
| 370 | 580 | |
| 371 | 581 | if ( $snippet_id ) { |
| 372 | - return wp_get_post_terms( $snippet_id, WINP_SNIPPETS_TAXONOMY, [ "fields" => "slugs" ] ); | |
| 582 | + $terms = wp_get_post_terms( $snippet_id, WINP_SNIPPETS_TAXONOMY, [ 'fields' => 'slugs' ] ); | |
| 583 | + if ( ! is_wp_error( $terms ) ) { | |
| 584 | + return $terms; | |
| 585 | + } | |
| 373 | 586 | } |
| 374 | 587 | |
| 375 | 588 | return $tags; |
| 376 | 589 | } |
| @@ -375,16 +588,34 @@ | ||
| 375 | 588 | return $tags; |
| 376 | 589 | } |
| 377 | 590 | |
| 378 | 591 | /** |
| 592 | + * Update taxonomy tags | |
| 593 | + * | |
| 594 | + * @param int $snippet_id Snippet ID. | |
| 595 | + * @param array<string> $tags Tags array. | |
| 596 | + * | |
| 597 | + * @return void | |
| 598 | + */ | |
| 599 | + private function update_taxonomy_tags( $snippet_id, $tags ) { | |
| 600 | + if ( ! empty( $tags ) ) { | |
| 601 | + foreach ( $tags as $tag_slug ) { | |
| 602 | + $term = get_term_by( 'slug', $tag_slug, WINP_SNIPPETS_TAXONOMY ); | |
| 603 | + if ( $term ) { | |
| 604 | + wp_set_post_terms( $snippet_id, [ $term->term_id ], WINP_SNIPPETS_TAXONOMY, true ); | |
| 605 | + } | |
| 606 | + } | |
| 607 | + } | |
| 608 | + } | |
| 609 | + | |
| 610 | + /** | |
| 379 | 611 | * Get post meta |
| 380 | 612 | * |
| 381 | - * @param $post_id | |
| 382 | - * @param $meta_name | |
| 613 | + * @param int $post_id Post ID. | |
| 614 | + * @param string $meta_name Meta name. | |
| 383 | 615 | * |
| 384 | 616 | * @return mixed |
| 385 | 617 | */ |
| 386 | - private function getMeta( $post_id, $meta_name ) { | |
| 387 | - return get_post_meta( $post_id, WINP_Plugin::app()->getPrefix() . $meta_name, true ); | |
| 618 | + private function get_meta( $post_id, $meta_name ) { | |
| 619 | + return get_post_meta( $post_id, 'wbcr_inp_' . $meta_name, true ); | |
| 388 | 620 | } |
| 389 | - | |
| 390 | -} | |
| 621 | +} | |