| 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 |
|
| 74 |
if ( ! wp_verify_nonce( $nonce, 'copy-the-code' ) ) { |
| 75 |
wp_send_json_error( [ 'message' => __( 'Invalid nonce', 'copy-the-code' ) ] ); |
| 76 |
} |
| 77 |
|
| 78 |
if ( ! current_user_can( 'edit_posts' ) ) { |
| 79 |
wp_send_json_error( [ 'message' => __( 'You do not have permission to edit posts', 'copy-the-code' ) ] ); |
| 80 |
} |
| 81 |
|
| 82 |
$post_title = isset( $post['post_title'] ) ? sanitize_text_field( $post['post_title'] ) : ''; |
| 83 |
|
| 84 |
if ( $on_edit ) { |
| 85 |
$post_id = isset( $post['ID'] ) ? absint( $post['ID'] ) : 0; |
| 86 |
|
| 87 |
wp_update_post( [ |
| 88 |
'ID' => $post_id, |
| 89 |
'post_title' => $post_title, |
| 90 |
] ); |
| 91 |
} else { |
| 92 |
$post = [ |
| 93 |
'post_title' => $post_title, |
| 94 |
'post_type' => 'copy-to-clipboard', |
| 95 |
'post_status' => 'publish', |
| 96 |
]; |
| 97 |
|
| 98 |
$post_id = wp_insert_post( $post ); |
| 99 |
} |
| 100 |
|
| 101 |
$meta = [ |
| 102 |
'button-text' => $btn_text, |
| 103 |
'button-copy-text' => $btn_after_copy_text, |
| 104 |
'button-title' => $btn_title, |
| 105 |
'copy-format' => $format, |
| 106 |
'selector' => $selector, |
| 107 |
'button-position' => $position, |
| 108 |
'style' => $style_type, |
| 109 |
]; |
| 110 |
|
| 111 |
foreach ( $meta as $key => $value ) { |
| 112 |
update_post_meta( $post_id, $key, $value ); |
| 113 |
} |
| 114 |
|
| 115 |
do_action( 'copy_the_code/dashboard/save' ); |
| 116 |
|
| 117 |
wp_send_json_success( [ |
| 118 |
'message' => __( 'Saved', 'copy-the-code' ), |
| 119 |
'edit_post_url' => get_edit_post_link( $post_id ), |
| 120 |
] ); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Filters the post edit link. |
| 125 |
* |
| 126 |
* @since 3.0.0 |
| 127 |
* |
| 128 |
* @param string $link The edit link. |
| 129 |
* @param int $post_id Post ID. |
| 130 |
* @param string $context The link context. If set to 'display' then ampersands |
| 131 |
* are encoded. |
| 132 |
*/ |
| 133 |
public function edit_post_link( $link, $post_id, $context ) { |
| 134 |
global $mode; |
| 135 |
|
| 136 |
if ( 'list' !== $mode ) { |
| 137 |
return $link; |
| 138 |
} |
| 139 |
|
| 140 |
$post_type = get_post_type( $post_id ); |
| 141 |
if ( 'copy-to-clipboard' !== $post_type ) { |
| 142 |
return $link; |
| 143 |
} |
| 144 |
|
| 145 |
return esc_url( admin_url( 'options-general.php?page=copy-the-code&id=' . $post_id ) ); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Post row actions |
| 150 |
* |
| 151 |
* @since 3.0.0 |
| 152 |
* @return array |
| 153 |
*/ |
| 154 |
public function post_row_actions( $actions, $post ) { |
| 155 |
if ( 'copy-to-clipboard' !== $post->post_type ) { |
| 156 |
return $actions; |
| 157 |
} |
| 158 |
|
| 159 |
$actions['edit'] = sprintf( |
| 160 |
'<a href="%s">%s</a>', |
| 161 |
esc_url( admin_url( 'options-general.php?page=copy-the-code&id=' . $post->ID ) ), |
| 162 |
__( 'Edit', 'copy-the-code' ) |
| 163 |
); |
| 164 |
|
| 165 |
return $actions; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Add menus |
| 170 |
* |
| 171 |
* @since 3.0.0 |
| 172 |
* @return void |
| 173 |
*/ |
| 174 |
public function add_menus() { |
| 175 |
if ( ! current_user_can( 'edit_posts' ) ) { |
| 176 |
return; |
| 177 |
} |
| 178 |
|
| 179 |
add_action( 'admin_menu', array( $this, 'register' ) ); |
| 180 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 181 |
add_action( 'admin_footer', array( $this, 'hide_menus' ) ); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Hide menus |
| 186 |
* |
| 187 |
* @since 3.0.0 |
| 188 |
* @return void |
| 189 |
*/ |
| 190 |
function hide_menus() { |
| 191 |
?> |
| 192 |
<style type="text/css"> |
| 193 |
#adminmenu a[href="options-general.php?page=copy-the-code"], |
| 194 |
#adminmenu a[href="options-general.php?page=copy-the-code-contact"], |
| 195 |
#adminmenu a[href="options-general.php?page=copy-the-code-wp-support-forum"], |
| 196 |
#adminmenu a[href="options-general.php?page=copy-the-code-pricing"], |
| 197 |
#adminmenu a[href="options-general.php?page=copy-the-code-account"] { |
| 198 |
display: none !important; |
| 199 |
} |
| 200 |
</style> |
| 201 |
<?php |
| 202 |
|
| 203 |
if ( ! isset( $_GET['post_type'] ) ) { |
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
if ( 'copy-to-clipboard' !== $_GET['post_type'] ) { |
| 208 |
return; |
| 209 |
} |
| 210 |
|
| 211 |
wp_enqueue_script( 'jquery' ); |
| 212 |
|
| 213 |
$menus = array( |
| 214 |
'copy-the-code-wp-support-forum' => 'Support Forum', |
| 215 |
); |
| 216 |
|
| 217 |
if ( ctc_fs()->is_not_paying() ) { |
| 218 |
$menus['copy-the-code-pricing'] = 'Upgrade'; |
| 219 |
} else { |
| 220 |
$menus['copy-the-code-account'] = 'Account'; |
| 221 |
} |
| 222 |
|
| 223 |
$menus = array_merge( |
| 224 |
$menus, |
| 225 |
array( |
| 226 |
'copy-the-code-contact' => 'Contact Us', |
| 227 |
'copy-the-code' => 'Dashboard', |
| 228 |
) |
| 229 |
); |
| 230 |
|
| 231 |
?> |
| 232 |
<script> |
| 233 |
// Add button tag after class .page-title-action. |
| 234 |
jQuery( document ).ready( function( $ ) { |
| 235 |
let menus = <?php echo wp_json_encode( $menus ); ?>; |
| 236 |
$.each( menus, function( key, value ) { |
| 237 |
let target = ''; |
| 238 |
if( 'copy-the-code-wp-support-forum' === key ) { |
| 239 |
target = 'target="_blank"'; |
| 240 |
} |
| 241 |
$( '.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>' ); |
| 242 |
} ); |
| 243 |
} ); |
| 244 |
</script> |
| 245 |
<style> |
| 246 |
.cta-sub-menu.cta-sub-menu-copy-the-code-wp-support-forum:after { |
| 247 |
content: "\f504"; |
| 248 |
font-family: dashicons; |
| 249 |
display: inline-block; |
| 250 |
line-height: 1; |
| 251 |
font-weight: 400; |
| 252 |
font-style: normal; |
| 253 |
speak: never; |
| 254 |
text-decoration: inherit; |
| 255 |
text-transform: none; |
| 256 |
text-rendering: auto; |
| 257 |
-webkit-font-smoothing: antialiased; |
| 258 |
-moz-osx-font-smoothing: grayscale; |
| 259 |
width: 20px; |
| 260 |
height: 20px; |
| 261 |
font-size: 20px; |
| 262 |
vertical-align: top; |
| 263 |
text-align: center; |
| 264 |
transition: color 0.1s ease-in; |
| 265 |
text-decoration: none; |
| 266 |
font-size: 14px; |
| 267 |
vertical-align: sub; |
| 268 |
} |
| 269 |
.wrap .page-title-action { |
| 270 |
margin-right: 10px; |
| 271 |
} |
| 272 |
.cta-sub-menu { |
| 273 |
padding: 0 5px; |
| 274 |
display: inline-block; |
| 275 |
margin: 0; |
| 276 |
text-decoration: underline; |
| 277 |
top: -3px; |
| 278 |
position: relative; |
| 279 |
} |
| 280 |
</style> |
| 281 |
<?php |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* Enqueue scripts |
| 286 |
* |
| 287 |
* @since 3.0.0 |
| 288 |
* @return void |
| 289 |
*/ |
| 290 |
public function enqueue_scripts( $hook = '' ) { |
| 291 |
if ( 'settings_page_copy-the-code' !== $hook ) { |
| 292 |
return; |
| 293 |
} |
| 294 |
|
| 295 |
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 ); |
| 296 |
wp_enqueue_style( 'copy-the-code-dashboard', COPY_THE_CODE_URI . 'assets/admin/css/dashboard.css', null, COPY_THE_CODE_VER, 'all' ); |
| 297 |
$id = isset( $_GET['id' ] ) ? absint( $_GET['id' ] ) : 0; |
| 298 |
|
| 299 |
wp_localize_script( |
| 300 |
'copy-the-code-dashboard', |
| 301 |
'CopyDashboardVars', |
| 302 |
[ |
| 303 |
'uri' => COPY_THE_CODE_URI, |
| 304 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 305 |
'nonce' => wp_create_nonce( 'copy-the-code' ), |
| 306 |
'onEdit' => $id ? true : false, |
| 307 |
'editUrl' => $id ? get_edit_post_link( $id ) : admin_url( 'edit.php?post_type=copy-to-clipboard' ), |
| 308 |
'post' => $id ? get_post( $id ) : [], |
| 309 |
'meta' => $id ? [ |
| 310 |
'button-text' => get_post_meta( $id, 'button-text', true ), |
| 311 |
'button-copy-text' => get_post_meta( $id, 'button-copy-text', true ), |
| 312 |
'button-title' => get_post_meta( $id, 'button-title', true ), |
| 313 |
'copy-format' => get_post_meta( $id, 'copy-format', true ), |
| 314 |
'button-position' => get_post_meta( $id, 'button-position', true ), |
| 315 |
'selector' => get_post_meta( $id, 'selector', true ), |
| 316 |
'style' => get_post_meta( $id, 'style', true ), |
| 317 |
] : [ |
| 318 |
'button-text' => 'Copy', |
| 319 |
'button-copy-text' => 'Copied', |
| 320 |
'button-title' => 'Copy', |
| 321 |
'copy-format' => '', |
| 322 |
'button-position' => 'outside', |
| 323 |
'selector' => 'pre', |
| 324 |
'style' => 'button', |
| 325 |
], |
| 326 |
'upgradeUrl' => admin_url( 'options-general.php?billing_cycle=annual&page=copy-the-code-pricing' ), |
| 327 |
'style' => get_option( 'ctc_default_style', [ |
| 328 |
'btn_color' => '#424242', |
| 329 |
'btn_bg_color' => '#e1e3e8', |
| 330 |
'btn_l_padding' => '20', |
| 331 |
'btn_t_padding' => '10', |
| 332 |
'btn_r_padding' => '20', |
| 333 |
'btn_b_padding' => '10', |
| 334 |
'btn_l_margin' => '0', |
| 335 |
'btn_t_margin' => '0', |
| 336 |
'btn_r_margin' => '0', |
| 337 |
'btn_b_margin' => '0', |
| 338 |
'btn_tl_radius' => '0', |
| 339 |
'btn_tr_radius' => '0', |
| 340 |
'btn_br_radius' => '0', |
| 341 |
'btn_bl_radius' => '0', |
| 342 |
'btn_font_size' => '14', |
| 343 |
'btn_line_height' => '18', |
| 344 |
'btn_h_color' => '#424242', |
| 345 |
'btn_h_bg_color' => '#e1e3e8', |
| 346 |
|
| 347 |
'svg_icon_color' => "#23282d", |
| 348 |
'svg_icon_width' => "20", |
| 349 |
'svg_icon_t_padding' => "5", |
| 350 |
'svg_icon_r_padding' => "5", |
| 351 |
'svg_icon_b_padding' => "5", |
| 352 |
'svg_icon_l_padding' => "5", |
| 353 |
'svg_icon_h_color' => "#23282d", |
| 354 |
|
| 355 |
'cover_color' => '#424242', |
| 356 |
'cover_font_size' => '14', |
| 357 |
] ), |
| 358 |
'nonce' => wp_create_nonce( 'copy-the-code' ), |
| 359 |
'elementor' => [ |
| 360 |
'blocks' => Blocks::get_blocks(), |
| 361 |
] |
| 362 |
] |
| 363 |
); |
| 364 |
|
| 365 |
} |
| 366 |
|
| 367 |
/** |
| 368 |
* Register menus |
| 369 |
* |
| 370 |
* @since 3.0.0 |
| 371 |
* @return void |
| 372 |
*/ |
| 373 |
public function register() { |
| 374 |
add_submenu_page( |
| 375 |
'options-general.php', |
| 376 |
__( 'Add New', 'copy-the-code' ), |
| 377 |
__( ' → Add New', 'copy-the-code' ), |
| 378 |
'manage_options', |
| 379 |
'copy-the-code', |
| 380 |
array( $this, 'markup' ) |
| 381 |
); |
| 382 |
} |
| 383 |
|
| 384 |
/** |
| 385 |
* Markup |
| 386 |
* |
| 387 |
* @since 3.0.0 |
| 388 |
* @return void |
| 389 |
*/ |
| 390 |
public function markup() { |
| 391 |
?> |
| 392 |
<div id="ctc-dashboard-root"></div> |
| 393 |
<?php |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
} |
| 398 |
|
| 399 |
/** |
| 400 |
* Initialize class object with 'get_instance()' method |
| 401 |
*/ |
| 402 |
Copy_The_Code_Dashboard::get_instance(); |
| 403 |
|
| 404 |
endif; |
| 405 |
|