| 1 |
<?php |
| 2 |
if( !class_exists( 'BBlocksCustomCursor' ) ){ |
| 3 |
class BBlocksCustomCursor{ |
| 4 |
function __construct(){ |
| 5 |
add_action( 'init', [ $this, 'bBlocksRegisterPostMeta' ] ); |
| 6 |
add_action( 'the_content', [ $this, 'bBlocksTheContent' ] ); |
| 7 |
add_action("enqueue_block_assets",array( $this, 'bBlocksEnqueueBlockAssets')); |
| 8 |
add_action("enqueue_block_editor_assets",array( $this, 'bBlocksEnqueueBlockEditorAssets' )); |
| 9 |
add_action('wp_ajax_bBlocks_cursor_data_settings', [$this, 'bBlocks_cursor_data_settings']); |
| 10 |
add_action('wp_ajax_bBlocks_get_cursor_data_settings', [$this, 'bBlocks_get_cursor_data_settings']); |
| 11 |
add_action('wp_ajax_nopriv_bBlocks_get_cursor_data_settings', [$this, 'bBlocks_get_cursor_data_settings']); |
| 12 |
} |
| 13 |
|
| 14 |
function bBlocksRegisterPostMeta(){ |
| 15 |
register_post_meta( '', 'bBlocksCursorData', [ |
| 16 |
'show_in_rest' => true, |
| 17 |
'single' => true, |
| 18 |
'type' => 'string', |
| 19 |
'sanitize_callback' => 'sanitize_text_field', |
| 20 |
] ); |
| 21 |
} |
| 22 |
|
| 23 |
function bBlocksTheContent( $content ){ |
| 24 |
$id = get_the_ID(); |
| 25 |
$postMeta = get_post_meta( $id, 'bBlocksCursorData', true ); |
| 26 |
|
| 27 |
if( !$postMeta ){ |
| 28 |
return $content; |
| 29 |
} |
| 30 |
|
| 31 |
return $content . '<div id="bBlocksCursorData" data-cursor="'. esc_attr( $postMeta ) .'"></div>'; |
| 32 |
} |
| 33 |
|
| 34 |
public function bBlocks_cursor_data_settings(){ |
| 35 |
|
| 36 |
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ?? '' ) ), 'wp_ajax' ) ) { |
| 37 |
wp_send_json_error( 'invalid request' ); |
| 38 |
} |
| 39 |
|
| 40 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 41 |
wp_send_json_error( 'Unauthorized' ); |
| 42 |
} |
| 43 |
|
| 44 |
$data = json_decode( sanitize_text_field( wp_unslash( $_POST['bBlocksCustomCursor'] ?? '' ) ), true ); |
| 45 |
|
| 46 |
if(!$data){ |
| 47 |
$data = get_option('bBlocks_custom_cursor_settings', []); |
| 48 |
wp_send_json_success($data); |
| 49 |
} |
| 50 |
|
| 51 |
update_option('bBlocks_custom_cursor_settings', $data); |
| 52 |
|
| 53 |
wp_send_json_success($data); |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
public function bBlocks_get_cursor_data_settings(){ |
| 58 |
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ?? '' ) ), 'wp_ajax' ) ) { |
| 59 |
wp_send_json_error('invalid request'); |
| 60 |
} |
| 61 |
|
| 62 |
$data = get_option('bBlocks_custom_cursor_settings', []); |
| 63 |
wp_send_json_success($data); |
| 64 |
|
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
public function bBlocksEnqueueBlockAssets() { |
| 69 |
wp_enqueue_script( 'bBlocks-cursor', B_BLOCKS_DIR_URL . 'build/cursor/cursor.js', array('react', 'react-dom', 'react-jsx-runtime', 'wp-util'), B_BLOCKS_VERSION, true); |
| 70 |
wp_enqueue_style( 'bBlocks-cursor', B_BLOCKS_DIR_URL . 'build/cursor/cursor.css', array(), B_BLOCKS_VERSION); |
| 71 |
|
| 72 |
wp_localize_script( 'bBlocks-cursor', 'bBlocksCursorDataConfig', array( |
| 73 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 74 |
'nonce' => wp_create_nonce('wp_ajax'), |
| 75 |
'dirUrl' => B_BLOCKS_DIR_URL |
| 76 |
)); |
| 77 |
} |
| 78 |
|
| 79 |
function bBlocksEnqueueBlockEditorAssets(){ |
| 80 |
wp_enqueue_script('bBlocks-cursor-settings', B_BLOCKS_DIR_URL . 'build/cursor/settings.js', array('wp-compose','wp-data','wp-editor','wp-plugins','wp-components','wp-i18n','react','react-dom','react-jsx-runtime'), B_BLOCKS_VERSION, true); |
| 81 |
wp_enqueue_style("bBlocks-cursor-settings", B_BLOCKS_DIR_URL . 'build/cursor/settings.css', array(), B_BLOCKS_VERSION); |
| 82 |
} |
| 83 |
} |
| 84 |
new BBlocksCustomCursor(); |
| 85 |
} |