| @@ -5,10 +5,8 @@ | ||
| 5 | 5 | * @package Copy the Code |
| 6 | 6 | * @since 1.2.0 |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | -use CopyTheCode\Helpers; | |
| 10 | - | |
| 11 | 9 | if ( ! class_exists( 'Copy_The_Code_Page' ) ) : |
| 12 | 10 | |
| 13 | 11 | /** |
| 14 | 12 | * Copy_The_Code_Page |
| @@ -29,9 +27,9 @@ | ||
| 29 | 27 | |
| 30 | 28 | /** |
| 31 | 29 | * Current selector |
| 32 | 30 | * |
| 33 | - * @since 3.0.0 | |
| 31 | + * @since x.x.x | |
| 34 | 32 | * |
| 35 | 33 | * @access private |
| 36 | 34 | * @var string Current CSS selector. |
| 37 | 35 | */ |
| @@ -58,86 +56,17 @@ | ||
| 58 | 56 | */ |
| 59 | 57 | public function __construct() { |
| 60 | 58 | add_filter( 'admin_url', array( $this, 'admin_url' ), 10, 3 ); |
| 61 | 59 | add_action( 'plugin_action_links_' . COPY_THE_CODE_BASE, array( $this, 'action_links' ) ); |
| 60 | + add_action( 'init', array( $this, 'save_settings' ), 11 ); | |
| 62 | 61 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) ); |
| 62 | + add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_assets' ) ); | |
| 63 | 63 | add_action( 'init', array( $this, 'register_post_type' ) ); |
| 64 | - add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) ); | |
| 65 | 64 | add_action( 'manage_copy-to-clipboard_posts_custom_column', array( $this, 'column_markup' ), 10, 2 ); |
| 66 | 65 | add_action( 'manage_copy-to-clipboard_posts_columns', array( $this, 'add_column' ), 10 ); |
| 67 | 66 | } |
| 68 | 67 | |
| 69 | 68 | /** |
| 70 | - * Add meta box | |
| 71 | - * | |
| 72 | - * @since 3.0.0 | |
| 73 | - */ | |
| 74 | - public function add_meta_box() { | |
| 75 | - add_meta_box( | |
| 76 | - 'copy-the-code-meta-box', | |
| 77 | - __( 'Copy to Clipboard Settings', 'copy-the-code' ), | |
| 78 | - array( $this, 'meta_box_markup' ), | |
| 79 | - 'copy-to-clipboard', | |
| 80 | - 'normal', | |
| 81 | - 'high' | |
| 82 | - ); | |
| 83 | - } | |
| 84 | - | |
| 85 | - /** | |
| 86 | - * Meta box markup | |
| 87 | - * | |
| 88 | - * @param object $post Post object. | |
| 89 | - * @since 3.0.0 | |
| 90 | - */ | |
| 91 | - public function meta_box_markup( $post ) { | |
| 92 | - $selector = get_post_meta( $post->ID, 'selector', true ); | |
| 93 | - $style = get_post_meta( $post->ID, 'style', true ); | |
| 94 | - $button_text = get_post_meta( $post->ID, 'button-text', true ); | |
| 95 | - $button_title = get_post_meta( $post->ID, 'button-title', true ); | |
| 96 | - $button_copy_text = get_post_meta( $post->ID, 'button-copy-text', true ); | |
| 97 | - $button_position = get_post_meta( $post->ID, 'button-position', true ); | |
| 98 | - $copy_format = get_post_meta( $post->ID, 'copy-format', true ); | |
| 99 | - | |
| 100 | - ?> | |
| 101 | - <table class="form-table"> | |
| 102 | - <tr> | |
| 103 | - <td><?php esc_html_e( 'CSS Selector', 'copy-the-code' ); ?></td> | |
| 104 | - <td><?php echo esc_html( $selector ); ?></td> | |
| 105 | - </tr> | |
| 106 | - <tr> | |
| 107 | - <td><?php esc_html_e( 'Style', 'copy-the-code' ); ?></td> | |
| 108 | - <td><?php echo esc_html( $style ); ?></td> | |
| 109 | - </tr> | |
| 110 | - <tr> | |
| 111 | - <td><?php esc_html_e( 'Button Text', 'copy-the-code' ); ?></td> | |
| 112 | - <td><?php echo esc_html( $button_text ); ?></td> | |
| 113 | - </tr> | |
| 114 | - <tr> | |
| 115 | - <td><?php esc_html_e( 'Button Title', 'copy-the-code' ); ?></td> | |
| 116 | - <td><?php echo esc_html( $button_title ); ?></td> | |
| 117 | - </tr> | |
| 118 | - <tr> | |
| 119 | - <td><?php esc_html_e( 'Button Copy Text', 'copy-the-code' ); ?></td> | |
| 120 | - <td><?php echo esc_html( $button_copy_text ); ?></td> | |
| 121 | - </tr> | |
| 122 | - <tr> | |
| 123 | - <td><?php esc_html_e( 'Button Position', 'copy-the-code' ); ?></td> | |
| 124 | - <td><?php echo esc_html( $button_position ); ?></td> | |
| 125 | - </tr> | |
| 126 | - <tr> | |
| 127 | - <td><?php esc_html_e( 'Copy Format', 'copy-the-code' ); ?></td> | |
| 128 | - <td><?php echo esc_html( $copy_format ); ?></td> | |
| 129 | - </tr> | |
| 130 | - </table> | |
| 131 | - | |
| 132 | - <p> | |
| 133 | - <a href="<?php echo esc_url( admin_url( 'options-general.php?page=copy-the-code&id=' . $post->ID ) ); ?>" class="button button-primary"><?php esc_html_e( 'Edit', 'copy-the-code' ); ?></a> | |
| 134 | - </p> | |
| 135 | - <?php | |
| 136 | - | |
| 137 | - } | |
| 138 | - | |
| 139 | - /** | |
| 140 | 69 | * Add custom column |
| 141 | 70 | * |
| 142 | 71 | * @param array $columns Columns. |
| 143 | 72 | * @since 2.0.0 |
| @@ -203,12 +132,8 @@ | ||
| 203 | 132 | $button_position = get_post_meta( $post_id, 'button-position', true ); |
| 204 | 133 | if ( ! empty( $button_position ) ) { |
| 205 | 134 | echo '<i>Button Position: </i><b>' . $button_position . '</b><br/>'; |
| 206 | 135 | } |
| 207 | - $format = get_post_meta( $post_id, 'copy-format', true ); | |
| 208 | - if ( ! empty( $format ) ) { | |
| 209 | - echo '<i>Copy Format: </i><b>' . $format . '</b><br/>'; | |
| 210 | - } | |
| 211 | 136 | } |
| 212 | 137 | |
| 213 | 138 | } |
| 214 | 139 | |
| @@ -227,9 +152,9 @@ | ||
| 227 | 152 | return $url; |
| 228 | 153 | } |
| 229 | 154 | |
| 230 | 155 | $url = get_site_url( $blog_id, 'wp-admin/', 'admin' ); |
| 231 | - $path = 'options-general.php?page=copy-the-code'; | |
| 156 | + $path = 'options-general.php?page=copy-to-clipboard-add-new'; | |
| 232 | 157 | |
| 233 | 158 | if ( $path && is_string( $path ) ) { |
| 234 | 159 | $url .= ltrim( $path, '/' ); |
| 235 | 160 | } |
| @@ -287,11 +212,13 @@ | ||
| 287 | 212 | 'query_var' => false, |
| 288 | 213 | 'can_export' => true, |
| 289 | 214 | 'rewrite' => false, |
| 290 | 215 | 'capability_type' => 'post', |
| 291 | - 'supports' => [ | |
| 216 | + 'supports' => array( | |
| 292 | 217 | 'title', |
| 293 | - ], | |
| 218 | + 'author', | |
| 219 | + 'custom-fields', | |
| 220 | + ), | |
| 294 | 221 | ); |
| 295 | 222 | |
| 296 | 223 | register_post_type( 'copy-to-clipboard', $args ); |
| 297 | 224 | } |
| @@ -298,8 +225,32 @@ | ||
| 298 | 225 | |
| 299 | 226 | /** |
| 300 | 227 | * Enqueue Assets. |
| 301 | 228 | * |
| 229 | + * @version 1.7.0 | |
| 230 | + * | |
| 231 | + * @param string $hook Current hook name. | |
| 232 | + * @return mixed | |
| 233 | + */ | |
| 234 | + function admin_enqueue_assets( $hook = '' ) { | |
| 235 | + | |
| 236 | + if ( 'settings_page_copy-to-clipboard-add-new' !== $hook ) { | |
| 237 | + return; | |
| 238 | + } | |
| 239 | + | |
| 240 | + wp_enqueue_script( 'copy-the-code-page', COPY_THE_CODE_URI . 'assets/js/page.js', array( 'jquery' ), COPY_THE_CODE_VER, true ); | |
| 241 | + wp_enqueue_style( 'copy-the-code-page', COPY_THE_CODE_URI . 'assets/css/page.css', null, COPY_THE_CODE_VER, 'all' ); | |
| 242 | + | |
| 243 | + wp_localize_script( | |
| 244 | + 'copy-the-code-page', | |
| 245 | + 'copyTheCode', | |
| 246 | + $this->get_localize_vars() | |
| 247 | + ); | |
| 248 | + } | |
| 249 | + | |
| 250 | + /** | |
| 251 | + * Enqueue Assets. | |
| 252 | + * | |
| 302 | 253 | * @version 1.0.0 |
| 303 | 254 | * |
| 304 | 255 | * @return void |
| 305 | 256 | */ |
| @@ -340,9 +291,8 @@ | ||
| 340 | 291 | 'button_text' => get_post_meta( $post_id, 'button-text', true ), |
| 341 | 292 | 'button_title' => get_post_meta( $post_id, 'button-title', true ), |
| 342 | 293 | 'button_copy_text' => get_post_meta( $post_id, 'button-copy-text', true ), |
| 343 | 294 | 'button_position' => get_post_meta( $post_id, 'button-position', true ), |
| 344 | - 'copy_format' => get_post_meta( $post_id, 'copy-format', true ), | |
| 345 | 295 | ); |
| 346 | 296 | } |
| 347 | 297 | } |
| 348 | 298 | |
| @@ -348,14 +298,12 @@ | ||
| 348 | 298 | |
| 349 | 299 | return apply_filters( |
| 350 | 300 | 'copy_the_code_localize_vars', |
| 351 | 301 | array( |
| 352 | - 'trim_lines' => false, | |
| 353 | - 'remove_spaces' => true, | |
| 354 | 302 | 'copy_content_as' => '', |
| 355 | 303 | 'previewMarkup' => '<h2>Hello World</h2>', |
| 356 | 304 | 'buttonMarkup' => '<button class="copy-the-code-button" title=""></button>', |
| 357 | - 'buttonSvg' => Helpers::get_svg_copy_icon(), | |
| 305 | + 'buttonSvg' => '<svg viewBox="-21 0 512 512" xmlns="http://www.w3.org/2000/svg"><path d="m186.667969 416c-49.984375 0-90.667969-40.683594-90.667969-90.667969v-218.664062h-37.332031c-32.363281 0-58.667969 26.300781-58.667969 58.664062v288c0 32.363281 26.304688 58.667969 58.667969 58.667969h266.664062c32.363281 0 58.667969-26.304688 58.667969-58.667969v-37.332031zm0 0"></path><path d="m469.332031 58.667969c0-32.40625-26.261719-58.667969-58.664062-58.667969h-224c-32.40625 0-58.667969 26.261719-58.667969 58.667969v266.664062c0 32.40625 26.261719 58.667969 58.667969 58.667969h224c32.402343 0 58.664062-26.261719 58.664062-58.667969zm0 0"></path></svg>', | |
| 358 | 306 | 'selectors' => $selectors, |
| 359 | 307 | 'selector' => 'pre', // Selector in which have the actual `<code>`. |
| 360 | 308 | 'settings' => $this->get_page_settings(), |
| 361 | 309 | 'string' => array( |
| @@ -363,14 +311,81 @@ | ||
| 363 | 311 | 'copy' => $this->get_page_setting( 'button-text', __( 'Copy', 'copy-the-code' ) ), |
| 364 | 312 | 'copied' => $this->get_page_setting( 'button-copy-text', __( 'Copied!', 'copy-the-code' ) ), |
| 365 | 313 | ), |
| 366 | 314 | 'image-url' => COPY_THE_CODE_URI . '/assets/images/copy-1.svg', |
| 367 | - 'redirect_url' => '', | |
| 368 | 315 | ) |
| 369 | 316 | ); |
| 370 | 317 | } |
| 371 | 318 | |
| 372 | 319 | /** |
| 320 | + * Admin Settings | |
| 321 | + * | |
| 322 | + * @return void | |
| 323 | + */ | |
| 324 | + function save_settings() { | |
| 325 | + | |
| 326 | + if ( ! isset( $_REQUEST['page'] ) || ! isset( $_REQUEST['copy-the-code'] ) ) { | |
| 327 | + return; | |
| 328 | + } | |
| 329 | + | |
| 330 | + if ( strpos( $_REQUEST['page'], 'copy-to-clipboard-add-new' ) === false ) { | |
| 331 | + return; | |
| 332 | + } | |
| 333 | + | |
| 334 | + // Only admins can save settings. | |
| 335 | + if ( ! current_user_can( 'manage_options' ) ) { | |
| 336 | + return; | |
| 337 | + } | |
| 338 | + | |
| 339 | + // Make sure we have a valid nonce. | |
| 340 | + if ( ! wp_verify_nonce( $_REQUEST['copy-the-code'], 'copy-the-code-nonce' ) ) { | |
| 341 | + return; | |
| 342 | + } | |
| 343 | + | |
| 344 | + $selector = ( isset( $_REQUEST['selector'] ) ) ? $_REQUEST['selector'] : 'pre'; | |
| 345 | + | |
| 346 | + $query_args = array( | |
| 347 | + 'post_type' => 'copy-to-clipboard', | |
| 348 | + | |
| 349 | + // Query performance optimization. | |
| 350 | + 'fields' => 'ids', | |
| 351 | + 'no_found_rows' => true, | |
| 352 | + 'posts_per_page' => -1, | |
| 353 | + 'meta_key' => 'selector', | |
| 354 | + 'meta_value' => $selector, | |
| 355 | + ); | |
| 356 | + | |
| 357 | + $query = new WP_Query( $query_args ); | |
| 358 | + if ( $query->post_count ) { | |
| 359 | + $this->selector = $selector; | |
| 360 | + add_action( 'admin_notices', array( $this, 'selector_exist_notice' ) ); | |
| 361 | + return; | |
| 362 | + } | |
| 363 | + | |
| 364 | + // New settings. | |
| 365 | + $new_data = array( | |
| 366 | + 'selector' => $selector, | |
| 367 | + 'style' => ( isset( $_REQUEST['style'] ) ) ? $_REQUEST['style'] : 'button', | |
| 368 | + 'button-text' => ( isset( $_REQUEST['button-text'] ) ) ? $_REQUEST['button-text'] : 'Copy', | |
| 369 | + 'button-title' => ( isset( $_REQUEST['button-title'] ) ) ? $_REQUEST['button-title'] : 'Copy', | |
| 370 | + 'button-copy-text' => ( isset( $_REQUEST['button-copy-text'] ) ) ? $_REQUEST['button-copy-text'] : 'Copied!', | |
| 371 | + 'button-position' => ( isset( $_REQUEST['button-position'] ) ) ? $_REQUEST['button-position'] : 'inside', | |
| 372 | + ); | |
| 373 | + | |
| 374 | + $data = array( | |
| 375 | + 'post_type' => 'copy-to-clipboard', | |
| 376 | + 'post_status' => 'publish', | |
| 377 | + 'post_title' => $new_data['selector'], | |
| 378 | + 'meta_input' => $new_data, | |
| 379 | + ); | |
| 380 | + wp_insert_post( $data ); | |
| 381 | + | |
| 382 | + wp_safe_redirect( admin_url( 'edit.php?post_type=copy-to-clipboard' ) ); | |
| 383 | + | |
| 384 | + exit(); | |
| 385 | + } | |
| 386 | + | |
| 387 | + /** | |
| 373 | 388 | * Get Setting |
| 374 | 389 | * |
| 375 | 390 | * @param string $key Setting key. |
| 376 | 391 | * @param string $default_value Setting default value. |
| @@ -386,8 +401,22 @@ | ||
| 386 | 401 | return $default_value; |
| 387 | 402 | } |
| 388 | 403 | |
| 389 | 404 | /** |
| 405 | + * Selector is exist notice. | |
| 406 | + * | |
| 407 | + * @since 2.0.0 | |
| 408 | + * @return void | |
| 409 | + */ | |
| 410 | + function selector_exist_notice() { | |
| 411 | + ?> | |
| 412 | + <div class="notice notice-error"> | |
| 413 | + <p><?php _e( 'Opp! The selector <b>' . $this->selector . '</b> is already exist! Please try another selector.', 'copy-the-code' ); ?></p> | |
| 414 | + </div> | |
| 415 | + <?php | |
| 416 | + } | |
| 417 | + | |
| 418 | + /** | |
| 390 | 419 | * Settings |
| 391 | 420 | * |
| 392 | 421 | * @return array Settings. |
| 393 | 422 | */ |
| @@ -400,9 +429,8 @@ | ||
| 400 | 429 | 'button-text' => __( 'Copy', 'copy-the-code' ), |
| 401 | 430 | 'button-title' => __( 'Copy to Clipboard', 'copy-the-code' ), |
| 402 | 431 | 'button-copy-text' => __( 'Copied!', 'copy-the-code' ), |
| 403 | 432 | 'button-position' => 'inside', |
| 404 | - 'copy-format' => 'default', | |
| 405 | 433 | ) |
| 406 | 434 | ); |
| 407 | 435 | |
| 408 | 436 | $stored = get_option( 'copy-the-code-settings', $defaults ); |
| @@ -417,9 +445,9 @@ | ||
| 417 | 445 | * @return array |
| 418 | 446 | */ |
| 419 | 447 | function action_links( $links ) { |
| 420 | 448 | $action_links = array( |
| 421 | - 'add-new' => '<a href="' . admin_url( 'options-general.php?page=copy-the-code' ) . '" aria-label="' . esc_attr__( 'Add new', 'copy-the-code' ) . '">' . esc_html__( 'Add new', 'copy-the-code' ) . '</a>', | |
| 449 | + 'add-new' => '<a href="' . admin_url( 'options-general.php?page=copy-to-clipboard-add-new' ) . '" aria-label="' . esc_attr__( 'Add new', 'copy-the-code' ) . '">' . esc_html__( 'Add new', 'copy-the-code' ) . '</a>', | |
| 422 | 450 | ); |
| 423 | 451 | |
| 424 | 452 | return array_merge( $action_links, $links ); |
| 425 | 453 | } |