| @@ -1,588 +1,379 @@ | ||
| 1 | -<?php | |
| 2 | -/** | |
| 3 | - * Settings Page | |
| 4 | - * | |
| 5 | - * @package Copy the Code | |
| 6 | - * @since 1.2.0 | |
| 7 | - */ | |
| 8 | - | |
| 9 | -use CopyTheCode\Helpers; | |
| 10 | - | |
| 11 | -if ( ! class_exists( 'Copy_The_Code_Page' ) ) : | |
| 12 | - | |
| 13 | - /** | |
| 14 | - * Copy_The_Code_Page | |
| 15 | - * | |
| 16 | - * @since 1.2.0 | |
| 17 | - */ | |
| 18 | - class Copy_The_Code_Page { | |
| 19 | - | |
| 20 | - /** | |
| 21 | - * Instance | |
| 22 | - * | |
| 23 | - * @since 1.2.0 | |
| 24 | - * | |
| 25 | - * @access private | |
| 26 | - * @var object Class object. | |
| 27 | - */ | |
| 28 | - private static $instance; | |
| 29 | - | |
| 30 | - /** | |
| 31 | - * Current selector | |
| 32 | - * | |
| 33 | - * @since 3.0.0 | |
| 34 | - * | |
| 35 | - * @access private | |
| 36 | - * @var string Current CSS selector. | |
| 37 | - */ | |
| 38 | - private $selector; | |
| 39 | - | |
| 40 | - /** | |
| 41 | - * Initiator | |
| 42 | - * | |
| 43 | - * @since 1.2.0 | |
| 44 | - * | |
| 45 | - * @return object initialized object of class. | |
| 46 | - */ | |
| 47 | - public static function get_instance() { | |
| 48 | - if ( ! isset( self::$instance ) ) { | |
| 49 | - self::$instance = new self(); | |
| 50 | - } | |
| 51 | - return self::$instance; | |
| 52 | - } | |
| 53 | - | |
| 54 | - /** | |
| 55 | - * Constructor | |
| 56 | - * | |
| 57 | - * @since 1.2.0 | |
| 58 | - */ | |
| 59 | - public function __construct() { | |
| 60 | - add_filter( 'admin_url', [ $this, 'admin_url' ], 10, 3 ); | |
| 61 | - add_action( 'plugin_action_links_' . COPY_THE_CODE_BASE, [ $this, 'action_links' ] ); | |
| 62 | - add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_assets' ] ); | |
| 63 | - add_action( 'init', [ $this, 'register_post_type' ] ); | |
| 64 | - add_action( 'add_meta_boxes', [ $this, 'add_meta_box' ] ); | |
| 65 | - add_action( 'manage_copy-to-clipboard_posts_custom_column', [ $this, 'column_markup' ], 10, 2 ); | |
| 66 | - add_action( 'manage_copy-to-clipboard_posts_columns', [ $this, 'add_column' ], 10 ); | |
| 67 | - } | |
| 68 | - | |
| 69 | - /** | |
| 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 | - esc_html__( 'Settings', 'copy-the-code' ), | |
| 78 | - [ $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 | - * Add custom column | |
| 141 | - * | |
| 142 | - * @param array $columns Columns. | |
| 143 | - * @since 2.0.0 | |
| 144 | - */ | |
| 145 | - function add_column( $columns = [] ) { | |
| 146 | - | |
| 147 | - if ( isset( $columns['author'] ) ) { | |
| 148 | - unset( $columns['author'] ); | |
| 149 | - } | |
| 150 | - | |
| 151 | - if ( isset( $columns['date'] ) ) { | |
| 152 | - unset( $columns['date'] ); | |
| 153 | - } | |
| 154 | - | |
| 155 | - $new_columns = [ | |
| 156 | - 'style' => __( 'Style', 'copy-the-code' ), | |
| 157 | - 'settings' => __( 'Settings', 'copy-the-code' ), | |
| 158 | - 'author' => 'Author', | |
| 159 | - 'date' => 'Date', | |
| 160 | - ]; | |
| 161 | - | |
| 162 | - return wp_parse_args( $new_columns, $columns ); | |
| 163 | - } | |
| 164 | - | |
| 165 | - /** | |
| 166 | - * Column markup | |
| 167 | - * | |
| 168 | - * @since 2.0.0 | |
| 169 | - * | |
| 170 | - * @param string $column_name Column slug. | |
| 171 | - * @param integer $post_id Post ID. | |
| 172 | - * @return void | |
| 173 | - */ | |
| 174 | - function column_markup( $column_name = '', $post_id = 0 ) { | |
| 175 | - | |
| 176 | - if ( 'style' === $column_name ) { | |
| 177 | - $style = get_post_meta( $post_id, 'style', true ); | |
| 178 | - switch ( $style ) { | |
| 179 | - case 'cover': | |
| 180 | - echo 'Cover'; | |
| 181 | - break; | |
| 182 | - case 'svg-icon': | |
| 183 | - echo 'SVG Icon'; | |
| 184 | - break; | |
| 185 | - case 'button': | |
| 186 | - echo 'Button'; | |
| 187 | - break; | |
| 188 | - } | |
| 189 | - } | |
| 190 | - if ( 'settings' === $column_name ) { | |
| 191 | - $button_text = get_post_meta( $post_id, 'button-text', true ); | |
| 192 | - if ( ! empty( $button_text ) ) { | |
| 193 | - echo '<i>Button Text: </i><b>' . $button_text . '</b><br/>'; | |
| 194 | - } | |
| 195 | - $button_title = get_post_meta( $post_id, 'button-title', true ); | |
| 196 | - if ( ! empty( $button_title ) ) { | |
| 197 | - echo '<i>Button Title: </i><b>' . $button_title . '</b><br/>'; | |
| 198 | - } | |
| 199 | - $button_copy_text = get_post_meta( $post_id, 'button-copy-text', true ); | |
| 200 | - if ( ! empty( $button_copy_text ) ) { | |
| 201 | - echo '<i>Button Copy Text: </i><b>' . $button_copy_text . '</b><br/>'; | |
| 202 | - } | |
| 203 | - $button_position = get_post_meta( $post_id, 'button-position', true ); | |
| 204 | - if ( ! empty( $button_position ) ) { | |
| 205 | - echo '<i>Button Position: </i><b>' . $button_position . '</b><br/>'; | |
| 206 | - } | |
| 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 | - } | |
| 212 | - | |
| 213 | - } | |
| 214 | - | |
| 215 | - /** | |
| 216 | - * Filters the admin area URL. | |
| 217 | - * | |
| 218 | - * @since 1.0.2 | |
| 219 | - * | |
| 220 | - * @param string $url The complete admin area URL including scheme and path. | |
| 221 | - * @param string $path Path relative to the admin area URL. Blank string if no path is specified. | |
| 222 | - * @param int|null $blog_id Site ID, or null for the current site. | |
| 223 | - */ | |
| 224 | - public function admin_url( $url, $path, $blog_id ) { | |
| 225 | - | |
| 226 | - if ( 'post-new.php?post_type=copy-to-clipboard' !== $path ) { | |
| 227 | - return $url; | |
| 228 | - } | |
| 229 | - | |
| 230 | - $url = get_site_url( $blog_id, 'wp-admin/', 'admin' ); | |
| 231 | - $path = 'options-general.php?page=copy-the-code'; | |
| 232 | - | |
| 233 | - if ( $path && is_string( $path ) ) { | |
| 234 | - $url .= ltrim( $path, '/' ); | |
| 235 | - } | |
| 236 | - | |
| 237 | - return $url; | |
| 238 | - } | |
| 239 | - | |
| 240 | - /** | |
| 241 | - * Add new page | |
| 242 | - * | |
| 243 | - * @since 2.0.0 | |
| 244 | - */ | |
| 245 | - public function add_new_page() { | |
| 246 | - $data = $this->get_page_settings(); | |
| 247 | - require_once COPY_THE_CODE_DIR . 'includes/add-new-form.php'; | |
| 248 | - } | |
| 249 | - | |
| 250 | - /** | |
| 251 | - * Registers a new post type | |
| 252 | - * | |
| 253 | - * @since 2.0.0 | |
| 254 | - */ | |
| 255 | - function register_post_type() { | |
| 256 | - | |
| 257 | - $labels = [ | |
| 258 | - 'name' => __( 'Copy to Clipboard', 'copy-the-code' ), | |
| 259 | - 'singular_name' => __( 'Copy to Clipboard', 'copy-the-code' ), | |
| 260 | - 'add_new' => _x( 'Add New', 'copy-the-code', 'copy-the-code' ), | |
| 261 | - 'add_new_item' => __( 'Add New', 'copy-the-code' ), | |
| 262 | - 'edit_item' => __( 'Edit Copy to Clipboard', 'copy-the-code' ), | |
| 263 | - 'new_item' => __( 'New Copy to Clipboard', 'copy-the-code' ), | |
| 264 | - 'view_item' => __( 'View Copy to Clipboard', 'copy-the-code' ), | |
| 265 | - 'search_items' => __( 'Search Copy to Clipboard', 'copy-the-code' ), | |
| 266 | - 'not_found' => __( 'No Copy to Clipboard found', 'copy-the-code' ), | |
| 267 | - 'not_found_in_trash' => __( 'No Copy to Clipboard found in Trash', 'copy-the-code' ), | |
| 268 | - 'parent_item_colon' => __( 'Parent Copy to Clipboard:', 'copy-the-code' ), | |
| 269 | - 'menu_name' => __( 'Copy to Clipboard', 'copy-the-code' ), | |
| 270 | - ]; | |
| 271 | - | |
| 272 | - $args = [ | |
| 273 | - 'labels' => $labels, | |
| 274 | - 'hierarchical' => false, | |
| 275 | - 'description' => 'description', | |
| 276 | - 'taxonomies' => [], | |
| 277 | - 'public' => false, | |
| 278 | - 'show_ui' => true, | |
| 279 | - 'show_in_menu' => 'options-general.php', | |
| 280 | - 'show_in_admin_bar' => false, | |
| 281 | - 'menu_position' => null, | |
| 282 | - 'menu_icon' => 'dashicons-clipboard', | |
| 283 | - 'show_in_nav_menus' => true, | |
| 284 | - 'publicly_queryable' => false, | |
| 285 | - 'exclude_from_search' => false, | |
| 286 | - 'has_archive' => false, | |
| 287 | - 'query_var' => false, | |
| 288 | - 'can_export' => true, | |
| 289 | - 'rewrite' => false, | |
| 290 | - 'capability_type' => 'post', | |
| 291 | - 'supports' => [ | |
| 292 | - 'title', | |
| 293 | - ], | |
| 294 | - ]; | |
| 295 | - | |
| 296 | - register_post_type( 'copy-to-clipboard', $args ); | |
| 297 | - } | |
| 298 | - | |
| 299 | - /** | |
| 300 | - * Enqueue Assets. | |
| 301 | - * | |
| 302 | - * @version 1.0.0 | |
| 303 | - * | |
| 304 | - * @return void | |
| 305 | - */ | |
| 306 | - public function enqueue_assets() { | |
| 307 | - $vars = $this->get_localize_vars(); | |
| 308 | - if ( ! $vars['selectors'] ) { | |
| 309 | - return; | |
| 310 | - } | |
| 311 | - | |
| 312 | - $conditions = []; | |
| 313 | - foreach ( $vars['selectors'] as $selector ) { | |
| 314 | - if ( ! $selector['conditions'] ) { | |
| 315 | - continue; | |
| 316 | - } | |
| 317 | - | |
| 318 | - $conditions = array_merge( $conditions, $selector['conditions'] ); | |
| 319 | - } | |
| 320 | - if ( ! $this->meet_conditions( $conditions ) ) { | |
| 321 | - return; | |
| 322 | - } | |
| 323 | - | |
| 324 | - wp_enqueue_style( 'copy-the-code', COPY_THE_CODE_URI . 'assets/css/copy-the-code.css', null, COPY_THE_CODE_VER, 'all' ); | |
| 325 | - wp_enqueue_script( 'copy-the-code', COPY_THE_CODE_URI . 'assets/js/copy-the-code.js', [ 'jquery' ], COPY_THE_CODE_VER, true ); | |
| 326 | - wp_localize_script( | |
| 327 | - 'copy-the-code', | |
| 328 | - 'copyTheCode', | |
| 329 | - $vars | |
| 330 | - ); | |
| 331 | - } | |
| 332 | - | |
| 333 | - /** | |
| 334 | - * Meet Conditions | |
| 335 | - * | |
| 336 | - * @param array $conditions Conditions. | |
| 337 | - * @return boolean | |
| 338 | - */ | |
| 339 | - public function meet_conditions( $conditions = [] ) { | |
| 340 | - if ( ! $conditions ) { | |
| 341 | - return true; | |
| 342 | - } | |
| 343 | - | |
| 344 | - $meet = true; | |
| 345 | - foreach ( $conditions as $condition ) { | |
| 346 | - $meet = $this->meet_condition( $condition ); | |
| 347 | - if ( ! $meet ) { | |
| 348 | - return false; | |
| 349 | - } | |
| 350 | - } | |
| 351 | - | |
| 352 | - return $meet; | |
| 353 | - } | |
| 354 | - | |
| 355 | - /** | |
| 356 | - * Meet Condition | |
| 357 | - * | |
| 358 | - * @param array $condition Condition. | |
| 359 | - * @return boolean | |
| 360 | - */ | |
| 361 | - public function meet_condition( $condition = [] ) { | |
| 362 | - if ( ! $condition ) { | |
| 363 | - return true; | |
| 364 | - } | |
| 365 | - | |
| 366 | - $meet = false; | |
| 367 | - switch ( $condition['type'] ) { | |
| 368 | - case 'post_type': | |
| 369 | - $meet = $this->meet_post_type_condition( $condition ); | |
| 370 | - break; | |
| 371 | - case 'user_role': | |
| 372 | - $meet = $this->meet_user_role_condition( $condition ); | |
| 373 | - break; | |
| 374 | - case 'taxonomy': | |
| 375 | - $meet = $this->meet_taxonomy_condition( $condition ); | |
| 376 | - break; | |
| 377 | - } | |
| 378 | - | |
| 379 | - return $meet; | |
| 380 | - } | |
| 381 | - | |
| 382 | - /** | |
| 383 | - * Meet Post Type Condition | |
| 384 | - * | |
| 385 | - * @param array $condition Condition. | |
| 386 | - * @return boolean | |
| 387 | - */ | |
| 388 | - public function meet_post_type_condition( $condition = [] ) { | |
| 389 | - if ( ! $condition ) { | |
| 390 | - return true; | |
| 391 | - } | |
| 392 | - | |
| 393 | - $post_type = get_post_type(); | |
| 394 | - if ( ! $post_type ) { | |
| 395 | - return false; | |
| 396 | - } | |
| 397 | - | |
| 398 | - $meet = false; | |
| 399 | - switch ( $condition['operator'] ) { | |
| 400 | - case '=': | |
| 401 | - $meet = $post_type === $condition['value']; | |
| 402 | - break; | |
| 403 | - case '!=': | |
| 404 | - $meet = $post_type !== $condition['value']; | |
| 405 | - break; | |
| 406 | - } | |
| 407 | - | |
| 408 | - return $meet; | |
| 409 | - } | |
| 410 | - | |
| 411 | - /** | |
| 412 | - * Meet User Role Condition | |
| 413 | - * | |
| 414 | - * @param array $condition Condition. | |
| 415 | - * @return boolean | |
| 416 | - */ | |
| 417 | - public function meet_user_role_condition( $condition = [] ) { | |
| 418 | - if ( ! $condition ) { | |
| 419 | - return true; | |
| 420 | - } | |
| 421 | - | |
| 422 | - $user = wp_get_current_user(); | |
| 423 | - if ( ! $user ) { | |
| 424 | - return false; | |
| 425 | - } | |
| 426 | - | |
| 427 | - $meet = false; | |
| 428 | - switch ( $condition['operator'] ) { | |
| 429 | - case '=': | |
| 430 | - $meet = in_array( $condition['value'], $user->roles, true ); | |
| 431 | - break; | |
| 432 | - case '!=': | |
| 433 | - $meet = ! in_array( $condition['value'], $user->roles, true ); | |
| 434 | - break; | |
| 435 | - } | |
| 436 | - | |
| 437 | - return $meet; | |
| 438 | - } | |
| 439 | - | |
| 440 | - /** | |
| 441 | - * Meet Taxonomy Condition | |
| 442 | - * | |
| 443 | - * @param array $condition Condition. | |
| 444 | - * @return boolean | |
| 445 | - */ | |
| 446 | - public function meet_taxonomy_condition( $condition = [] ) { | |
| 447 | - if ( ! $condition ) { | |
| 448 | - return true; | |
| 449 | - } | |
| 450 | - | |
| 451 | - $taxonomy = get_current_screen()->taxonomy; | |
| 452 | - if ( ! $taxonomy ) { | |
| 453 | - return false; | |
| 454 | - } | |
| 455 | - | |
| 456 | - $meet = false; | |
| 457 | - switch ( $condition['operator'] ) { | |
| 458 | - case '=': | |
| 459 | - $meet = $taxonomy === $condition['value']; | |
| 460 | - break; | |
| 461 | - case '!=': | |
| 462 | - $meet = $taxonomy !== $condition['value']; | |
| 463 | - break; | |
| 464 | - } | |
| 465 | - | |
| 466 | - return $meet; | |
| 467 | - } | |
| 468 | - | |
| 469 | - /** | |
| 470 | - * Localize Vars | |
| 471 | - * | |
| 472 | - * @return array | |
| 473 | - */ | |
| 474 | - function get_localize_vars() { | |
| 475 | - | |
| 476 | - $query_args = [ | |
| 477 | - 'post_type' => 'copy-to-clipboard', | |
| 478 | - | |
| 479 | - // Query performance optimization. | |
| 480 | - 'fields' => 'ids', | |
| 481 | - 'no_found_rows' => true, | |
| 482 | - 'posts_per_page' => -1, | |
| 483 | - ]; | |
| 484 | - | |
| 485 | - $query = new WP_Query( $query_args ); | |
| 486 | - $selectors = []; | |
| 487 | - if ( $query->posts ) { | |
| 488 | - foreach ( $query->posts as $key => $post_id ) { | |
| 489 | - $selectors[] = [ | |
| 490 | - 'selector' => get_post_meta( $post_id, 'selector', true ), | |
| 491 | - 'style' => get_post_meta( $post_id, 'style', true ), | |
| 492 | - // '/ $copy_as' => get_post_meta( $post_id, 'copy-as', true ), | |
| 493 | - 'button_text' => get_post_meta( $post_id, 'button-text', true ), | |
| 494 | - 'button_title' => get_post_meta( $post_id, 'button-title', true ), | |
| 495 | - 'button_copy_text' => get_post_meta( $post_id, 'button-copy-text', true ), | |
| 496 | - 'button_position' => get_post_meta( $post_id, 'button-position', true ), | |
| 497 | - 'copy_format' => get_post_meta( $post_id, 'copy-format', true ), | |
| 498 | - 'conditions' => get_post_meta( $post_id, 'conditions', true ), | |
| 499 | - ]; | |
| 500 | - } | |
| 501 | - } | |
| 502 | - | |
| 503 | - return apply_filters( | |
| 504 | - 'copy_the_code_localize_vars', | |
| 505 | - [ | |
| 506 | - 'trim_lines' => false, | |
| 507 | - 'remove_spaces' => true, | |
| 508 | - 'copy_content_as' => '', | |
| 509 | - 'previewMarkup' => '<h2>Hello World</h2>', | |
| 510 | - 'buttonMarkup' => '<button class="copy-the-code-button" title=""></button>', | |
| 511 | - 'buttonSvg' => Helpers::get_svg_copy_icon(), | |
| 512 | - 'selectors' => $selectors, | |
| 513 | - 'selector' => 'pre', // Selector in which have the actual `<code>`. | |
| 514 | - 'settings' => $this->get_page_settings(), | |
| 515 | - 'string' => [ | |
| 516 | - 'title' => $this->get_page_setting( 'button-title', __( 'Copy to Clipboard', 'copy-the-code' ) ), | |
| 517 | - 'copy' => $this->get_page_setting( 'button-text', __( 'Copy to Clipboard', 'copy-the-code' ) ), | |
| 518 | - 'copied' => $this->get_page_setting( 'button-copy-text', __( 'Copied!', 'copy-the-code' ) ), | |
| 519 | - ], | |
| 520 | - 'image-url' => COPY_THE_CODE_URI . '/assets/images/copy-1.svg', | |
| 521 | - 'redirect_url' => '', | |
| 522 | - ] | |
| 523 | - ); | |
| 524 | - } | |
| 525 | - | |
| 526 | - /** | |
| 527 | - * Get Setting | |
| 528 | - * | |
| 529 | - * @param string $key Setting key. | |
| 530 | - * @param string $default_value Setting default value. | |
| 531 | - * @return mixed Single Setting. | |
| 532 | - */ | |
| 533 | - function get_page_setting( $key = '', $default_value = '' ) { | |
| 534 | - $settings = $this->get_page_settings(); | |
| 535 | - | |
| 536 | - if ( array_key_exists( $key, $settings ) ) { | |
| 537 | - return $settings[ $key ]; | |
| 538 | - } | |
| 539 | - | |
| 540 | - return $default_value; | |
| 541 | - } | |
| 542 | - | |
| 543 | - /** | |
| 544 | - * Settings | |
| 545 | - * | |
| 546 | - * @return array Settings. | |
| 547 | - */ | |
| 548 | - public function get_page_settings() { | |
| 549 | - $defaults = apply_filters( | |
| 550 | - 'copy_the_code_default_page_settings', | |
| 551 | - [ | |
| 552 | - 'selector' => 'pre', | |
| 553 | - // 'copy-as' => 'text', | |
| 554 | - 'button-text' => __( 'Copy to Clipboard', 'copy-the-code' ), | |
| 555 | - 'button-title' => __( 'Copy to Clipboard', 'copy-the-code' ), | |
| 556 | - 'button-copy-text' => __( 'Copied!', 'copy-the-code' ), | |
| 557 | - 'button-position' => 'inside', | |
| 558 | - 'copy-format' => 'default', | |
| 559 | - ] | |
| 560 | - ); | |
| 561 | - | |
| 562 | - $stored = get_option( 'copy-the-code-settings', $defaults ); | |
| 563 | - | |
| 564 | - return apply_filters( 'copy_the_code_page_settings', wp_parse_args( $stored, $defaults ) ); | |
| 565 | - } | |
| 566 | - | |
| 567 | - /** | |
| 568 | - * Show action links on the plugin screen. | |
| 569 | - * | |
| 570 | - * @param mixed $links Plugin Action links. | |
| 571 | - * @return array | |
| 572 | - */ | |
| 573 | - function action_links( $links ) { | |
| 574 | - $action_links = [ | |
| 575 | - '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>', | |
| 576 | - ]; | |
| 577 | - | |
| 578 | - return array_merge( $action_links, $links ); | |
| 579 | - } | |
| 580 | - | |
| 581 | - } | |
| 582 | - | |
| 583 | - /** | |
| 584 | - * Initialize class object with 'get_instance()' method | |
| 585 | - */ | |
| 586 | - Copy_The_Code_Page::get_instance(); | |
| 587 | - | |
| 588 | -endif; | |
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Settings Page | |
| 4 | + * | |
| 5 | + * @package Copy the Code | |
| 6 | + * @since 1.2.0 | |
| 7 | + */ | |
| 8 | + | |
| 9 | +if ( ! class_exists( 'Copy_The_Code_Page' ) ) : | |
| 10 | + | |
| 11 | + /** | |
| 12 | + * Copy_The_Code_Page | |
| 13 | + * | |
| 14 | + * @since 1.2.0 | |
| 15 | + */ | |
| 16 | + class Copy_The_Code_Page { | |
| 17 | + | |
| 18 | + /** | |
| 19 | + * Instance | |
| 20 | + * | |
| 21 | + * @since 1.2.0 | |
| 22 | + * | |
| 23 | + * @access private | |
| 24 | + * @var object Class object. | |
| 25 | + */ | |
| 26 | + private static $instance; | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * Initiator | |
| 30 | + * | |
| 31 | + * @since 1.2.0 | |
| 32 | + * | |
| 33 | + * @return object initialized object of class. | |
| 34 | + */ | |
| 35 | + public static function get_instance() { | |
| 36 | + if ( ! isset( self::$instance ) ) { | |
| 37 | + self::$instance = new self; | |
| 38 | + } | |
| 39 | + return self::$instance; | |
| 40 | + } | |
| 41 | + | |
| 42 | + /** | |
| 43 | + * Constructor | |
| 44 | + * | |
| 45 | + * @since 1.2.0 | |
| 46 | + */ | |
| 47 | + public function __construct() { | |
| 48 | + add_action( 'admin_menu', array( $this, 'register_admin_menu' ) ); | |
| 49 | + add_action( 'plugin_action_links_' . COPY_THE_CODE_BASE, array( $this, 'action_links' ) ); | |
| 50 | + add_action( 'after_setup_theme', array( $this, 'save_settings' ) ); | |
| 51 | + add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) ); | |
| 52 | + add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_assets' ) ); | |
| 53 | + } | |
| 54 | + | |
| 55 | + /** | |
| 56 | + * Enqueue Assets. | |
| 57 | + * | |
| 58 | + * @version 1.7.0 | |
| 59 | + * | |
| 60 | + * @return void | |
| 61 | + */ | |
| 62 | + function admin_enqueue_assets() { | |
| 63 | + wp_enqueue_script( 'copy-the-code-page', COPY_THE_CODE_URI . 'assets/js/page.js', array( 'jquery' ), COPY_THE_CODE_VER, true ); | |
| 64 | + } | |
| 65 | + | |
| 66 | + /** | |
| 67 | + * Enqueue Assets. | |
| 68 | + * | |
| 69 | + * @version 1.0.0 | |
| 70 | + * | |
| 71 | + * @return void | |
| 72 | + */ | |
| 73 | + function enqueue_assets() { | |
| 74 | + wp_enqueue_style( 'copy-the-code', COPY_THE_CODE_URI . 'assets/css/copy-the-code.css', null, COPY_THE_CODE_VER, 'all' ); | |
| 75 | + wp_enqueue_script( 'copy-the-code', COPY_THE_CODE_URI . 'assets/js/copy-the-code.js', array( 'jquery' ), COPY_THE_CODE_VER, true ); | |
| 76 | + wp_localize_script( | |
| 77 | + 'copy-the-code', | |
| 78 | + 'copyTheCode', | |
| 79 | + apply_filters( | |
| 80 | + 'copy_the_code_localize_vars', | |
| 81 | + array( | |
| 82 | + 'selector' => 'pre', // Selector in which have the actual `<code>`. | |
| 83 | + 'settings' => $this->get_page_settings(), | |
| 84 | + 'string' => array( | |
| 85 | + 'title' => $this->get_page_setting( 'button-title', __( 'Copy to Clipboard', 'copy-the-code' ) ), | |
| 86 | + 'copy' => $this->get_page_setting( 'button-text', __( 'Copy', 'copy-the-code' ) ), | |
| 87 | + 'copied' => $this->get_page_setting( 'button-copy-text', __( 'Copied!', 'copy-the-code' ) ), | |
| 88 | + ), | |
| 89 | + ) | |
| 90 | + ) | |
| 91 | + ); | |
| 92 | + } | |
| 93 | + | |
| 94 | + /** | |
| 95 | + * Admin Settings | |
| 96 | + * | |
| 97 | + * @return void | |
| 98 | + */ | |
| 99 | + function save_settings() { | |
| 100 | + | |
| 101 | + if ( isset( $_REQUEST['page'] ) && strpos( $_REQUEST['page'], 'copy-the-code' ) !== false ) { | |
| 102 | + | |
| 103 | + // Only admins can save settings. | |
| 104 | + if ( ! current_user_can( 'manage_options' ) ) { | |
| 105 | + return; | |
| 106 | + } | |
| 107 | + | |
| 108 | + // Make sure we have a valid nonce. | |
| 109 | + if ( isset( $_REQUEST['copy-the-code'] ) && wp_verify_nonce( $_REQUEST['copy-the-code'], 'copy-the-code-nonce' ) ) { | |
| 110 | + | |
| 111 | + // Stored Settings. | |
| 112 | + $stored_data = $this->get_page_settings(); | |
| 113 | + | |
| 114 | + // New settings. | |
| 115 | + $new_data = array( | |
| 116 | + 'selector' => ( isset( $_REQUEST['selector'] ) ) ? $_REQUEST['selector'] : 'pre', | |
| 117 | + 'copy-as' => ( isset( $_REQUEST['copy-as'] ) ) ? $_REQUEST['copy-as'] : 'html', | |
| 118 | + 'button-text' => ( isset( $_REQUEST['button-text'] ) ) ? $_REQUEST['button-text'] : 'Copy', | |
| 119 | + 'button-title' => ( isset( $_REQUEST['button-title'] ) ) ? $_REQUEST['button-title'] : 'Copy', | |
| 120 | + 'button-copy-text' => ( isset( $_REQUEST['button-copy-text'] ) ) ? $_REQUEST['button-copy-text'] : 'Copied!', | |
| 121 | + 'button-position' => ( isset( $_REQUEST['button-position'] ) ) ? $_REQUEST['button-position'] : 'inside', | |
| 122 | + ); | |
| 123 | + | |
| 124 | + // Merge settings. | |
| 125 | + $data = wp_parse_args( $new_data, $stored_data ); | |
| 126 | + | |
| 127 | + // Update settings. | |
| 128 | + update_option( 'copy-the-code-settings', $data ); | |
| 129 | + } | |
| 130 | + } | |
| 131 | + } | |
| 132 | + | |
| 133 | + /** | |
| 134 | + * Get Setting | |
| 135 | + * | |
| 136 | + * @return mixed Single Setting. | |
| 137 | + */ | |
| 138 | + function get_page_setting( $key = '', $default_value = '' ) { | |
| 139 | + $settings = $this->get_page_settings(); | |
| 140 | + | |
| 141 | + if ( array_key_exists( $key, $settings ) ) { | |
| 142 | + return $settings[ $key ]; | |
| 143 | + } | |
| 144 | + | |
| 145 | + return $default_value; | |
| 146 | + } | |
| 147 | + | |
| 148 | + /** | |
| 149 | + * Settings | |
| 150 | + * | |
| 151 | + * @return array Settings. | |
| 152 | + */ | |
| 153 | + function get_page_settings() { | |
| 154 | + $defaults = apply_filters( | |
| 155 | + 'copy_the_code_default_page_settings', | |
| 156 | + array( | |
| 157 | + 'selector' => 'pre', | |
| 158 | + 'copy-as' => 'html', | |
| 159 | + 'button-text' => __( 'Copy', 'copy-the-code' ), | |
| 160 | + 'button-title' => __( 'Copy to Clipboard', 'copy-the-code' ), | |
| 161 | + 'button-copy-text' => __( 'Copied!', 'copy-the-code' ), | |
| 162 | + 'button-position' => 'inside', | |
| 163 | + ) | |
| 164 | + ); | |
| 165 | + | |
| 166 | + $stored = get_option( 'copy-the-code-settings', $defaults ); | |
| 167 | + | |
| 168 | + return apply_filters( 'copy_the_code_page_settings', wp_parse_args( $stored, $defaults ) ); | |
| 169 | + } | |
| 170 | + | |
| 171 | + /** | |
| 172 | + * Show action links on the plugin screen. | |
| 173 | + * | |
| 174 | + * @param mixed $links Plugin Action links. | |
| 175 | + * @return array | |
| 176 | + */ | |
| 177 | + function action_links( $links ) { | |
| 178 | + $action_links = array( | |
| 179 | + 'settings' => '<a href="' . admin_url( 'options-general.php?page=copy-the-code' ) . '" aria-label="' . esc_attr__( 'Settings', 'copy-the-code' ) . '">' . esc_html__( 'Settings', 'copy-the-code' ) . '</a>', | |
| 180 | + ); | |
| 181 | + | |
| 182 | + return array_merge( $action_links, $links ); | |
| 183 | + } | |
| 184 | + | |
| 185 | + /** | |
| 186 | + * Register menu | |
| 187 | + * | |
| 188 | + * @since 1.2.0 | |
| 189 | + * @return void | |
| 190 | + */ | |
| 191 | + function register_admin_menu() { | |
| 192 | + add_submenu_page( 'options-general.php', __( 'Copy to Clipboard', 'copy-the-code' ), __( 'Copy to Clipboard', 'copy-the-code' ), 'manage_options', 'copy-the-code', array( $this, 'options_page' ) ); | |
| 193 | + } | |
| 194 | + | |
| 195 | + /** | |
| 196 | + * Tabs | |
| 197 | + * | |
| 198 | + * @since 1.7.0 | |
| 199 | + * @return array | |
| 200 | + */ | |
| 201 | + function get_tabs() { | |
| 202 | + return array( | |
| 203 | + 'general' => esc_html__( 'General', 'copy-the-code' ), | |
| 204 | + 'style' => esc_html__( 'Style', 'copy-the-code' ), | |
| 205 | + ); | |
| 206 | + } | |
| 207 | + | |
| 208 | + /** | |
| 209 | + * Option Page | |
| 210 | + * | |
| 211 | + * @since 1.2.0 | |
| 212 | + * @return void | |
| 213 | + */ | |
| 214 | + function options_page() { | |
| 215 | + $data = $this->get_page_settings(); | |
| 216 | + ?> | |
| 217 | + <div class="wrap copy-the-code" id="sync-post"> | |
| 218 | + <div class="wrap"> | |
| 219 | + <h1><?php echo esc_html( COPY_THE_CODE_TITLE ); ?> <small>v<?php echo esc_html( COPY_THE_CODE_VER ); ?></small></h1> | |
| 220 | + <div id="poststuff"> | |
| 221 | + <div id="post-body" class="columns-2"> | |
| 222 | + <div id="post-body-content"> | |
| 223 | + | |
| 224 | + <div class="nav-tab-wrapper"> | |
| 225 | + <?php $tabs = $this->get_tabs(); ?> | |
| 226 | + <?php foreach ($tabs as $tab_slug => $tab_title) { ?> | |
| 227 | + <a class="nav-tab" style="cursor: pointer;" data-id="tab-<?php echo esc_attr( $tab_slug ); ?>"><?php echo esc_html( $tab_title ); ?></a> | |
| 228 | + <?php } ?> | |
| 229 | + </div> | |
| 230 | + | |
| 231 | + <form enctype="multipart/form-data" method="post"> | |
| 232 | + <div class="tabs"> | |
| 233 | + <div id="tab-general" style="padding: 30px;"> | |
| 234 | + <table class="form-table"> | |
| 235 | + <tr> | |
| 236 | + <th scope="row"><?php _e( 'Selector', 'copy-the-code' ); ?></th> | |
| 237 | + <td> | |
| 238 | + <fieldset> | |
| 239 | + <input type="text" name="selector" class="regular-text" value="<?php echo esc_attr( $data['selector'] ); ?>" /> | |
| 240 | + <p class="description"><?php _e( 'It is the selector which contain the content which you want to copy.<br/>Default its <pre> html tag.', 'copy-the-code' ); ?></p> | |
| 241 | + </fieldset> | |
| 242 | + </td> | |
| 243 | + </tr> | |
| 244 | + <tr> | |
| 245 | + <th scope="row"><?php _e( 'Copy Content As', 'copy-the-code' ); ?></th> | |
| 246 | + <td> | |
| 247 | + <fieldset> | |
| 248 | + <select name="copy-as"> | |
| 249 | + <option value="html" <?php selected( $data['copy-as'], 'html' ); ?>><?php echo 'HTML'; ?></option> | |
| 250 | + <option value="text" <?php selected( $data['copy-as'], 'text' ); ?>><?php echo 'Text'; ?></option> | |
| 251 | + </select> | |
| 252 | + <p class="description"><?php _e( 'Copy the content as Text or HTML.', 'copy-the-code' ); ?></p> | |
| 253 | + </fieldset> | |
| 254 | + </td> | |
| 255 | + </tr> | |
| 256 | + </table> | |
| 257 | + </div> | |
| 258 | + <div id="tab-style" style="padding: 30px; display: none;"> | |
| 259 | + <table class="form-table"> | |
| 260 | + <tr> | |
| 261 | + <th scope="row"><?php _e( 'Button Text', 'copy-the-code' ); ?></th> | |
| 262 | + <td> | |
| 263 | + <fieldset> | |
| 264 | + <input type="text" name="button-text" class="regular-text" value="<?php echo esc_attr( $data['button-text'] ); ?>" /> | |
| 265 | + <p class="description"><?php _e( 'Copy button text. Default \'Copy\'.', 'copy-the-code' ); ?></p> | |
| 266 | + </fieldset> | |
| 267 | + </td> | |
| 268 | + </tr> | |
| 269 | + <tr> | |
| 270 | + <th scope="row"><?php _e( 'Button Copy Text', 'copy-the-code' ); ?></th> | |
| 271 | + <td> | |
| 272 | + <fieldset> | |
| 273 | + <input type="text" name="button-copy-text" class="regular-text" value="<?php echo esc_attr( $data['button-copy-text'] ); ?>" /> | |
| 274 | + <p class="description"><?php _e( 'Copy button text which appear after click on it. Default \'Copied!\'.', 'copy-the-code' ); ?></p> | |
| 275 | + </fieldset> | |
| 276 | + </td> | |
| 277 | + </tr> | |
| 278 | + <tr> | |
| 279 | + <th scope="row"><?php _e( 'Button Title', 'copy-the-code' ); ?></th> | |
| 280 | + <td> | |
| 281 | + <fieldset> | |
| 282 | + <input type="text" name="button-title" class="regular-text" value="<?php echo esc_attr( $data['button-title'] ); ?>" /> | |
| 283 | + <p class="description"><?php _e( 'It is showing on hover on the button. Default \'Copy to Clipboard\'.', 'copy-the-code' ); ?></p> | |
| 284 | + </fieldset> | |
| 285 | + </td> | |
| 286 | + </tr> | |
| 287 | + <tr> | |
| 288 | + <th scope="row"><?php _e( 'Button Position', 'copy-the-code' ); ?></th> | |
| 289 | + <td> | |
| 290 | + <fieldset> | |
| 291 | + <select name="button-position"> | |
| 292 | + <option value="inside" <?php selected( $data['button-position'], 'inside' ); ?>><?php echo 'Inside'; ?></option> | |
| 293 | + <option value="outside" <?php selected( $data['button-position'], 'outside' ); ?>><?php echo 'Outside'; ?></option> | |
| 294 | + </select> | |
| 295 | + <p class="description"><?php _e( 'Button Position Inside/Outside. Default Inside.', 'copy-the-code' ); ?></p> | |
| 296 | + </fieldset> | |
| 297 | + </td> | |
| 298 | + </tr> | |
| 299 | + </table> | |
| 300 | + </div> | |
| 301 | + </div> | |
| 302 | + | |
| 303 | + <input type="hidden" name="message" value="saved" /> | |
| 304 | + <?php wp_nonce_field( 'copy-the-code-nonce', 'copy-the-code' ); ?> | |
| 305 | + | |
| 306 | + <?php submit_button(); ?> | |
| 307 | + | |
| 308 | + </form> | |
| 309 | + | |
| 310 | + </div> | |
| 311 | + | |
| 312 | + <div class="postbox-container" id="postbox-container-1"> | |
| 313 | + <div id="side-sortables" style=""> | |
| 314 | + <div class="postbox"> | |
| 315 | + <h2 class="hndle"><span><?php _e( 'Getting Started', 'copy-the-code' ); ?></span></h2> | |
| 316 | + <div class="inside"> | |
| 317 | + <ul class="items"> | |
| 318 | + <li>» <a style="text-decoration: none;" target="_blank" href="https://maheshwaghmare.com/doc/copy-anything-to-clipboard/#how-does-it-work"><?php esc_html_e( 'How does it work?', 'copy-the-code' ); ?></a></li> | |
| 319 | + <li>» <a style="text-decoration: none;" target="_blank" href="https://maheshwaghmare.com/doc/copy-anything-to-clipboard/#what-is-the-selector"><?php esc_html_e( 'What is the selector?', 'copy-the-code' ); ?></a></li> | |
| 320 | + <li>» <a style="text-decoration: none;" target="_blank" href="https://maheshwaghmare.com/doc/copy-anything-to-clipboard/#example-1-using-html-tag-as-a-selector"><?php esc_html_e( 'Example 1 - Using HTML tag as a selector', 'copy-the-code' ); ?></a></li> | |
| 321 | + <li>» <a style="text-decoration: none;" target="_blank" href="https://maheshwaghmare.com/doc/copy-anything-to-clipboard/#example-2-using-class-as-a-selector"><?php esc_html_e( 'Example 2 - Using class as a selector', 'copy-the-code' ); ?></a></li> | |
| 322 | + <li>» <a style="text-decoration: none;" target="_blank" href="https://maheshwaghmare.com/doc/copy-anything-to-clipboard/#example-3-using-id-as-a-selector"><?php esc_html_e( 'Example 3 - Using ID as a selector', 'copy-the-code' ); ?></a></li> | |
| 323 | + <li>» <a style="text-decoration: none;" target="_blank" href="https://maheshwaghmare.com/doc/copy-anything-to-clipboard/#example-4-using-nested-selector"><?php esc_html_e( 'Example 4 - Using nested selector', 'copy-the-code' ); ?></a></li> | |
| 324 | + <li>» <a style="text-decoration: none;" target="_blank" href="https://maheshwaghmare.com/doc/copy-anything-to-clipboard/#example-5-copy-content-as-html-as-text"><?php esc_html_e( 'Example 5 - Copy content as HTML as Text', 'copy-the-code' ); ?></a></li> | |
| 325 | + </ul> | |
| 326 | + </div> | |
| 327 | + </div> | |
| 328 | + | |
| 329 | + <div class="postbox"> | |
| 330 | + <h2 class="hndle"><span><?php _e( 'Support', 'copy-the-code' ); ?></span></h2> | |
| 331 | + <div class="inside"> | |
| 332 | + <p><?php _e( 'Do you have any issue with this plugin? Or Do you have any suggessions?', 'copy-the-code' ); ?></p> | |
| 333 | + <p><?php _e( 'Please don\'t hesitate to <a href="http://maheshwaghmare.wordpress.com/?p=999" target="_blank">send request »</a>.', 'copy-the-code' ); ?></p> | |
| 334 | + </div> | |
| 335 | + </div> | |
| 336 | + | |
| 337 | + <?php | |
| 338 | + $response = wp_dev_remote_request_get( 'https://maheshwaghmare.com/wp-json/wp/v2/posts/?_fields=id,title,link&per_page=5' ); | |
| 339 | + if( $response['success'] ) { | |
| 340 | + ?> | |
| 341 | + <div class="postbox"> | |
| 342 | + <h2 class="hndle"><span><?php _e( 'Latest News', 'copy-the-code' ); ?></span></h2> | |
| 343 | + <div class="inside"> | |
| 344 | + <ul> | |
| 345 | + <?php foreach ($response['data'] as $key => $item) { ?> | |
| 346 | + <li data-id="<?php echo esc_attr( $item['id'] ); ?>"> | |
| 347 | + » <a style="text-decoration: none;" href="<?php echo esc_attr( $item['link'] ); ?>?utm_source=copy-the-code&utm_medium=plugin&utm_campaign=wp.org" target="_blank"><?php echo esc_html( $item['title']['rendered'] ); ?> | |
| 348 | + </a> | |
| 349 | + </li> | |
| 350 | + <?php } ?> | |
| 351 | + </ul> | |
| 352 | + <p><a href="https://maheshwaghmare.com/blog/?utm_source=copy-the-code&utm_medium=plugin&utm_campaign=wp.org" target="_blank"><?php esc_html_e( 'Read More »', 'copy-the-code' ); ?></a></p> | |
| 353 | + </div> | |
| 354 | + </div> | |
| 355 | + <?php } ?> | |
| 356 | + | |
| 357 | + <div class="postbox"> | |
| 358 | + <h2 class="hndle"><span><?php _e( 'Donate', 'copy-the-code' ); ?></span></h2> | |
| 359 | + <div class="inside"> | |
| 360 | + <p><?php _e( 'Would you like to support the advancement of this plugin?', 'copy-the-code' ); ?></p> | |
| 361 | + <a href="https://www.paypal.me/mwaghmare7/" target="_blank" class="button button-primary"><?php _e( 'Donate Now!', 'copy-the-code' ); ?></a> | |
| 362 | + </div> | |
| 363 | + </div> | |
| 364 | + </div> | |
| 365 | + </div> | |
| 366 | + </div> | |
| 367 | + </div> | |
| 368 | + </div> | |
| 369 | + </div> | |
| 370 | + <?php | |
| 371 | + } | |
| 372 | + } | |
| 373 | + | |
| 374 | + /** | |
| 375 | + * Initialize class object with 'get_instance()' method | |
| 376 | + */ | |
| 377 | + Copy_The_Code_Page::get_instance(); | |
| 378 | + | |
| 379 | +endif; | |