| 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 |
$id = ( false === empty( $_POST['id'] ) ) ? (int) $_POST['id'] : NULL; |
| 91 |
|
| 92 |
if ( false === empty( $id ) ) { |
| 93 |
|
| 94 |
$new_status = sh_cd_toggle_status( $id ); |
| 95 |
|
| 96 |
wp_send_json( [ 'id' => $id, 'status' => $new_status, 'ok' => 1 ] ); |
| 97 |
} |
| 98 |
|
| 99 |
wp_send_json( 'shortcode-not-found' ); |
| 100 |
} |
| 101 |
add_action( 'wp_ajax_toggle_status', 'sh_cd_ajax_toggle_status' ); |
| 102 |
|
| 103 |
/** |
| 104 |
Ajax handler for deleting a shortcode |
| 105 |
**/ |
| 106 |
function sh_cd_ajax_delete_shortcode() { |
| 107 |
|
| 108 |
check_ajax_referer( 'sh-cd-security', 'security' ); |
| 109 |
|
| 110 |
$id = ( false === empty( $_POST['id'] ) ) ? (int) $_POST['id'] : NULL; |
| 111 |
|
| 112 |
if ( false === empty( $id ) ) { |
| 113 |
|
| 114 |
$result = sh_cd_db_shortcodes_delete( $id ); |
| 115 |
|
| 116 |
wp_send_json( [ 'id' => $id, 'ok' => $result ] ); |
| 117 |
} |
| 118 |
|
| 119 |
wp_send_json( 'shortcode-not-found' ); |
| 120 |
} |
| 121 |
add_action( 'wp_ajax_delete_shortcode', 'sh_cd_ajax_delete_shortcode' ); |
| 122 |
|
| 123 |
/** |
| 124 |
Ajax handler for toggling disable status of a shortcode |
| 125 |
**/ |
| 126 |
function sh_cd_ajax_toggle_multisite() { |
| 127 |
|
| 128 |
if ( false === SH_CD_IS_PREMIUM ) { |
| 129 |
wp_send_json( 'not-premium' ); |
| 130 |
} |
| 131 |
|
| 132 |
check_ajax_referer( 'sh-cd-security', 'security' ); |
| 133 |
|
| 134 |
$id = ( false === empty( $_POST['id'] ) ) ? (int) $_POST['id'] : NULL; |
| 135 |
|
| 136 |
if ( false === empty( $id ) ) { |
| 137 |
|
| 138 |
$new_multisite = sh_cd_toggle_multisite( $id ); |
| 139 |
|
| 140 |
wp_send_json( [ 'id' => $id, 'multisite' => $new_multisite, 'ok' => 1 ] ); |
| 141 |
} |
| 142 |
|
| 143 |
wp_send_json( 'shortcode-not-found' ); |
| 144 |
} |
| 145 |
add_action( 'wp_ajax_toggle_multisite', 'sh_cd_ajax_toggle_multisite' ); |
| 146 |
|
| 147 |
/** |
| 148 |
Ajax handler for saving shortcode inline |
| 149 |
**/ |
| 150 |
function sh_cd_ajax_update_shortcode() { |
| 151 |
|
| 152 |
if ( false === SH_CD_IS_PREMIUM ) { |
| 153 |
wp_send_json( 'not-premium' ); |
| 154 |
} |
| 155 |
|
| 156 |
check_ajax_referer( 'sh-cd-security', 'security' ); |
| 157 |
|
| 158 |
$id = ( false === empty( $_POST['id'] ) ) ? (int) $_POST['id'] : NULL; |
| 159 |
$content = ( false === empty( $_POST['content'] ) ) ? $_POST['content'] : ''; |
| 160 |
|
| 161 |
if ( false === empty( $id ) ) { |
| 162 |
|
| 163 |
$result = sh_cd_db_shortcodes_update_content( $id, $content ); |
| 164 |
|
| 165 |
wp_send_json( [ 'id' => $id, 'ok' => ( true === $result ) ? 1 : 0 ] ); |
| 166 |
} |
| 167 |
|
| 168 |
wp_send_json( 'shortcode-not-found' ); |
| 169 |
} |
| 170 |
add_action( 'wp_ajax_update_shortcode', 'sh_cd_ajax_update_shortcode' ); |
| 171 |
|
| 172 |
/** |
| 173 |
Ajax handler for adding shortcode inline |
| 174 |
**/ |
| 175 |
function sh_cd_ajax_add_shortcode() { |
| 176 |
|
| 177 |
if ( false === SH_CD_IS_PREMIUM ) { |
| 178 |
wp_send_json( 'not-premium' ); |
| 179 |
} |
| 180 |
|
| 181 |
check_ajax_referer( 'sh-cd-security', 'security' ); |
| 182 |
|
| 183 |
if( true === empty( $_POST['slug'] ) ) { |
| 184 |
wp_send_json( [ 'id' => 0, 'ok' => 0, 'error_message' => __( 'Error: Please specify a "Slug".', SH_CD_SLUG ) ] ); |
| 185 |
} |
| 186 |
|
| 187 |
if( true === empty( $_POST['content'] ) ) { |
| 188 |
wp_send_json( [ 'id' => 0, 'ok' => 0, 'error_message' => __( 'Error: Please add something for "Content".', SH_CD_SLUG ) ] ); |
| 189 |
} |
| 190 |
|
| 191 |
$shortcode = [ 'slug' => $_POST['slug'], |
| 192 |
'previous_slug' => '', |
| 193 |
'data' => $_POST['content'], |
| 194 |
'disabled' => ! sh_cd_to_bool( $_POST[ 'enabled' ] ), |
| 195 |
'multisite' => sh_cd_to_bool( $_POST[ 'multisite' ] ) |
| 196 |
]; |
| 197 |
|
| 198 |
$result = sh_cd_db_shortcodes_save( $shortcode, true ); |
| 199 |
|
| 200 |
wp_send_json( [ 'shortcode' => $result, 'ok' => ( false !== $result ), 'error_message' => __( 'Error: There was an error when saving your shortcode.', SH_CD_SLUG ) ] ); |
| 201 |
|
| 202 |
} |
| 203 |
add_action( 'wp_ajax_add_shortcode', 'sh_cd_ajax_add_shortcode' ); |
| 204 |
|
| 205 |
/** |
| 206 |
* Replace shortcodes within menu titles. |
| 207 |
* |
| 208 |
* @param $title |
| 209 |
* @return mixed |
| 210 |
*/ |
| 211 |
function sh_cd_menu_replace_shortcodes( $title ) { |
| 212 |
return do_shortcode( $title ); |
| 213 |
} |
| 214 |
add_filter('nav_menu_item_title', 'sh_cd_menu_replace_shortcodes', 10, 1); |
| 215 |
|