| 1 |
<?php |
| 2 |
/** |
| 3 |
* Export snippet |
| 4 |
* |
| 5 |
* @author Webcraftic <wordpress.webraftic@gmail.com> |
| 6 |
* @copyright (c) 16.11.2018, Webcraftic |
| 7 |
* @version 1.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
// Exit if accessed directly |
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
class WINP_Export_Snippet { |
| 16 |
|
| 17 |
/** |
| 18 |
* WINP_Export_Snippet constructor. |
| 19 |
*/ |
| 20 |
public function __construct() { |
| 21 |
$this->registerHooks(); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Register hooks |
| 26 |
*/ |
| 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 ); |
| 34 |
|
| 35 |
add_action( 'post_submitbox_start', [ $this, 'postSubmitboxStart' ] ); |
| 36 |
add_action( 'admin_init', [ $this, 'adminInit' ] ); |
| 37 |
add_action( 'current_screen', [ $this, 'current_screen' ] ); |
| 38 |
add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Get export url |
| 43 |
* |
| 44 |
* @param int $post_id |
| 45 |
* |
| 46 |
* @return string |
| 47 |
*/ |
| 48 |
private function get_export_url( $post_id ) { |
| 49 |
$url = admin_url( 'post.php?post=' . $post_id ); |
| 50 |
|
| 51 |
return add_query_arg( [ |
| 52 |
'action' => 'export', |
| 53 |
'_wpnonce' => wp_create_nonce( 'winp_export_snippet_' . $post_id ), |
| 54 |
], $url ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Get close url |
| 59 |
* |
| 60 |
* @param int $post_id |
| 61 |
* |
| 62 |
* @return string |
| 63 |
*/ |
| 64 |
private function get_close_url( $post_id ) { |
| 65 |
$url = admin_url( 'post.php?post=' . $post_id ); |
| 66 |
|
| 67 |
return add_query_arg( [ |
| 68 |
'action' => 'close', |
| 69 |
'_wpnonce' => wp_create_nonce( 'winp_close_snippet_' . $post_id ), |
| 70 |
], $url ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* postRowActions |
| 75 |
* |
| 76 |
* @param $actions |
| 77 |
* @param $post |
| 78 |
* |
| 79 |
* @return mixed |
| 80 |
*/ |
| 81 |
public function postRowActions( $actions, $post ) { |
| 82 |
if ( $post->post_type == WINP_SNIPPETS_POST_TYPE ) { |
| 83 |
$export_link = $this->get_export_url( $post->ID ); |
| 84 |
|
| 85 |
if ( isset( $actions['trash'] ) ) { |
| 86 |
$trash = $actions['trash']; |
| 87 |
unset( $actions['trash'] ); |
| 88 |
} |
| 89 |
|
| 90 |
$actions['export'] = sprintf( '<a href="%1$s">%2$s</a>', esc_url( $export_link ), esc_html( __( 'Export', 'insert-php' ) ) ); |
| 91 |
|
| 92 |
if ( isset( $trash ) ) { |
| 93 |
$actions['trash'] = $trash; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
return $actions; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* actionBulkEditPost |
| 102 |
* |
| 103 |
* @param $bulk_actions |
| 104 |
* |
| 105 |
* @return mixed |
| 106 |
*/ |
| 107 |
public function actionBulkEditPost( $bulk_actions ) { |
| 108 |
$pro = WINP_Plugin::app()->get_api_object()->is_key() ? '' : ' (PRO)'; |
| 109 |
|
| 110 |
$bulk_actions['exportsnp'] = __( 'Export' . $pro, 'insert-php' ); |
| 111 |
$bulk_actions['deletesnp'] = __( 'Delete', 'insert-php' ); |
| 112 |
$bulk_actions['deactivate'] = __( 'Deactivate', 'insert-php' ); |
| 113 |
$bulk_actions['activate'] = __( 'Activate', 'insert-php' ); |
| 114 |
|
| 115 |
return $bulk_actions; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* handleActionBulkEditPost |
| 120 |
* |
| 121 |
* @param $redirect_to |
| 122 |
* @param $doaction |
| 123 |
* @param $post_ids |
| 124 |
* |
| 125 |
* @return mixed |
| 126 |
*/ |
| 127 |
public function handleActionBulkEditPost( $redirect_to, $doaction, $post_ids ) { |
| 128 |
if ( ! WINP_Plugin::app()->currentUserCan() ) { |
| 129 |
return false; |
| 130 |
} |
| 131 |
|
| 132 |
check_admin_referer( 'bulk-posts' ); |
| 133 |
|
| 134 |
$actions = [ |
| 135 |
'exportsnp' => 1, |
| 136 |
'deletesnp' => 1, |
| 137 |
'deactivate' => 1, |
| 138 |
'activate' => 1 |
| 139 |
]; |
| 140 |
|
| 141 |
if ( ! isset( $actions[ $doaction ] ) ) { |
| 142 |
return $redirect_to; |
| 143 |
} |
| 144 |
|
| 145 |
if ( count( $post_ids ) ) { |
| 146 |
switch ( $doaction ) { |
| 147 |
case 'exportsnp': |
| 148 |
if ( WINP_Plugin::app()->get_api_object()->is_key() ) { |
| 149 |
$this->exportSnippets( $post_ids ); |
| 150 |
} |
| 151 |
break; |
| 152 |
case 'deletesnp': |
| 153 |
$this->deleteSnippets( $post_ids ); |
| 154 |
break; |
| 155 |
case 'deactivate': |
| 156 |
$this->deactivateSnippets( $post_ids ); |
| 157 |
break; |
| 158 |
case 'activate': |
| 159 |
$this->activateSnippets( $post_ids ); |
| 160 |
break; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
return $redirect_to; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Выводим кнопки в форме редактирования сниппета, возле кнопки публикации / обновления |
| 169 |
*/ |
| 170 |
public function postSubmitboxStart() { |
| 171 |
global $post; |
| 172 |
|
| 173 |
if ( $post && WINP_SNIPPETS_POST_TYPE == $post->post_type ) { |
| 174 |
if ( WINP_Helper::getMetaOption( $post->ID, 'snippet_draft', false ) ) { |
| 175 |
$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>"; |
| 177 |
} else { |
| 178 |
$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>"; |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Set up the current page to act like a downloadable file instead of being shown in the browser |
| 186 |
* |
| 187 |
* @param string $format |
| 188 |
* @param array $ids |
| 189 |
* @param string $mime_type |
| 190 |
* |
| 191 |
* @return array |
| 192 |
*/ |
| 193 |
public function prepareExport( $format, $ids, $mime_type = '' ) { |
| 194 |
$snippets = []; |
| 195 |
|
| 196 |
if ( count( $ids ) ) { |
| 197 |
foreach ( $ids as $id ) { |
| 198 |
$post = get_post( $id ); |
| 199 |
$snippets[] = [ |
| 200 |
'name' => $post->post_name, |
| 201 |
'title' => $post->post_title, |
| 202 |
'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 |
'description' => $this->getMeta( $id, 'snippet_description' ), |
| 209 |
'attributes' => $this->getMeta( $id, 'snippet_tags' ), |
| 210 |
'tags' => $this->getTaxonomyTags( $id ) |
| 211 |
]; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
/* Build the export filename */ |
| 216 |
if ( 1 == count( $ids ) ) { |
| 217 |
$name = $snippets[0]['title']; |
| 218 |
$title = strtolower( $name ); |
| 219 |
} else { |
| 220 |
/* Otherwise, use the site name as set in Settings > General */ |
| 221 |
$title = strtolower( get_bloginfo( 'name' ) ); |
| 222 |
} |
| 223 |
|
| 224 |
$filename = "{$title}.php-code-snippets.{$format}"; |
| 225 |
|
| 226 |
/* Set HTTP headers */ |
| 227 |
header( 'Content-Disposition: attachment; filename=' . sanitize_file_name( $filename ) ); |
| 228 |
|
| 229 |
if ( '' !== $mime_type ) { |
| 230 |
header( "Content-Type: $mime_type; charset=" . get_bloginfo( 'charset' ) ); |
| 231 |
} |
| 232 |
|
| 233 |
return $snippets; |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Export snippets in JSON format |
| 238 |
* |
| 239 |
* @param array $ids |
| 240 |
*/ |
| 241 |
public function exportSnippets( $ids ) { |
| 242 |
$snippets = $this->prepareExport( 'json', $ids, 'application/json' ); |
| 243 |
|
| 244 |
$data = [ |
| 245 |
'generator' => 'PHP Code Snippets v' . WINP_PLUGIN_VERSION, |
| 246 |
'date_created' => date( 'Y-m-d H:i' ), |
| 247 |
'snippets' => $snippets, |
| 248 |
]; |
| 249 |
|
| 250 |
echo wp_json_encode( $data, 0 ); |
| 251 |
exit; |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Action admin_init |
| 256 |
*/ |
| 257 |
public function adminInit() { |
| 258 |
if ( ! WINP_Plugin::app()->currentUserCan() ) { |
| 259 |
return; |
| 260 |
} |
| 261 |
|
| 262 |
$post_id = WINP_Plugin::app()->request->get( 'post', 0, 'intval' ); |
| 263 |
$action = WINP_Plugin::app()->request->get( 'action' ); |
| 264 |
|
| 265 |
if ( ! empty( $action ) && ! empty( $post_id ) ) { |
| 266 |
switch ( $action ) { |
| 267 |
case 'export': |
| 268 |
check_admin_referer( 'winp_export_snippet_' . $post_id ); |
| 269 |
$this->exportSnippets( [ $post_id ] ); |
| 270 |
|
| 271 |
break; |
| 272 |
case 'close': |
| 273 |
check_admin_referer( 'winp_close_snippet_' . $post_id ); |
| 274 |
wp_delete_post( $post_id ); |
| 275 |
wp_redirect( admin_url( 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE . '&page=snippet-library-wbcr_insert_php' ) ); |
| 276 |
exit(); |
| 277 |
break; |
| 278 |
default: |
| 279 |
return; |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* Action current_screen |
| 286 |
* Add script for disabled export button |
| 287 |
*/ |
| 288 |
public function current_screen() { |
| 289 |
$current_screen = get_current_screen(); |
| 290 |
|
| 291 |
if ( 'edit-wbcr-snippets' === $current_screen->id && WINP_SNIPPETS_POST_TYPE === $current_screen->post_type && ! WINP_Plugin::app()->get_api_object()->is_key() ) { |
| 292 |
wp_enqueue_script( 'winp-snippet-list', WINP_PLUGIN_URL . '/admin/assets/js/snippet-list.js' ); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Add style for close button for auto-draft snippet (preview) |
| 298 |
*/ |
| 299 |
public function admin_enqueue_scripts() { |
| 300 |
global $post; |
| 301 |
|
| 302 |
$current_screen = get_current_screen(); |
| 303 |
|
| 304 |
if ( 'wbcr-snippets' === $current_screen->id && WINP_SNIPPETS_POST_TYPE === $post->post_type && WINP_Helper::getMetaOption( get_the_ID(), 'snippet_draft', false ) ) { |
| 305 |
wp_enqueue_style( 'winp-snippet-preview', WINP_PLUGIN_URL . '/admin/assets/css/snippet-preview.css' ); |
| 306 |
|
| 307 |
// Remove Clear cache button for WP-Rocket plugin in preview snippet page |
| 308 |
remove_action( 'post_submitbox_start', 'rocket_post_submitbox_start' ); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* Delete snippets |
| 314 |
* |
| 315 |
* @param $ids |
| 316 |
*/ |
| 317 |
private function deleteSnippets( $ids ) { |
| 318 |
if ( count( $ids ) ) { |
| 319 |
foreach ( $ids as $id ) { |
| 320 |
wp_trash_post( $id ); |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
/** |
| 326 |
* Deactivate snippets |
| 327 |
* |
| 328 |
* @param $ids |
| 329 |
*/ |
| 330 |
private function deactivateSnippets( $ids ) { |
| 331 |
if ( count( $ids ) ) { |
| 332 |
foreach ( $ids as $id ) { |
| 333 |
update_post_meta( $id, WINP_Plugin::app()->getPrefix() . 'snippet_activate', 0 ); |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* Activate snippets |
| 340 |
* |
| 341 |
* @param $ids |
| 342 |
*/ |
| 343 |
private function activateSnippets( $ids ) { |
| 344 |
if ( count( $ids ) ) { |
| 345 |
foreach ( $ids as $id ) { |
| 346 |
$is_activate = (int) WINP_Helper::getMetaOption( $id, 'snippet_activate', 0 ); |
| 347 |
$snippet_scope = WINP_Helper::getMetaOption( $id, 'snippet_scope' ); |
| 348 |
$snippet_type = WINP_Helper::get_snippet_type( $id ); |
| 349 |
|
| 350 |
if ( ( $snippet_scope == 'evrywhere' || $snippet_scope == 'auto' ) && ! $is_activate && $snippet_type != WINP_SNIPPET_TYPE_TEXT && WINP_Plugin::app()->getExecuteObject()->getSnippetError( $id ) ) { |
| 351 |
continue; |
| 352 |
} |
| 353 |
|
| 354 |
update_post_meta( $id, WINP_Plugin::app()->getPrefix() . 'snippet_activate', 1 ); |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
/** |
| 361 |
* Get taxonomy tags |
| 362 |
* |
| 363 |
* @param $snippet_id |
| 364 |
* |
| 365 |
* @return array |
| 366 |
*/ |
| 367 |
private function getTaxonomyTags( $snippet_id ) { |
| 368 |
$tags = []; |
| 369 |
|
| 370 |
if ( $snippet_id ) { |
| 371 |
return wp_get_post_terms( $snippet_id, WINP_SNIPPETS_TAXONOMY, [ "fields" => "slugs" ] ); |
| 372 |
} |
| 373 |
|
| 374 |
return $tags; |
| 375 |
} |
| 376 |
|
| 377 |
/** |
| 378 |
* Get post meta |
| 379 |
* |
| 380 |
* @param $post_id |
| 381 |
* @param $meta_name |
| 382 |
* |
| 383 |
* @return mixed |
| 384 |
*/ |
| 385 |
private function getMeta( $post_id, $meta_name ) { |
| 386 |
return get_post_meta( $post_id, WINP_Plugin::app()->getPrefix() . $meta_name, true ); |
| 387 |
} |
| 388 |
|
| 389 |
} |