| @@ -1,97 +1,224 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -defined('ABSPATH') or die('Jog on!'); | |
| 4 | - | |
| 5 | -/** | |
| 6 | - * Build admin menu | |
| 7 | - */ | |
| 8 | -function sh_cd_build_admin_menu() { | |
| 9 | - | |
| 10 | - add_menu_page( SH_CD_PLUGIN_NAME, SH_CD_PLUGIN_NAME, 'manage_options', 'sh-cd-shortcode-variables-main-menu', 'sh_cd_user_defined_page', 'dashicons-editor-kitchensink' ); | |
| 11 | - | |
| 12 | - // Hide duplicated sub menu (wee hack!) | |
| 13 | - add_submenu_page( 'sh-cd-shortcode-variables-main-menu', '', '', 'manage_options', 'sh-cd-shortcode-variables-main-menu', 'sh_cd_user_defined_page'); | |
| 14 | - | |
| 15 | - // Add sub menus | |
| 16 | - add_submenu_page( 'sh-cd-shortcode-variables-main-menu', __( 'Your Shortcodes', SH_CD_SLUG ), __('Your shortcodes'), 'manage_options', 'sh-cd-shortcode-variables-your-shortcodes', 'sh_cd_pages_your_shortcodes'); | |
| 17 | - add_submenu_page( 'sh-cd-shortcode-variables-main-menu', __( 'Premade Shortcodes', SH_CD_SLUG ), __('Premade shortcodes'), 'manage_options', 'sh-cd-shortcode-variables-sub-premade', 'sh_cd_premade_shortcodes_page'); | |
| 18 | - | |
| 19 | - $menu_text = ( true === sh_cd_license_is_premium() ) ? __('Your License', SH_CD_SLUG ) : __('Upgrade to Premium', SH_CD_SLUG ); | |
| 20 | - | |
| 21 | - add_submenu_page( 'sh-cd-shortcode-variables-main-menu', $menu_text, $menu_text, 'manage_options', 'sh-cd-shortcode-variables-license', 'sh_cd_advertise_pro'); | |
| 22 | -} | |
| 23 | -add_action( 'admin_menu', 'sh_cd_build_admin_menu' ); | |
| 24 | - | |
| 25 | -/** | |
| 26 | - * Enqueue relevant CSS / JS | |
| 27 | - */ | |
| 28 | -function sh_cd_enqueue_scripts() { | |
| 29 | - wp_enqueue_style( 'sh-cd', plugins_url( '../assets/css/sh-cd.css', __FILE__ ), [], SH_CD_PLUGIN_VERSION ) ; | |
| 30 | - wp_enqueue_style('fontawesome', 'https://use.fontawesome.com/releases/v5.7.2/css/all.css', [], SH_CD_PLUGIN_VERSION); | |
| 31 | - wp_enqueue_script( 'sh-cd', plugins_url( '../assets/js/sh-cd.js', __FILE__ ), [ 'jquery' ], SH_CD_PLUGIN_VERSION); | |
| 32 | - | |
| 33 | - wp_localize_script( 'sh-cd', 'sh_cd', [ 'security' => wp_create_nonce( 'sh-cd-security' ), 'premium' => sh_cd_license_is_premium() ] ); | |
| 34 | - | |
| 35 | -} | |
| 36 | -add_action( 'admin_enqueue_scripts', 'sh_cd_enqueue_scripts' ); | |
| 37 | - | |
| 38 | -/** | |
| 39 | - * Run installer on each version number change or install | |
| 40 | - */ | |
| 41 | -function sh_cd_upgrade() { | |
| 42 | - | |
| 43 | - if ( true === update_option( 'sh-cd-version-number', SH_CD_PLUGIN_VERSION ) ) { | |
| 44 | - sh_cd_create_database_table(); | |
| 45 | - } | |
| 46 | -} | |
| 47 | -add_action('admin_init', 'sh_cd_upgrade'); | |
| 48 | - | |
| 49 | - | |
| 50 | -/** | |
| 51 | - Ajax handler for toggling disable status of a shortcode | |
| 52 | - **/ | |
| 53 | -function sh_cd_ajax_toggle_status() { | |
| 54 | - | |
| 55 | - if ( false === sh_cd_license_is_premium() ) { | |
| 56 | - wp_send_json( 'not-premium' ); | |
| 57 | - } | |
| 58 | - | |
| 59 | - check_ajax_referer( 'sh-cd-security', 'security' ); | |
| 60 | - | |
| 61 | - $id = ( false === empty( $_POST['id'] ) ) ? (int) $_POST['id'] : NULL; | |
| 62 | - | |
| 63 | - if ( false === empty( $id ) ) { | |
| 64 | - | |
| 65 | - $new_status = sh_cd_toggle_status( $id ); | |
| 66 | - | |
| 67 | - wp_send_json( [ 'id' => $id, 'status' => $new_status, 'ok' => 1 ] ); | |
| 68 | - } | |
| 69 | - | |
| 70 | - wp_send_json( 'shortcode-not-found' ); | |
| 71 | -} | |
| 72 | -add_action( 'wp_ajax_toggle_status', 'sh_cd_ajax_toggle_status' ); | |
| 73 | - | |
| 74 | -/** | |
| 75 | -Ajax handler for saving shortcode inline | |
| 76 | - **/ | |
| 77 | -function sh_cd_ajax_update_shortcode() { | |
| 78 | - | |
| 79 | - if ( false === sh_cd_license_is_premium() ) { | |
| 80 | - wp_send_json( 'not-premium' ); | |
| 81 | - } | |
| 82 | - | |
| 83 | - check_ajax_referer( 'sh-cd-security', 'security' ); | |
| 84 | - | |
| 85 | - $id = ( false === empty( $_POST['id'] ) ) ? (int) $_POST['id'] : NULL; | |
| 86 | - $content = ( false === empty( $_POST['content'] ) ) ? $_POST['content'] : ''; | |
| 87 | - | |
| 88 | - if ( false === empty( $id ) ) { | |
| 89 | - | |
| 90 | - $result = sh_cd_db_shortcodes_update_content( $id, $content ); | |
| 91 | - | |
| 92 | - wp_send_json( [ 'id' => $id, 'ok' => ( true === $result ) ? 1 : 0 ] ); | |
| 93 | - } | |
| 94 | - | |
| 95 | - wp_send_json( 'shortcode-not-found' ); | |
| 96 | -} | |
| 97 | -add_action( 'wp_ajax_update_shortcode', 'sh_cd_ajax_update_shortcode' ); | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +defined('ABSPATH') or die('Jog on!'); | |
| 4 | + | |
| 5 | +/** | |
| 6 | + * Build admin menu | |
| 7 | + */ | |
| 8 | +function sh_cd_build_admin_menu() { | |
| 9 | + | |
| 10 | + $allowed_viewer = sh_cd_permission_role(); | |
| 11 | + | |
| 12 | + add_menu_page( SH_CD_PLUGIN_NAME, SH_CD_PLUGIN_NAME, 'manage_options', 'sh-cd-shortcode-variables-main-menu', 'sh_cd_pages_your_shortcodes', 'dashicons-editor-kitchensink' ); | |
| 13 | + | |
| 14 | + // Hide duplicated sub menu (wee hack!) | |
| 15 | + add_submenu_page( 'sh-cd-shortcode-variables-main-menu', '', '', 'manage_options', 'sh-cd-shortcode-variables-main-menu', 'sh_cd_pages_your_shortcodes'); | |
| 16 | + | |
| 17 | + // Add sub menus | |
| 18 | + add_submenu_page( 'sh-cd-shortcode-variables-main-menu', __( 'Your Shortcodes', SH_CD_SLUG ), __( 'Your shortcodes', SH_CD_SLUG ), $allowed_viewer, 'sh-cd-shortcode-variables-your-shortcodes', 'sh_cd_pages_your_shortcodes'); | |
| 19 | + add_submenu_page( 'sh-cd-shortcode-variables-main-menu', __( 'Premade Shortcodes', SH_CD_SLUG ), __( 'Premade shortcodes', SH_CD_SLUG ), $allowed_viewer, 'sh-cd-shortcode-variables-sub-premade', 'sh_cd_premade_shortcodes_page'); | |
| 20 | + | |
| 21 | + add_submenu_page( 'sh-cd-shortcode-variables-main-menu', __( 'Import shortcodes', SH_CD_SLUG ), __( 'Import shortcodes', SH_CD_SLUG ), 'manage_options', 'sh-cd-import', 'sh_cd_admin_page_import' ); | |
| 22 | + | |
| 23 | + $menu_text = ( true === SH_CD_IS_PREMIUM ) ? __( 'Your License', SH_CD_SLUG ) : __( 'Upgrade to Premium', SH_CD_SLUG ); | |
| 24 | + | |
| 25 | + add_submenu_page( 'sh-cd-shortcode-variables-main-menu', $menu_text, $menu_text, 'manage_options', 'sh-cd-shortcode-variables-license', 'sh_cd_advertise_pro'); | |
| 26 | + add_submenu_page( 'sh-cd-shortcode-variables-main-menu', __( 'Settings', SH_CD_SLUG ), __( 'Settings', SH_CD_SLUG ), 'manage_options', 'sh-cd-settings', 'sh_cd_settings_page_generic' ); | |
| 27 | + add_submenu_page( 'sh-cd-shortcode-variables-main-menu', __( 'Help', SH_CD_SLUG ), __( 'Help', SH_CD_SLUG ), 'manage_options', 'sh-cd-help', 'sh_cd_help_page' ); | |
| 28 | +} | |
| 29 | +add_action( 'admin_menu', 'sh_cd_build_admin_menu' ); | |
| 30 | + | |
| 31 | +/** | |
| 32 | + * Enqueue relevant CSS / JS | |
| 33 | + */ | |
| 34 | +function sh_cd_enqueue_scripts() { | |
| 35 | + wp_enqueue_style( 'sh-cd', plugins_url( '../assets/css/sh-cd.css', __FILE__ ), [], SH_CD_PLUGIN_VERSION ) ; | |
| 36 | + wp_enqueue_style('fontawesome', 'https://use.fontawesome.com/releases/v5.7.2/css/all.css', [], SH_CD_PLUGIN_VERSION); | |
| 37 | + wp_enqueue_script( 'sh-cd', plugins_url( '../assets/js/sh-cd.js', __FILE__ ), [ 'jquery' ], SH_CD_PLUGIN_VERSION, true ); | |
| 38 | + | |
| 39 | + wp_localize_script( 'sh-cd', 'sh_cd', sh_cd_js_config() ); | |
| 40 | + | |
| 41 | +} | |
| 42 | +add_action( 'admin_enqueue_scripts', 'sh_cd_enqueue_scripts' ); | |
| 43 | + | |
| 44 | +/** | |
| 45 | + * Config for JS | |
| 46 | + * @return array | |
| 47 | + */ | |
| 48 | +function sh_cd_js_config() { | |
| 49 | + return [ | |
| 50 | + 'security' => wp_create_nonce( 'sh-cd-security' ), | |
| 51 | + 'premium' => SH_CD_IS_PREMIUM, | |
| 52 | + 'text-delete-confirm' => __( 'Are you sure you wish to delete this shortcode?', SH_CD_SLUG ), | |
| 53 | + 'text-add' => __( 'Add', SH_CD_SLUG ), | |
| 54 | + 'text-save' => __( 'Save', SH_CD_SLUG ), | |
| 55 | + 'text-saved' => __( 'Saved!', SH_CD_SLUG ), | |
| 56 | + 'text-error' => __( 'Unfortunately something went wrong!', SH_CD_SLUG ) | |
| 57 | + ]; | |
| 58 | +} | |
| 59 | + | |
| 60 | +/** | |
| 61 | + * Run installer on each version number change or install | |
| 62 | + */ | |
| 63 | +function sh_cd_upgrade() { | |
| 64 | + | |
| 65 | + if ( true === update_option( 'sh-cd-version-number-2021', SH_CD_PLUGIN_VERSION ) ) { | |
| 66 | + | |
| 67 | + sh_cd_create_database_table(); | |
| 68 | + | |
| 69 | + sh_cd_create_database_table_multisite(); | |
| 70 | + | |
| 71 | + sh_cd_cron_licence_check(); | |
| 72 | + | |
| 73 | + do_action( 'sh-cd-upgrade' ); | |
| 74 | + } | |
| 75 | +} | |
| 76 | +add_action('admin_init', 'sh_cd_upgrade'); | |
| 77 | + | |
| 78 | + | |
| 79 | +/** | |
| 80 | + Ajax handler for toggling disable status of a shortcode | |
| 81 | + **/ | |
| 82 | +function sh_cd_ajax_toggle_status() { | |
| 83 | + | |
| 84 | + if ( false === SH_CD_IS_PREMIUM ) { | |
| 85 | + wp_send_json( 'not-premium' ); | |
| 86 | + } | |
| 87 | + | |
| 88 | + check_ajax_referer( 'sh-cd-security', 'security' ); | |
| 89 | + | |
| 90 | + sh_cd_permission_check(); | |
| 91 | + | |
| 92 | + $id = ( false === empty( $_POST['id'] ) ) ? (int) $_POST['id'] : NULL; | |
| 93 | + | |
| 94 | + if ( false === empty( $id ) ) { | |
| 95 | + | |
| 96 | + $new_status = sh_cd_toggle_status( $id ); | |
| 97 | + | |
| 98 | + wp_send_json( [ 'id' => $id, 'status' => $new_status, 'ok' => 1 ] ); | |
| 99 | + } | |
| 100 | + | |
| 101 | + wp_send_json( 'shortcode-not-found' ); | |
| 102 | +} | |
| 103 | +add_action( 'wp_ajax_toggle_status', 'sh_cd_ajax_toggle_status' ); | |
| 104 | + | |
| 105 | +/** | |
| 106 | +Ajax handler for deleting a shortcode | |
| 107 | + **/ | |
| 108 | +function sh_cd_ajax_delete_shortcode() { | |
| 109 | + | |
| 110 | + check_ajax_referer( 'sh-cd-security', 'security' ); | |
| 111 | + | |
| 112 | + sh_cd_permission_check(); | |
| 113 | + | |
| 114 | + $id = ( false === empty( $_POST['id'] ) ) ? (int) $_POST['id'] : NULL; | |
| 115 | + | |
| 116 | + if ( false === empty( $id ) ) { | |
| 117 | + | |
| 118 | + $result = sh_cd_db_shortcodes_delete( $id ); | |
| 119 | + | |
| 120 | + wp_send_json( [ 'id' => $id, 'ok' => $result ] ); | |
| 121 | + } | |
| 122 | + | |
| 123 | + wp_send_json( 'shortcode-not-found' ); | |
| 124 | +} | |
| 125 | +add_action( 'wp_ajax_delete_shortcode', 'sh_cd_ajax_delete_shortcode' ); | |
| 126 | + | |
| 127 | +/** | |
| 128 | +Ajax handler for toggling disable status of a shortcode | |
| 129 | + **/ | |
| 130 | +function sh_cd_ajax_toggle_multisite() { | |
| 131 | + | |
| 132 | + if ( false === SH_CD_IS_PREMIUM ) { | |
| 133 | + wp_send_json( 'not-premium' ); | |
| 134 | + } | |
| 135 | + | |
| 136 | + check_ajax_referer( 'sh-cd-security', 'security' ); | |
| 137 | + | |
| 138 | + sh_cd_permission_check(); | |
| 139 | + | |
| 140 | + $id = ( false === empty( $_POST['id'] ) ) ? (int) $_POST['id'] : NULL; | |
| 141 | + | |
| 142 | + if ( false === empty( $id ) ) { | |
| 143 | + | |
| 144 | + $new_multisite = sh_cd_toggle_multisite( $id ); | |
| 145 | + | |
| 146 | + wp_send_json( [ 'id' => $id, 'multisite' => $new_multisite, 'ok' => 1 ] ); | |
| 147 | + } | |
| 148 | + | |
| 149 | + wp_send_json( 'shortcode-not-found' ); | |
| 150 | +} | |
| 151 | +add_action( 'wp_ajax_toggle_multisite', 'sh_cd_ajax_toggle_multisite' ); | |
| 152 | + | |
| 153 | +/** | |
| 154 | +Ajax handler for saving shortcode inline | |
| 155 | + **/ | |
| 156 | +function sh_cd_ajax_update_shortcode() { | |
| 157 | + | |
| 158 | + if ( false === SH_CD_IS_PREMIUM ) { | |
| 159 | + wp_send_json( 'not-premium' ); | |
| 160 | + } | |
| 161 | + | |
| 162 | + check_ajax_referer( 'sh-cd-security', 'security' ); | |
| 163 | + | |
| 164 | + sh_cd_permission_check(); | |
| 165 | + | |
| 166 | + $id = ( false === empty( $_POST['id'] ) ) ? (int) $_POST['id'] : NULL; | |
| 167 | + $content = ( false === empty( $_POST['content'] ) ) ? $_POST['content'] : ''; | |
| 168 | + | |
| 169 | + if ( false === empty( $id ) ) { | |
| 170 | + | |
| 171 | + $result = sh_cd_db_shortcodes_update_content( $id, $content ); | |
| 172 | + | |
| 173 | + wp_send_json( [ 'id' => $id, 'ok' => ( true === $result ) ? 1 : 0 ] ); | |
| 174 | + } | |
| 175 | + | |
| 176 | + wp_send_json( 'shortcode-not-found' ); | |
| 177 | +} | |
| 178 | +add_action( 'wp_ajax_update_shortcode', 'sh_cd_ajax_update_shortcode' ); | |
| 179 | + | |
| 180 | +/** | |
| 181 | +Ajax handler for adding shortcode inline | |
| 182 | + **/ | |
| 183 | +function sh_cd_ajax_add_shortcode() { | |
| 184 | + | |
| 185 | + if ( false === SH_CD_IS_PREMIUM ) { | |
| 186 | + wp_send_json( 'not-premium' ); | |
| 187 | + } | |
| 188 | + | |
| 189 | + check_ajax_referer( 'sh-cd-security', 'security' ); | |
| 190 | + | |
| 191 | + sh_cd_permission_check(); | |
| 192 | + | |
| 193 | + if( true === empty( $_POST['slug'] ) ) { | |
| 194 | + wp_send_json( [ 'id' => 0, 'ok' => 0, 'error_message' => __( 'Error: Please specify a "Slug".', SH_CD_SLUG ) ] ); | |
| 195 | + } | |
| 196 | + | |
| 197 | + if( true === empty( $_POST['content'] ) ) { | |
| 198 | + wp_send_json( [ 'id' => 0, 'ok' => 0, 'error_message' => __( 'Error: Please add something for "Content".', SH_CD_SLUG ) ] ); | |
| 199 | + } | |
| 200 | + | |
| 201 | + $shortcode = [ 'slug' => $_POST['slug'], | |
| 202 | + 'previous_slug' => '', | |
| 203 | + 'data' => $_POST['content'], | |
| 204 | + 'disabled' => ! sh_cd_to_bool( $_POST[ 'enabled' ] ), | |
| 205 | + 'multisite' => sh_cd_to_bool( $_POST[ 'multisite' ] ) | |
| 206 | + ]; | |
| 207 | + | |
| 208 | + $result = sh_cd_db_shortcodes_save( $shortcode, true ); | |
| 209 | + | |
| 210 | + wp_send_json( [ 'shortcode' => $result, 'ok' => ( false !== $result ), 'error_message' => __( 'Error: There was an error when saving your shortcode.', SH_CD_SLUG ) ] ); | |
| 211 | + | |
| 212 | +} | |
| 213 | +add_action( 'wp_ajax_add_shortcode', 'sh_cd_ajax_add_shortcode' ); | |
| 214 | + | |
| 215 | +/** | |
| 216 | + * Replace shortcodes within menu titles. | |
| 217 | + * | |
| 218 | + * @param $title | |
| 219 | + * @return mixed | |
| 220 | + */ | |
| 221 | +function sh_cd_menu_replace_shortcodes( $title ) { | |
| 222 | + return do_shortcode( $title ); | |
| 223 | +} | |
| 224 | +add_filter('nav_menu_item_title', 'sh_cd_menu_replace_shortcodes', 10, 1); | |