| 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', array( $this, 'admin_url' ), 10, 3 ); |
| 61 |
add_action( 'plugin_action_links_' . COPY_THE_CODE_BASE, array( $this, 'action_links' ) ); |
| 62 |
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) ); |
| 63 |
add_action( 'init', array( $this, 'register_post_type' ) ); |
| 64 |
add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) ); |
| 65 |
add_action( 'manage_copy-to-clipboard_posts_custom_column', array( $this, 'column_markup' ), 10, 2 ); |
| 66 |
add_action( 'manage_copy-to-clipboard_posts_columns', array( $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 |
__( '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 |
* Add custom column |
| 141 |
* |
| 142 |
* @param array $columns Columns. |
| 143 |
* @since 2.0.0 |
| 144 |
*/ |
| 145 |
function add_column( $columns = array() ) { |
| 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 = array( |
| 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 = array( |
| 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 = array( |
| 273 |
'labels' => $labels, |
| 274 |
'hierarchical' => false, |
| 275 |
'description' => 'description', |
| 276 |
'taxonomies' => array(), |
| 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 |
function enqueue_assets() { |
| 307 |
wp_enqueue_style( 'copy-the-code', COPY_THE_CODE_URI . 'assets/css/copy-the-code.css', null, COPY_THE_CODE_VER, 'all' ); |
| 308 |
wp_enqueue_script( 'copy-the-code', COPY_THE_CODE_URI . 'assets/js/copy-the-code.js', array( 'jquery' ), COPY_THE_CODE_VER, true ); |
| 309 |
wp_localize_script( |
| 310 |
'copy-the-code', |
| 311 |
'copyTheCode', |
| 312 |
$this->get_localize_vars() |
| 313 |
); |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Localize Vars |
| 318 |
* |
| 319 |
* @return array |
| 320 |
*/ |
| 321 |
function get_localize_vars() { |
| 322 |
|
| 323 |
$query_args = array( |
| 324 |
'post_type' => 'copy-to-clipboard', |
| 325 |
|
| 326 |
// Query performance optimization. |
| 327 |
'fields' => 'ids', |
| 328 |
'no_found_rows' => true, |
| 329 |
'posts_per_page' => -1, |
| 330 |
); |
| 331 |
|
| 332 |
$query = new WP_Query( $query_args ); |
| 333 |
$selectors = array(); |
| 334 |
if ( $query->posts ) { |
| 335 |
foreach ( $query->posts as $key => $post_id ) { |
| 336 |
$selectors[] = array( |
| 337 |
'selector' => get_post_meta( $post_id, 'selector', true ), |
| 338 |
'style' => get_post_meta( $post_id, 'style', true ), |
| 339 |
// '/ $copy_as' => get_post_meta( $post_id, 'copy-as', true ), |
| 340 |
'button_text' => get_post_meta( $post_id, 'button-text', true ), |
| 341 |
'button_title' => get_post_meta( $post_id, 'button-title', true ), |
| 342 |
'button_copy_text' => get_post_meta( $post_id, 'button-copy-text', true ), |
| 343 |
'button_position' => get_post_meta( $post_id, 'button-position', true ), |
| 344 |
'copy_format' => get_post_meta( $post_id, 'copy-format', true ), |
| 345 |
); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
return apply_filters( |
| 350 |
'copy_the_code_localize_vars', |
| 351 |
array( |
| 352 |
'trim_lines' => false, |
| 353 |
'remove_spaces' => true, |
| 354 |
'copy_content_as' => '', |
| 355 |
'previewMarkup' => '<h2>Hello World</h2>', |
| 356 |
'buttonMarkup' => '<button class="copy-the-code-button" title=""></button>', |
| 357 |
'buttonSvg' => Helpers::get_svg_copy_icon(), |
| 358 |
'selectors' => $selectors, |
| 359 |
'selector' => 'pre', // Selector in which have the actual `<code>`. |
| 360 |
'settings' => $this->get_page_settings(), |
| 361 |
'string' => array( |
| 362 |
'title' => $this->get_page_setting( 'button-title', __( 'Copy to Clipboard', 'copy-the-code' ) ), |
| 363 |
'copy' => $this->get_page_setting( 'button-text', __( 'Copy', 'copy-the-code' ) ), |
| 364 |
'copied' => $this->get_page_setting( 'button-copy-text', __( 'Copied!', 'copy-the-code' ) ), |
| 365 |
), |
| 366 |
'image-url' => COPY_THE_CODE_URI . '/assets/images/copy-1.svg', |
| 367 |
'redirect_url' => '', |
| 368 |
) |
| 369 |
); |
| 370 |
} |
| 371 |
|
| 372 |
/** |
| 373 |
* Get Setting |
| 374 |
* |
| 375 |
* @param string $key Setting key. |
| 376 |
* @param string $default_value Setting default value. |
| 377 |
* @return mixed Single Setting. |
| 378 |
*/ |
| 379 |
function get_page_setting( $key = '', $default_value = '' ) { |
| 380 |
$settings = $this->get_page_settings(); |
| 381 |
|
| 382 |
if ( array_key_exists( $key, $settings ) ) { |
| 383 |
return $settings[ $key ]; |
| 384 |
} |
| 385 |
|
| 386 |
return $default_value; |
| 387 |
} |
| 388 |
|
| 389 |
/** |
| 390 |
* Settings |
| 391 |
* |
| 392 |
* @return array Settings. |
| 393 |
*/ |
| 394 |
function get_page_settings() { |
| 395 |
$defaults = apply_filters( |
| 396 |
'copy_the_code_default_page_settings', |
| 397 |
array( |
| 398 |
'selector' => 'pre', |
| 399 |
// 'copy-as' => 'text', |
| 400 |
'button-text' => __( 'Copy', 'copy-the-code' ), |
| 401 |
'button-title' => __( 'Copy to Clipboard', 'copy-the-code' ), |
| 402 |
'button-copy-text' => __( 'Copied!', 'copy-the-code' ), |
| 403 |
'button-position' => 'inside', |
| 404 |
'copy-format' => 'default', |
| 405 |
) |
| 406 |
); |
| 407 |
|
| 408 |
$stored = get_option( 'copy-the-code-settings', $defaults ); |
| 409 |
|
| 410 |
return apply_filters( 'copy_the_code_page_settings', wp_parse_args( $stored, $defaults ) ); |
| 411 |
} |
| 412 |
|
| 413 |
/** |
| 414 |
* Show action links on the plugin screen. |
| 415 |
* |
| 416 |
* @param mixed $links Plugin Action links. |
| 417 |
* @return array |
| 418 |
*/ |
| 419 |
function action_links( $links ) { |
| 420 |
$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>', |
| 422 |
); |
| 423 |
|
| 424 |
return array_merge( $action_links, $links ); |
| 425 |
} |
| 426 |
|
| 427 |
} |
| 428 |
|
| 429 |
/** |
| 430 |
* Initialize class object with 'get_instance()' method |
| 431 |
*/ |
| 432 |
Copy_The_Code_Page::get_instance(); |
| 433 |
|
| 434 |
endif; |
| 435 |
|