PluginProbe
Copy Anything to Clipboard for WordPress – Copy Button, Copy Text & Copy Code / 2.0.0
Copy Anything to Clipboard for WordPress – Copy Button, Copy Text & Copy Code v2.0.0
5.5.3 3.1.0 3.2.0 3.2.1 3.3.0 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.6.0 3.7.0 3.8.0 3.8.1 3.8.2 3.8.3 4.0.0 4.0.2 4.0.3 4.0.4 4.0.5 4.1.0 4.1.1 All 78 releases
copy-the-code / classes / class-copy-the-code-page.php

class-copy-the-code-page.php in Copy Anything to Clipboard for WordPress – Copy Button, Copy Text & Copy Code 2.0.0, at classes/class-copy-the-code-page.php

508 lines 15.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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_filter( 'admin_url', array( $this, 'admin_url' ), 10, 3 );
49 add_action( 'plugin_action_links_' . COPY_THE_CODE_BASE, array( $this, 'action_links' ) );
50 add_action( 'init', array( $this, 'save_settings' ), 11 );
51 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) );
52 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_assets' ) );
53 add_action( 'init', array( $this, 'register_post_type' ) );
54 add_action( 'after_setup_theme', array( $this, 'init_admin_settings' ) );
55 add_action( 'manage_copy-to-clipboard_posts_custom_column', array( $this, 'column_markup' ), 10, 2 );
56 add_action( 'manage_copy-to-clipboard_posts_columns', array( $this, 'add_column' ), 10 );
57 }
58
59 /**
60 * Add custom column
61 *
62 * @param array $columns Columns.
63 * @since 2.0.0
64 */
65 function add_column( $columns = array() ) {
66
67 if ( isset( $columns['author'] ) ) {
68 unset( $columns['author'] );
69 }
70
71 if ( isset( $columns['date'] ) ) {
72 unset( $columns['date'] );
73 }
74
75 $new_columns = array(
76 'style' => __( 'Style', 'copy-the-code' ),
77 'settings' => __( 'Settings', 'copy-the-code' ),
78 'author' => 'Author',
79 'date' => 'Date',
80 );
81
82 return wp_parse_args( $new_columns, $columns );
83 }
84
85 /**
86 * Column markup
87 *
88 * @since 2.0.0
89 *
90 * @param string $column_name Column slug.
91 * @param integer $post_id Post ID.
92 * @return void
93 */
94 function column_markup( $column_name = '', $post_id = 0 ) {
95
96 if ( 'style' === $column_name ) {
97 $style = get_post_meta( $post_id, 'style', true );
98 switch ( $style ) {
99 case 'cover':
100 echo 'Cover';
101 break;
102 case 'svg-icon':
103 echo 'SVG Icon';
104 break;
105 case 'button':
106 echo 'Button';
107 break;
108 }
109 }
110 if ( 'settings' === $column_name ) {
111 $button_text = get_post_meta( $post_id, 'button-text', true );
112 if ( ! empty( $button_text ) ) {
113 echo '<i>Button Text: </i><b>' . $button_text . '</b><br/>';
114 }
115 $button_title = get_post_meta( $post_id, 'button-title', true );
116 if ( ! empty( $button_title ) ) {
117 echo '<i>Button Title: </i><b>' . $button_title . '</b><br/>';
118 }
119 $button_copy_text = get_post_meta( $post_id, 'button-copy-text', true );
120 if ( ! empty( $button_copy_text ) ) {
121 echo '<i>Button Copy Text: </i><b>' . $button_copy_text . '</b><br/>';
122 }
123 $button_position = get_post_meta( $post_id, 'button-position', true );
124 if ( ! empty( $button_position ) ) {
125 echo '<i>Button Position: </i><b>' . $button_position . '</b><br/>';
126 }
127 }
128
129 }
130
131 /**
132 * Filters the admin area URL.
133 *
134 * @since 1.0.2
135 *
136 * @param string $url The complete admin area URL including scheme and path.
137 * @param string $path Path relative to the admin area URL. Blank string if no path is specified.
138 * @param int|null $blog_id Site ID, or null for the current site.
139 */
140 public function admin_url( $url, $path, $blog_id ) {
141
142 if ( 'post-new.php?post_type=copy-to-clipboard' !== $path ) {
143 return $url;
144 }
145
146 $url = get_site_url( $blog_id, 'wp-admin/', 'admin' );
147 $path = 'edit.php?post_type=copy-to-clipboard&page=copy-to-clipboard-add-new';
148
149 if ( $path && is_string( $path ) ) {
150 $url .= ltrim( $path, '/' );
151 }
152
153 return $url;
154 }
155
156 /**
157 * Register menus
158 *
159 * @since 2.0.0
160 * @return void
161 */
162 function init_admin_settings() {
163 if ( current_user_can( 'edit_posts' ) ) {
164 add_action( 'admin_menu', array( $this, 'register' ) );
165 add_action( 'submenu_file', array( $this, 'submenu_file' ), 999, 2 );
166 }
167 }
168
169 /**
170 * Sets the active menu item for the builder admin submenu.
171 *
172 * @since 2.0.0
173 *
174 * @param string $submenu_file Submenu file.
175 * @param string $parent_file Parent file.
176 * @return string Submenu file.
177 */
178 public function submenu_file( $submenu_file, $parent_file ) {
179 global $pagenow;
180
181 $screen = get_current_screen();
182
183 if ( isset( $_GET['page'] ) && 'copy-to-clipboard-add-new' === $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
184 $submenu_file = 'copy-to-clipboard-add-new';
185 } elseif ( 'post.php' === $pagenow && 'copy-to-clipboard' === $screen->post_type ) {
186 $submenu_file = 'edit.php?post_type=copy-to-clipboard';
187 }
188
189 return $submenu_file;
190 }
191
192 /**
193 * Registers the add new portfolio form admin menu for adding portfolios.
194 *
195 * @since 2.0.0
196 *
197 * @return void
198 */
199 public function register() {
200 global $submenu, $_registered_pages;
201
202 $parent = 'edit.php?post_type=copy-to-clipboard';
203 $add_new_hook = 'copy-to-clipboard_page_copy-to-clipboard-add-new';
204
205 $submenu[ $parent ] = array(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
206 $submenu[ $parent ][10] = array( __( 'All Items', 'copy-the-code' ), 'edit_posts', $parent ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
207 $submenu[ $parent ][20] = array( __( 'Add New', 'copy-the-code' ), 'edit_posts', 'copy-to-clipboard-add-new', '' ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
208 add_action( $add_new_hook, array( $this, 'add_new_page' ) );
209 $_registered_pages[ $add_new_hook ] = true; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
210 }
211
212 /**
213 * Add new page
214 *
215 * @since 2.0.0
216 */
217 public function add_new_page() {
218 $data = $this->get_page_settings();
219 require_once COPY_THE_CODE_DIR . 'includes/add-new-form.php';
220 }
221
222 /**
223 * Registers a new post type
224 *
225 * @since 2.0.0
226 */
227 function register_post_type() {
228
229 $labels = array(
230 'name' => __( 'Copy to Clipboard', 'copy-the-code' ),
231 'singular_name' => __( 'Copy to Clipboard', 'copy-the-code' ),
232 'add_new' => _x( 'Add New', 'copy-the-code', 'copy-the-code' ),
233 'add_new_item' => __( 'Add New', 'copy-the-code' ),
234 'edit_item' => __( 'Edit Copy to Clipboard', 'copy-the-code' ),
235 'new_item' => __( 'New Copy to Clipboard', 'copy-the-code' ),
236 'view_item' => __( 'View Copy to Clipboard', 'copy-the-code' ),
237 'search_items' => __( 'Search Copy to Clipboard', 'copy-the-code' ),
238 'not_found' => __( 'No Copy to Clipboard found', 'copy-the-code' ),
239 'not_found_in_trash' => __( 'No Copy to Clipboard found in Trash', 'copy-the-code' ),
240 'parent_item_colon' => __( 'Parent Copy to Clipboard:', 'copy-the-code' ),
241 'menu_name' => __( 'Copy to Clipboard', 'copy-the-code' ),
242 );
243
244 $args = array(
245 'labels' => $labels,
246 'hierarchical' => false,
247 'description' => 'description',
248 'taxonomies' => array(),
249 'public' => false,
250 'show_ui' => true,
251 'show_in_menu' => true,
252 'show_in_admin_bar' => false,
253 'menu_position' => null,
254 'menu_icon' => 'dashicons-clipboard',
255 'show_in_nav_menus' => true,
256 'publicly_queryable' => false,
257 'exclude_from_search' => false,
258 'has_archive' => false,
259 'query_var' => false,
260 'can_export' => true,
261 'rewrite' => false,
262 'capability_type' => 'post',
263 'supports' => array(
264 'title',
265 'author',
266 'custom-fields',
267 ),
268 );
269
270 register_post_type( 'copy-to-clipboard', $args );
271 }
272
273 /**
274 * Enqueue Assets.
275 *
276 * @version 1.7.0
277 *
278 * @param string $hook Current hook name.
279 * @return mixed
280 */
281 function admin_enqueue_assets( $hook = '' ) {
282
283 if ( 'copy-to-clipboard_page_copy-to-clipboard-add-new' !== $hook ) {
284 return;
285 }
286
287 wp_enqueue_script( 'copy-the-code-page', COPY_THE_CODE_URI . 'assets/js/page.js', array( 'jquery' ), COPY_THE_CODE_VER, true );
288 wp_enqueue_style( 'copy-the-code-page', COPY_THE_CODE_URI . 'assets/css/page.css', null, COPY_THE_CODE_VER, 'all' );
289
290 wp_localize_script(
291 'copy-the-code-page',
292 'copyTheCode',
293 $this->get_localize_vars()
294 );
295 }
296
297 /**
298 * Enqueue Assets.
299 *
300 * @version 1.0.0
301 *
302 * @return void
303 */
304 function enqueue_assets() {
305 wp_enqueue_style( 'copy-the-code', COPY_THE_CODE_URI . 'assets/css/copy-the-code.css', null, COPY_THE_CODE_VER, 'all' );
306 wp_enqueue_script( 'copy-the-code', COPY_THE_CODE_URI . 'assets/js/copy-the-code.js', array( 'jquery' ), COPY_THE_CODE_VER, true );
307 wp_localize_script(
308 'copy-the-code',
309 'copyTheCode',
310 $this->get_localize_vars()
311 );
312 }
313
314 /**
315 * Localize Vars
316 *
317 * @return array
318 */
319 function get_localize_vars() {
320
321 $query_args = array(
322 'post_type' => 'copy-to-clipboard',
323
324 // Query performance optimization.
325 'fields' => 'ids',
326 'no_found_rows' => true,
327 'posts_per_page' => -1,
328 );
329
330 $query = new WP_Query( $query_args );
331 $selectors = array();
332 if ( $query->posts ) {
333 foreach ( $query->posts as $key => $post_id ) {
334 $selectors[] = array(
335 'selector' => get_post_meta( $post_id, 'selector', true ),
336 'style' => get_post_meta( $post_id, 'style', true ),
337 // '/ $copy_as' => get_post_meta( $post_id, 'copy-as', true ),
338 'button_text' => get_post_meta( $post_id, 'button-text', true ),
339 'button_title' => get_post_meta( $post_id, 'button-title', true ),
340 'button_copy_text' => get_post_meta( $post_id, 'button-copy-text', true ),
341 'button_position' => get_post_meta( $post_id, 'button-position', true ),
342 );
343 }
344 }
345
346 return apply_filters(
347 'copy_the_code_localize_vars',
348 array(
349 'previewMarkup' => '&lt;h2&gt;Hello World&lt;/h2&gt;',
350 'buttonMarkup' => '<button class="copy-the-code-button" title=""></button>',
351 '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>',
352 'selectors' => $selectors,
353 'selector' => 'pre', // Selector in which have the actual `<code>`.
354 'settings' => $this->get_page_settings(),
355 'string' => array(
356 'title' => $this->get_page_setting( 'button-title', __( 'Copy to Clipboard', 'copy-the-code' ) ),
357 'copy' => $this->get_page_setting( 'button-text', __( 'Copy', 'copy-the-code' ) ),
358 'copied' => $this->get_page_setting( 'button-copy-text', __( 'Copied!', 'copy-the-code' ) ),
359 ),
360 'image-url' => COPY_THE_CODE_URI . '/assets/images/copy-1.svg',
361 )
362 );
363 }
364
365 /**
366 * Admin Settings
367 *
368 * @return void
369 */
370 function save_settings() {
371
372 if ( ! isset( $_REQUEST['page'] ) || ! isset( $_REQUEST['copy-the-code'] ) ) {
373 return;
374 }
375
376 if ( strpos( $_REQUEST['page'], 'copy-to-clipboard-add-new' ) === false ) {
377 return;
378 }
379
380 // Only admins can save settings.
381 if ( ! current_user_can( 'manage_options' ) ) {
382 return;
383 }
384
385 // Make sure we have a valid nonce.
386 if ( ! wp_verify_nonce( $_REQUEST['copy-the-code'], 'copy-the-code-nonce' ) ) {
387 return;
388 }
389
390 $selector = ( isset( $_REQUEST['selector'] ) ) ? $_REQUEST['selector'] : 'pre';
391
392 $query_args = array(
393 'post_type' => 'copy-to-clipboard',
394
395 // Query performance optimization.
396 'fields' => 'ids',
397 'no_found_rows' => true,
398 'posts_per_page' => -1,
399 'meta_key' => 'selector',
400 'meta_value' => $selector,
401 );
402
403 $query = new WP_Query( $query_args );
404 if ( $query->post_count ) {
405 add_action( 'admin_notices', array( $this, 'selector_exist_notice' ) );
406 return;
407 }
408
409 // New settings.
410 $new_data = array(
411 'selector' => $selector,
412 'style' => ( isset( $_REQUEST['style'] ) ) ? $_REQUEST['style'] : 'button',
413 'button-text' => ( isset( $_REQUEST['button-text'] ) ) ? $_REQUEST['button-text'] : 'Copy',
414 'button-title' => ( isset( $_REQUEST['button-title'] ) ) ? $_REQUEST['button-title'] : 'Copy',
415 'button-copy-text' => ( isset( $_REQUEST['button-copy-text'] ) ) ? $_REQUEST['button-copy-text'] : 'Copied!',
416 'button-position' => ( isset( $_REQUEST['button-position'] ) ) ? $_REQUEST['button-position'] : 'inside',
417 );
418
419 $data = array(
420 'post_type' => 'copy-to-clipboard',
421 'post_status' => 'publish',
422 'post_title' => $new_data['selector'],
423 'meta_input' => $new_data,
424 );
425 wp_insert_post( $data );
426
427 wp_safe_redirect( admin_url( 'edit.php?post_type=copy-to-clipboard' ) );
428
429 exit();
430 }
431
432 /**
433 * Get Setting
434 *
435 * @param string $key Setting key.
436 * @param string $default_value Setting default value.
437 * @return mixed Single Setting.
438 */
439 function get_page_setting( $key = '', $default_value = '' ) {
440 $settings = $this->get_page_settings();
441
442 if ( array_key_exists( $key, $settings ) ) {
443 return $settings[ $key ];
444 }
445
446 return $default_value;
447 }
448
449 /**
450 * Selector is exist notice.
451 *
452 * @since 2.0.0
453 * @return void
454 */
455 function selector_exist_notice() {
456 ?>
457 <div class="notice notice-error">
458 <p><?php esc_html__( 'The selector is already exist! Please try another selector.', 'copy-the-code' ); ?></p>
459 </div>
460 <?php
461 }
462
463 /**
464 * Settings
465 *
466 * @return array Settings.
467 */
468 function get_page_settings() {
469 $defaults = apply_filters(
470 'copy_the_code_default_page_settings',
471 array(
472 'selector' => 'pre',
473 // 'copy-as' => 'text',
474 'button-text' => __( 'Copy', 'copy-the-code' ),
475 'button-title' => __( 'Copy to Clipboard', 'copy-the-code' ),
476 'button-copy-text' => __( 'Copied!', 'copy-the-code' ),
477 'button-position' => 'inside',
478 )
479 );
480
481 $stored = get_option( 'copy-the-code-settings', $defaults );
482
483 return apply_filters( 'copy_the_code_page_settings', wp_parse_args( $stored, $defaults ) );
484 }
485
486 /**
487 * Show action links on the plugin screen.
488 *
489 * @param mixed $links Plugin Action links.
490 * @return array
491 */
492 function action_links( $links ) {
493 $action_links = array(
494 'add-new' => '<a href="' . admin_url( 'edit.php?post_type=copy-to-clipboard&page=copy-to-clipboard-add-new' ) . '" aria-label="' . esc_attr__( 'Add new', 'copy-the-code' ) . '">' . esc_html__( 'Add new', 'copy-the-code' ) . '</a>',
495 );
496
497 return array_merge( $action_links, $links );
498 }
499
500 }
501
502 /**
503 * Initialize class object with 'get_instance()' method
504 */
505 Copy_The_Code_Page::get_instance();
506
507 endif;
508