css
11 months ago
font
11 months ago
images
11 months ago
js
11 months ago
admin.php
11 months ago
edit.php
11 months ago
form.php
11 months ago
insert.php
11 months ago
manage.php
11 months ago
settings.php
11 months ago
tools.php
11 months ago
tools.php
123 lines
| 1 | <?php |
| 2 | |
| 3 | if( ! defined( 'ABSPATH' ) ) exit; |
| 4 | |
| 5 | class SC_Admin_Tools{ |
| 6 | |
| 7 | public static function init(){ |
| 8 | |
| 9 | // Register the action for admin ajax features |
| 10 | add_action( 'wp_ajax_sc_insert_window', array( __CLASS__, 'insert_window' ) ); |
| 11 | |
| 12 | // Add TinyMCE button |
| 13 | add_action( 'admin_init', array( __CLASS__, 'register_mce' ) ); |
| 14 | |
| 15 | add_action( 'wp_enqueue_editor', array( __CLASS__, 'enqueue_insert_scripts' ) ); |
| 16 | |
| 17 | // Gutenberg block |
| 18 | add_action( 'init', array( __CLASS__, 'register_block' ) ); |
| 19 | |
| 20 | } |
| 21 | |
| 22 | public static function register_mce(){ |
| 23 | |
| 24 | add_filter( 'mce_buttons', array( __CLASS__, 'register_mce_button' ) ); |
| 25 | |
| 26 | add_filter( 'mce_external_plugins', array( __CLASS__, 'register_mce_js' ) ); |
| 27 | |
| 28 | } |
| 29 | |
| 30 | public static function register_mce_button( $buttons ){ |
| 31 | |
| 32 | if( self::is_sc_edit_page() ) |
| 33 | return $buttons; |
| 34 | |
| 35 | array_push( $buttons, 'separator', 'shortcoder' ); |
| 36 | return $buttons; |
| 37 | |
| 38 | } |
| 39 | |
| 40 | public static function register_mce_js( $plugins ){ |
| 41 | |
| 42 | if( self::is_sc_edit_page() ) |
| 43 | return $plugins; |
| 44 | |
| 45 | $plugins[ 'shortcoder' ] = SC_ADMIN_URL . '/js/tinymce/editor_plugin.js'; |
| 46 | return $plugins; |
| 47 | |
| 48 | } |
| 49 | |
| 50 | public static function register_block(){ |
| 51 | |
| 52 | if( !function_exists( 'register_block_type' ) ){ |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | $asset_file = include( SC_PATH . 'admin/js/blocks/index.asset.php'); |
| 57 | |
| 58 | wp_register_script( |
| 59 | 'shortcoder', |
| 60 | SC_ADMIN_URL . '/js/blocks/index.js', |
| 61 | $asset_file[ 'dependencies' ], |
| 62 | $asset_file[ 'version' ] |
| 63 | ); |
| 64 | |
| 65 | register_block_type( 'shortcoder/shortcoder', array( |
| 66 | 'render_callback' => array( __CLASS__, 'render_block' ), |
| 67 | 'editor_script' => 'shortcoder' |
| 68 | )); |
| 69 | |
| 70 | } |
| 71 | |
| 72 | public static function enqueue_insert_scripts(){ |
| 73 | |
| 74 | if( self::is_sc_edit_page() || !is_admin() ) |
| 75 | return; |
| 76 | |
| 77 | wp_enqueue_script( 'sc-tools-js', SC_ADMIN_URL . 'js/script-tools.js', array( 'jquery' ), SC_VERSION ); |
| 78 | |
| 79 | wp_enqueue_style( 'sc-tools-css', SC_ADMIN_URL . 'css/style-tools.css', array(), SC_VERSION ); |
| 80 | |
| 81 | wp_localize_script( 'sc-tools-js', 'SC_INSERT_VARS', array( |
| 82 | 'insert_page' => admin_url( 'admin-ajax.php?action=sc_insert_window' ), |
| 83 | 'popup_title' => __( 'Insert shortcode to editor', 'shortcoder' ), |
| 84 | 'popup_opened' => false, |
| 85 | 'block_editor' => false, |
| 86 | 'block_inline_insert' => false |
| 87 | )); |
| 88 | |
| 89 | } |
| 90 | |
| 91 | public static function render_block( $attributes, $content ){ |
| 92 | return wpautop( $content ); |
| 93 | } |
| 94 | |
| 95 | public static function insert_window(){ |
| 96 | |
| 97 | if( !current_user_can( 'edit_posts' ) ){ |
| 98 | wp_die( __( 'Not enough permissions to browse and insert shortcodes', 'shortcoder' ) ); |
| 99 | } |
| 100 | |
| 101 | include_once( 'insert.php' ); |
| 102 | wp_die(); |
| 103 | |
| 104 | } |
| 105 | |
| 106 | public static function is_sc_edit_page(){ |
| 107 | |
| 108 | if( !is_admin() ){ |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | require_once( ABSPATH . 'wp-admin/includes/screen.php' ); |
| 113 | |
| 114 | $screen = get_current_screen(); |
| 115 | return ( $screen && $screen->post_type == SC_POST_TYPE && $screen->base == 'post' ); |
| 116 | |
| 117 | } |
| 118 | |
| 119 | } |
| 120 | |
| 121 | SC_Admin_Tools::init(); |
| 122 | |
| 123 | ?> |