| 1 |
<?php |
| 2 |
/** |
| 3 |
* Dashboard |
| 4 |
* |
| 5 |
* @package Copy the Code |
| 6 |
* @since 3.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
use CopyTheCode\Elementor\Blocks; |
| 10 |
|
| 11 |
if ( ! class_exists( 'Copy_The_Code_Dashboard' ) ) : |
| 12 |
|
| 13 |
/** |
| 14 |
* Copy The Code Dashboard |
| 15 |
* |
| 16 |
* @since 3.0.0 |
| 17 |
*/ |
| 18 |
class Copy_The_Code_Dashboard { |
| 19 |
|
| 20 |
/** |
| 21 |
* Instance |
| 22 |
* |
| 23 |
* @since 3.0.0 |
| 24 |
* |
| 25 |
* @access private |
| 26 |
* @var object Class object. |
| 27 |
*/ |
| 28 |
private static $instance; |
| 29 |
|
| 30 |
/** |
| 31 |
* Initiator |
| 32 |
* |
| 33 |
* @since 3.0.0 |
| 34 |
* |
| 35 |
* @return object initialized object of class. |
| 36 |
*/ |
| 37 |
public static function get_instance() { |
| 38 |
if ( ! isset( self::$instance ) ) { |
| 39 |
self::$instance = new self(); |
| 40 |
} |
| 41 |
return self::$instance; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Constructor |
| 46 |
* |
| 47 |
* @since 3.0.0 |
| 48 |
*/ |
| 49 |
public function __construct() { |
| 50 |
add_action( 'after_setup_theme', [ $this, 'add_menus' ] ); |
| 51 |
add_action( 'post_row_actions', [ $this, 'post_row_actions' ], 10, 2 ); |
| 52 |
add_action( 'get_edit_post_link', [ $this, 'edit_post_link' ], 10, 3 ); |
| 53 |
add_action( 'wp_ajax_ctc_save_changes', [ $this, 'save' ] ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Save |
| 58 |
* |
| 59 |
* @since 3.0.0 |
| 60 |
* @return void |
| 61 |
*/ |
| 62 |
public function save() { |
| 63 |
$style_type = isset( $_POST['style_type'] ) ? sanitize_text_field( $_POST['style_type'] ) : ''; |
| 64 |
$position = isset( $_POST['position'] ) ? sanitize_text_field( $_POST['position'] ) : ''; |
| 65 |
$format = isset( $_POST['format'] ) ? sanitize_text_field( $_POST['format'] ) : ''; |
| 66 |
$selector = isset( $_POST['selector'] ) ? sanitize_text_field( $_POST['selector'] ) : ''; |
| 67 |
$btn_text = isset( $_POST['btn_text'] ) ? sanitize_text_field( $_POST['btn_text'] ) : ''; |
| 68 |
$btn_after_copy_text = isset( $_POST['btn_after_copy_text'] ) ? sanitize_text_field( $_POST['btn_after_copy_text'] ) : ''; |
| 69 |
$btn_title = isset( $_POST['btn_title'] ) ? sanitize_text_field( $_POST['btn_title'] ) : ''; |
| 70 |
$post = isset( $_POST['post'] ) ? $_POST['post'] : []; |
| 71 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : ''; |
| 72 |
$on_edit = isset( $_POST['on_edit'] ) ? sanitize_text_field( $_POST['on_edit'] ) : ''; |
| 73 |
$conditions = isset( $_POST['conditions'] ) ? $_POST['conditions'] : []; |
| 74 |
if ( ! $conditions ) { |
| 75 |
$conditions = array_map( |
| 76 |
function( $condition ) { |
| 77 |
return [ |
| 78 |
'id' => sanitize_text_field( $condition['id'] ), |
| 79 |
'operator' => sanitize_text_field( $condition['operator'] ), |
| 80 |
'type' => sanitize_text_field( $condition['type'] ), |
| 81 |
'value' => sanitize_text_field( $condition['value'] ), |
| 82 |
]; |
| 83 |
}, |
| 84 |
$conditions |
| 85 |
); |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
if ( ! wp_verify_nonce( $nonce, 'copy-the-code' ) ) { |
| 90 |
wp_send_json_error( [ 'message' => esc_html__( 'Invalid nonce', 'copy-the-code' ) ] ); |
| 91 |
} |
| 92 |
|
| 93 |
if ( ! current_user_can( 'edit_posts' ) ) { |
| 94 |
wp_send_json_error( [ 'message' => esc_html__( 'You do not have permission to edit posts', 'copy-the-code' ) ] ); |
| 95 |
} |
| 96 |
|
| 97 |
$post_title = isset( $post['post_title'] ) ? sanitize_text_field( $post['post_title'] ) : ''; |
| 98 |
|
| 99 |
if ( $on_edit ) { |
| 100 |
$post_id = isset( $post['ID'] ) ? absint( $post['ID'] ) : 0; |
| 101 |
|
| 102 |
wp_update_post( |
| 103 |
[ |
| 104 |
'ID' => $post_id, |
| 105 |
'post_title' => $post_title, |
| 106 |
] |
| 107 |
); |
| 108 |
} else { |
| 109 |
$post = [ |
| 110 |
'post_title' => $post_title, |
| 111 |
'post_type' => 'copy-to-clipboard', |
| 112 |
'post_status' => 'publish', |
| 113 |
]; |
| 114 |
|
| 115 |
$post_id = wp_insert_post( $post ); |
| 116 |
} |
| 117 |
|
| 118 |
$meta = [ |
| 119 |
'button-text' => $btn_text, |
| 120 |
'button-copy-text' => $btn_after_copy_text, |
| 121 |
'button-title' => $btn_title, |
| 122 |
'copy-format' => $format, |
| 123 |
'selector' => $selector, |
| 124 |
'button-position' => $position, |
| 125 |
'style' => $style_type, |
| 126 |
'conditions' => $conditions, |
| 127 |
]; |
| 128 |
|
| 129 |
foreach ( $meta as $key => $value ) { |
| 130 |
update_post_meta( $post_id, $key, $value ); |
| 131 |
} |
| 132 |
|
| 133 |
do_action( 'copy_the_code/dashboard/save' ); |
| 134 |
|
| 135 |
wp_send_json_success( |
| 136 |
[ |
| 137 |
'message' => esc_html__( 'Saved', 'copy-the-code' ), |
| 138 |
'edit_post_url' => get_edit_post_link( $post_id ), |
| 139 |
] |
| 140 |
); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Filters the post edit link. |
| 145 |
* |
| 146 |
* @since 3.0.0 |
| 147 |
* |
| 148 |
* @param string $link The edit link. |
| 149 |
* @param int $post_id Post ID. |
| 150 |
* @param string $context The link context. If set to 'display' then ampersands |
| 151 |
* are encoded. |
| 152 |
*/ |
| 153 |
public function edit_post_link( $link, $post_id, $context ) { |
| 154 |
global $mode; |
| 155 |
|
| 156 |
if ( 'list' !== $mode ) { |
| 157 |
return $link; |
| 158 |
} |
| 159 |
|
| 160 |
$post_type = get_post_type( $post_id ); |
| 161 |
if ( 'copy-to-clipboard' !== $post_type ) { |
| 162 |
return $link; |
| 163 |
} |
| 164 |
|
| 165 |
return esc_url( admin_url( 'options-general.php?page=copy-the-code&id=' . $post_id ) ); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Post row actions |
| 170 |
* |
| 171 |
* @since 3.0.0 |
| 172 |
* @return array |
| 173 |
*/ |
| 174 |
public function post_row_actions( $actions, $post ) { |
| 175 |
if ( 'copy-to-clipboard' !== $post->post_type ) { |
| 176 |
return $actions; |
| 177 |
} |
| 178 |
|
| 179 |
$actions['edit'] = sprintf( |
| 180 |
'<a href="%s">%s</a>', |
| 181 |
esc_url( admin_url( 'options-general.php?page=copy-the-code&id=' . $post->ID ) ), |
| 182 |
__( 'Edit', 'copy-the-code' ) |
| 183 |
); |
| 184 |
|
| 185 |
return $actions; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Add menus |
| 190 |
* |
| 191 |
* @since 3.0.0 |
| 192 |
* @return void |
| 193 |
*/ |
| 194 |
public function add_menus() { |
| 195 |
if ( ! current_user_can( 'edit_posts' ) ) { |
| 196 |
return; |
| 197 |
} |
| 198 |
|
| 199 |
add_action( 'admin_menu', [ $this, 'register' ] ); |
| 200 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); |
| 201 |
add_action( 'admin_footer', [ $this, 'hide_menus' ] ); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Hide menus |
| 206 |
* |
| 207 |
* @since 3.0.0 |
| 208 |
* @return void |
| 209 |
*/ |
| 210 |
function hide_menus() { |
| 211 |
?> |
| 212 |
<style type="text/css"> |
| 213 |
#adminmenu a[href="options-general.php?page=copy-the-code"], |
| 214 |
#adminmenu a[href="options-general.php?page=copy-the-code-contact"], |
| 215 |
#adminmenu a[href="options-general.php?page=copy-the-code-wp-support-forum"], |
| 216 |
#adminmenu a[href="options-general.php?page=copy-the-code-pricing"], |
| 217 |
#adminmenu a[href="options-general.php?page=copy-the-code-account"] { |
| 218 |
display: none !important; |
| 219 |
} |
| 220 |
</style> |
| 221 |
<?php |
| 222 |
|
| 223 |
if ( ! isset( $_GET['post_type'] ) ) { |
| 224 |
return; |
| 225 |
} |
| 226 |
|
| 227 |
if ( 'copy-to-clipboard' !== $_GET['post_type'] ) { |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
wp_enqueue_script( 'jquery' ); |
| 232 |
|
| 233 |
$menus = [ |
| 234 |
'copy-the-code-wp-support-forum' => 'Support Forum', |
| 235 |
]; |
| 236 |
|
| 237 |
if ( ctc_fs()->is_not_paying() ) { |
| 238 |
$menus['copy-the-code-pricing'] = 'Upgrade'; |
| 239 |
} else { |
| 240 |
$menus['copy-the-code-account'] = 'Account'; |
| 241 |
} |
| 242 |
|
| 243 |
$menus = array_merge( |
| 244 |
$menus, |
| 245 |
[ |
| 246 |
'copy-the-code-contact' => 'Contact Us', |
| 247 |
'copy-the-code' => 'Dashboard', |
| 248 |
] |
| 249 |
); |
| 250 |
|
| 251 |
?> |
| 252 |
<script> |
| 253 |
// Add button tag after class .page-title-action. |
| 254 |
jQuery( document ).ready( function( $ ) { |
| 255 |
let menus = <?php echo wp_json_encode( $menus ); ?>; |
| 256 |
$.each( menus, function( key, value ) { |
| 257 |
let target = ''; |
| 258 |
if( 'copy-the-code-wp-support-forum' === key ) { |
| 259 |
target = 'target="_blank"'; |
| 260 |
} |
| 261 |
$( '.wrap > .page-title-action' ).after( '<a ' + target + ' class="cta-sub-menu cta-sub-menu-'+key+'" href="<?php echo admin_url( 'options-general.php?page=' ); ?>' + key + '">' + value + '</a>' ); |
| 262 |
} ); |
| 263 |
} ); |
| 264 |
</script> |
| 265 |
<style> |
| 266 |
.cta-sub-menu.cta-sub-menu-copy-the-code-wp-support-forum:after { |
| 267 |
content: "\f504"; |
| 268 |
font-family: dashicons; |
| 269 |
display: inline-block; |
| 270 |
line-height: 1; |
| 271 |
font-weight: 400; |
| 272 |
font-style: normal; |
| 273 |
speak: never; |
| 274 |
text-decoration: inherit; |
| 275 |
text-transform: none; |
| 276 |
text-rendering: auto; |
| 277 |
-webkit-font-smoothing: antialiased; |
| 278 |
-moz-osx-font-smoothing: grayscale; |
| 279 |
width: 20px; |
| 280 |
height: 20px; |
| 281 |
font-size: 20px; |
| 282 |
vertical-align: top; |
| 283 |
text-align: center; |
| 284 |
transition: color 0.1s ease-in; |
| 285 |
text-decoration: none; |
| 286 |
font-size: 14px; |
| 287 |
vertical-align: sub; |
| 288 |
} |
| 289 |
.wrap .page-title-action { |
| 290 |
margin-right: 10px; |
| 291 |
} |
| 292 |
.cta-sub-menu { |
| 293 |
padding: 0 5px; |
| 294 |
display: inline-block; |
| 295 |
margin: 0; |
| 296 |
text-decoration: underline; |
| 297 |
top: -3px; |
| 298 |
position: relative; |
| 299 |
} |
| 300 |
</style> |
| 301 |
<?php |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* Enqueue scripts |
| 306 |
* |
| 307 |
* @since 3.0.0 |
| 308 |
* @return void |
| 309 |
*/ |
| 310 |
public function enqueue_scripts( $hook = '' ) { |
| 311 |
if ( 'settings_page_copy-the-code' !== $hook ) { |
| 312 |
return; |
| 313 |
} |
| 314 |
|
| 315 |
wp_enqueue_script( 'copy-the-code-dashboard', COPY_THE_CODE_URI . 'assets/admin/js/dashboard.js', [ 'jquery', 'wp-element', 'wp-hooks', 'lodash', 'wp-api-fetch' ], COPY_THE_CODE_VER, true ); |
| 316 |
wp_enqueue_style( 'copy-the-code-dashboard', COPY_THE_CODE_URI . 'assets/admin/css/dashboard.css', null, COPY_THE_CODE_VER, 'all' ); |
| 317 |
$id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : 0; |
| 318 |
|
| 319 |
$conditions = get_post_meta( $id, 'conditions', true ); |
| 320 |
if ( ! is_array( $conditions ) ) { |
| 321 |
$conditions = []; |
| 322 |
} |
| 323 |
|
| 324 |
wp_localize_script( |
| 325 |
'copy-the-code-dashboard', |
| 326 |
'CopyDashboardVars', |
| 327 |
[ |
| 328 |
'uri' => COPY_THE_CODE_URI, |
| 329 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 330 |
'nonce' => wp_create_nonce( 'copy-the-code' ), |
| 331 |
'onEdit' => $id ? true : false, |
| 332 |
'editUrl' => $id ? get_edit_post_link( $id ) : admin_url( 'edit.php?post_type=copy-to-clipboard' ), |
| 333 |
'post' => $id ? get_post( $id ) : [], |
| 334 |
'meta' => $id ? [ |
| 335 |
'button-text' => get_post_meta( $id, 'button-text', true ), |
| 336 |
'button-copy-text' => get_post_meta( $id, 'button-copy-text', true ), |
| 337 |
'button-title' => get_post_meta( $id, 'button-title', true ), |
| 338 |
'copy-format' => get_post_meta( $id, 'copy-format', true ), |
| 339 |
'button-position' => get_post_meta( $id, 'button-position', true ), |
| 340 |
'selector' => get_post_meta( $id, 'selector', true ), |
| 341 |
'style' => get_post_meta( $id, 'style', true ), |
| 342 |
'conditions' => $conditions, |
| 343 |
] : [ |
| 344 |
'button-text' => 'Copy to Clipboard', |
| 345 |
'button-copy-text' => 'Copied', |
| 346 |
'button-title' => 'Copy to Clipboard', |
| 347 |
'copy-format' => '', |
| 348 |
'button-position' => 'outside', |
| 349 |
'selector' => 'pre', |
| 350 |
'style' => 'button', |
| 351 |
'conditions' => $conditions, |
| 352 |
], |
| 353 |
'upgradeUrl' => admin_url( 'options-general.php?billing_cycle=annual&page=copy-the-code-pricing' ), |
| 354 |
'buttonStyle' => get_option( |
| 355 |
'ctc_default_style', |
| 356 |
[ |
| 357 |
'btn_color' => '#424242', |
| 358 |
'btn_bg_color' => '#e1e3e8', |
| 359 |
'btn_l_padding' => '20', |
| 360 |
'btn_t_padding' => '10', |
| 361 |
'btn_r_padding' => '20', |
| 362 |
'btn_b_padding' => '10', |
| 363 |
'btn_l_margin' => '0', |
| 364 |
'btn_t_margin' => '0', |
| 365 |
'btn_r_margin' => '0', |
| 366 |
'btn_b_margin' => '0', |
| 367 |
'btn_tl_radius' => '0', |
| 368 |
'btn_tr_radius' => '0', |
| 369 |
'btn_br_radius' => '0', |
| 370 |
'btn_bl_radius' => '0', |
| 371 |
'btn_font_size' => '14', |
| 372 |
'btn_line_height' => '18', |
| 373 |
'btn_h_color' => '#424242', |
| 374 |
'btn_h_bg_color' => '#e1e3e8', |
| 375 |
|
| 376 |
'svg_icon_color' => '#23282d', |
| 377 |
'svg_icon_width' => '20', |
| 378 |
'svg_icon_t_padding' => '5', |
| 379 |
'svg_icon_r_padding' => '5', |
| 380 |
'svg_icon_b_padding' => '5', |
| 381 |
'svg_icon_l_padding' => '5', |
| 382 |
'svg_icon_h_color' => '#23282d', |
| 383 |
|
| 384 |
'cover_color' => '#424242', |
| 385 |
'cover_font_size' => '14', |
| 386 |
] |
| 387 |
), |
| 388 |
'nonce' => wp_create_nonce( 'copy-the-code' ), |
| 389 |
'elementor' => [ |
| 390 |
'blocks' => Blocks::get_blocks(), |
| 391 |
], |
| 392 |
'conditions' => [ |
| 393 |
[ |
| 394 |
'key' => 'post_type', |
| 395 |
'name' => 'Post Type', |
| 396 |
'types' => $this->get_post_types(), |
| 397 |
], |
| 398 |
[ |
| 399 |
'key' => 'taxonomy', |
| 400 |
'name' => 'Taxonomy', |
| 401 |
'types' => $this->get_taxonomies(), |
| 402 |
], |
| 403 |
[ |
| 404 |
'key' => 'user_role', |
| 405 |
'name' => 'User Role', |
| 406 |
'types' => $this->get_user_roles(), |
| 407 |
], |
| 408 |
], |
| 409 |
] |
| 410 |
); |
| 411 |
|
| 412 |
} |
| 413 |
|
| 414 |
/** |
| 415 |
* Get post types |
| 416 |
* |
| 417 |
* @since 3.8.0 |
| 418 |
* @return array |
| 419 |
*/ |
| 420 |
public function get_post_types() { |
| 421 |
$post_types = get_post_types( [ 'public' => true ], 'objects' ); |
| 422 |
$conditions = [ |
| 423 |
[ |
| 424 |
'value' => 'all', |
| 425 |
'name' => esc_html__( 'All', 'copy-the-code' ), |
| 426 |
], |
| 427 |
]; |
| 428 |
|
| 429 |
foreach ( $post_types as $post_type ) { |
| 430 |
$conditions[] = [ |
| 431 |
'value' => $post_type->name, |
| 432 |
'name' => $post_type->label, |
| 433 |
]; |
| 434 |
} |
| 435 |
|
| 436 |
return $conditions; |
| 437 |
} |
| 438 |
|
| 439 |
/** |
| 440 |
* Get taxonomies |
| 441 |
* |
| 442 |
* @since 3.8.0 |
| 443 |
* @return array |
| 444 |
*/ |
| 445 |
public function get_taxonomies() { |
| 446 |
$taxonomies = get_taxonomies( [ 'public' => true ], 'objects' ); |
| 447 |
$conditions = [ |
| 448 |
[ |
| 449 |
'value' => 'all', |
| 450 |
'name' => esc_html__( 'All', 'copy-the-code' ), |
| 451 |
], |
| 452 |
]; |
| 453 |
|
| 454 |
foreach ( $taxonomies as $taxonomy ) { |
| 455 |
$conditions[] = [ |
| 456 |
'value' => $taxonomy->name, |
| 457 |
'name' => $taxonomy->label, |
| 458 |
]; |
| 459 |
} |
| 460 |
|
| 461 |
return $conditions; |
| 462 |
} |
| 463 |
|
| 464 |
/** |
| 465 |
* Get user roles |
| 466 |
* |
| 467 |
* @since 3.8.0 |
| 468 |
* @return array |
| 469 |
*/ |
| 470 |
public function get_user_roles() { |
| 471 |
global $wp_roles; |
| 472 |
|
| 473 |
$roles = $wp_roles->roles; |
| 474 |
$conditions = [ |
| 475 |
[ |
| 476 |
'value' => 'all', |
| 477 |
'name' => esc_html__( 'All', 'copy-the-code' ), |
| 478 |
], |
| 479 |
]; |
| 480 |
|
| 481 |
foreach ( $roles as $role => $details ) { |
| 482 |
$conditions[] = [ |
| 483 |
'value' => $role, |
| 484 |
'name' => $details['name'], |
| 485 |
]; |
| 486 |
} |
| 487 |
|
| 488 |
return $conditions; |
| 489 |
} |
| 490 |
|
| 491 |
/** |
| 492 |
* Register menus |
| 493 |
* |
| 494 |
* @since 3.0.0 |
| 495 |
* @return void |
| 496 |
*/ |
| 497 |
public function register() { |
| 498 |
add_submenu_page( |
| 499 |
'options-general.php', |
| 500 |
__( 'Add New', 'copy-the-code' ), |
| 501 |
__( ' → Add New', 'copy-the-code' ), |
| 502 |
'manage_options', |
| 503 |
'copy-the-code', |
| 504 |
[ $this, 'markup' ] |
| 505 |
); |
| 506 |
} |
| 507 |
|
| 508 |
/** |
| 509 |
* Markup |
| 510 |
* |
| 511 |
* @since 3.0.0 |
| 512 |
* @return void |
| 513 |
*/ |
| 514 |
public function markup() { |
| 515 |
?> |
| 516 |
<div id="ctc-dashboard-root"></div> |
| 517 |
<?php |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
} |
| 522 |
|
| 523 |
/** |
| 524 |
* Initialize class object with 'get_instance()' method |
| 525 |
*/ |
| 526 |
Copy_The_Code_Dashboard::get_instance(); |
| 527 |
|
| 528 |
endif; |
| 529 |
|