| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin class. |
| 4 |
* This is core class which is responsible for admin functionality. |
| 5 |
* |
| 6 |
* @version 4.0.0 |
| 7 |
* @author Expresstech System |
| 8 |
* |
| 9 |
* @package responsive-menu |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace RMP\Features\Inc; |
| 13 |
|
| 14 |
use RMP\Features\Inc\Traits\Singleton; |
| 15 |
use RMP\Features\Inc\RMP_Menu; |
| 16 |
use RMP\Features\Inc\Theme_Manager; |
| 17 |
use RMP\Features\Inc\Option_Manager; |
| 18 |
|
| 19 |
// Disable the direct access to this class. |
| 20 |
if ( ! defined( 'ABSPATH' ) ) { |
| 21 |
exit; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Class Admin |
| 26 |
*/ |
| 27 |
class Admin { |
| 28 |
|
| 29 |
use Singleton; |
| 30 |
|
| 31 |
/** |
| 32 |
* Instance of Option Manager class. |
| 33 |
* |
| 34 |
* @since 4.0.0 |
| 35 |
* @access protected |
| 36 |
* @var object. |
| 37 |
*/ |
| 38 |
protected static $option_manager; |
| 39 |
|
| 40 |
/** |
| 41 |
* Construct method. |
| 42 |
*/ |
| 43 |
protected function __construct() { |
| 44 |
$this->setup_hooks(); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* To setup action/filter. |
| 49 |
* |
| 50 |
* @return void |
| 51 |
*/ |
| 52 |
protected function setup_hooks() { |
| 53 |
add_action( 'wp_ajax_rmp_save_global_settings', array( $this, 'save_menu_global_settings' ) ); |
| 54 |
add_action( 'wp_ajax_rmp_create_new_menu', array( $this, 'create_new_menu' ) ); |
| 55 |
add_action( 'wp_ajax_rmp_export_menu', array( $this, 'rmp_export_menu' ) ); |
| 56 |
add_action( 'wp_ajax_rmp_import_menu', array( $this, 'rmp_import_menu' ) ); |
| 57 |
|
| 58 |
add_shortcode( 'rmp_menu', array( $this, 'register_menu_shortcode' ) ); |
| 59 |
add_shortcode( 'responsive_menu', array( $this, 'responsive_menu_shortcode' ) ); |
| 60 |
add_action( 'init', array( $this, 'rmp_menu_cpt' ), 0 ); |
| 61 |
|
| 62 |
add_filter( 'post_row_actions', array( $this, 'rmp_menu_row_actions' ), 10, 2 ); |
| 63 |
add_filter( 'get_edit_post_link', array( $this, 'rmp_edit_post_link' ), 10, 2 ); |
| 64 |
|
| 65 |
add_filter( 'manage_rmp_menu_posts_columns', array( $this, 'set_custom_edit_menu_columns' ) ); |
| 66 |
add_action( 'manage_rmp_menu_posts_custom_column', array( $this, 'add_custom_columns' ), 10, 2 ); |
| 67 |
add_action( 'admin_footer', array( $this, 'add_new_menu_widget' ) ); |
| 68 |
add_action( 'admin_menu', array( $this, 'rmp_register_submenu_page' ) ); |
| 69 |
add_action( 'admin_menu', array( $this, 'remove_default_add_cpt_page' ) ); |
| 70 |
add_action( 'rmp_create_new_menu', array( $this, 'set_global_options' ), 10, 0 ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Function to save the global settings of setting page. |
| 75 |
* |
| 76 |
* @return json |
| 77 |
*/ |
| 78 |
public function save_menu_global_settings() { |
| 79 |
check_ajax_referer( 'rmp_nonce', 'ajax_nonce' ); |
| 80 |
if ( ! current_user_can( 'administrator' ) ) { |
| 81 |
wp_send_json_error( __( 'You can\'t edit global settings!', 'responsive-menu' ) ); |
| 82 |
} |
| 83 |
|
| 84 |
$options = array(); |
| 85 |
$form_data = isset( $_POST['form'] ) ? rm_sanitize_rec_array( wp_unslash( $_POST['form'] ) ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 86 |
wp_parse_str( $form_data, $options ); |
| 87 |
|
| 88 |
foreach ( $options as $key => $value ) { |
| 89 |
$options[ $key ] = sanitize_text_field( $value ); |
| 90 |
} |
| 91 |
|
| 92 |
update_option( 'rmp_global_setting_options', $options ); |
| 93 |
|
| 94 |
/** |
| 95 |
* Fires after global settings is saved. |
| 96 |
* |
| 97 |
* @since 4.0.0 |
| 98 |
* |
| 99 |
* @param array $option List of global settings. |
| 100 |
*/ |
| 101 |
do_action( 'rmp_save_global_settings', $options ); |
| 102 |
|
| 103 |
wp_send_json_success( __( 'Saved', 'responsive-menu' ) ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Function to create a new theme. |
| 108 |
* |
| 109 |
* @since 4.0.0 |
| 110 |
* |
| 111 |
* @return json |
| 112 |
*/ |
| 113 |
public function create_new_menu() { |
| 114 |
check_ajax_referer( 'rmp_nonce', 'ajax_nonce' ); |
| 115 |
|
| 116 |
if ( ! current_user_can( 'administrator' ) ) { |
| 117 |
wp_send_json_error( array( 'message' => __( 'You can not create menu !', 'responsive-menu' ) ) ); |
| 118 |
} |
| 119 |
|
| 120 |
$menu_name = isset( $_POST['menu_name'] ) ? sanitize_text_field( wp_unslash( $_POST['menu_name'] ) ) : ''; |
| 121 |
if ( empty( $menu_name ) ) { |
| 122 |
wp_send_json_error( array( 'message' => __( 'Enter the Menu name !', 'responsive-menu' ) ) ); |
| 123 |
} |
| 124 |
|
| 125 |
$menu_to_use = isset( $_POST['menu_to_use'] ) ? sanitize_text_field( wp_unslash( $_POST['menu_to_use'] ) ) : ''; |
| 126 |
if ( empty( $menu_to_use ) ) { |
| 127 |
wp_send_json_error( array( 'message' => __( 'Select menu to use !', 'responsive-menu' ) ) ); |
| 128 |
} |
| 129 |
|
| 130 |
$menu_to_hide = isset( $_POST['menu_to_hide'] ) ? sanitize_text_field( wp_unslash( $_POST['menu_to_hide'] ) ) : ''; |
| 131 |
|
| 132 |
$menu_theme = isset( $_POST['menu_theme'] ) ? sanitize_text_field( wp_unslash( $_POST['menu_theme'] ) ) : ''; |
| 133 |
|
| 134 |
$theme_type = isset( $_POST['theme_type'] ) ? sanitize_text_field( wp_unslash( $_POST['theme_type'] ) ) : ''; |
| 135 |
|
| 136 |
$menu_show_on = isset( $_POST['menu_show_on'] ) ? sanitize_text_field( wp_unslash( $_POST['menu_show_on'] ) ) : ''; |
| 137 |
|
| 138 |
$menu_show_on_pages = array(); |
| 139 |
if ( ! empty( $_POST['menu_show_on_pages'] ) && is_array( $_POST['menu_show_on_pages'] ) ) { |
| 140 |
$menu_show_on_pages = rm_sanitize_rec_array( wp_unslash( $_POST['menu_show_on_pages'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 141 |
} |
| 142 |
|
| 143 |
$theme_options = array(); |
| 144 |
|
| 145 |
// Get appropriate theme as per theme type and theme name. |
| 146 |
if ( ! empty( $theme_type ) && 'downloaded' == $theme_type ) { |
| 147 |
$theme_manager = Theme_Manager::get_instance(); |
| 148 |
$theme_options = $theme_manager->get_available_theme_settings( $menu_theme ); |
| 149 |
} elseif ( ! empty( $theme_type ) && 'template' == $theme_type ) { |
| 150 |
$theme_manager = Theme_Manager::get_instance(); |
| 151 |
$theme_options = $theme_manager->get_saved_theme_options( $menu_theme ); |
| 152 |
} else { |
| 153 |
$theme_options = rmp_get_default_options(); |
| 154 |
} |
| 155 |
|
| 156 |
// Create menu as post with rmp_menu cpt. |
| 157 |
$new_menu = array( |
| 158 |
'post_title' => wp_strip_all_tags( $menu_name ), |
| 159 |
'post_author' => get_current_user_id(), |
| 160 |
'post_status' => 'publish', |
| 161 |
'post_type' => 'rmp_menu', |
| 162 |
); |
| 163 |
|
| 164 |
$menu_id = wp_insert_post( $new_menu ); |
| 165 |
|
| 166 |
$new_options = array( |
| 167 |
'menu_name' => $menu_name, |
| 168 |
'menu_to_use' => $menu_to_use, |
| 169 |
'menu_theme' => $menu_theme, |
| 170 |
'theme_type' => $theme_type, |
| 171 |
'menu_display_on' => $menu_show_on, |
| 172 |
'menu_show_on_pages' => $menu_show_on_pages, |
| 173 |
'menu_id' => $menu_id, |
| 174 |
'menu_to_hide' => $menu_to_hide, |
| 175 |
); |
| 176 |
|
| 177 |
$new_options = array_merge( $theme_options, $new_options ); |
| 178 |
|
| 179 |
if ( ! empty( $menu_id ) ) { |
| 180 |
update_post_meta( $menu_id, 'rmp_menu_meta', $new_options ); |
| 181 |
|
| 182 |
/** |
| 183 |
* Fires when menu is created and options is saved. |
| 184 |
* |
| 185 |
* @param int $menu_id Menu ID. |
| 186 |
*/ |
| 187 |
do_action( 'rmp_create_new_menu', $menu_id ); |
| 188 |
|
| 189 |
wp_send_json_success( |
| 190 |
array( |
| 191 |
'message' => __( 'Menu is created successfully', 'responsive-menu' ), |
| 192 |
'customize_url' => sprintf( |
| 193 |
'%spost.php?post=%s&action=edit&editor=true', |
| 194 |
get_admin_url(), |
| 195 |
$menu_id |
| 196 |
), |
| 197 |
) |
| 198 |
); |
| 199 |
} else { |
| 200 |
wp_send_json_error( array( 'message' => __( 'Unable to create new Menu !', 'responsive-menu' ) ) ); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* This function register the shortcode for menu. |
| 206 |
* |
| 207 |
* @since 4.0.0 |
| 208 |
* |
| 209 |
* @param Array $atts Attributes List. |
| 210 |
* |
| 211 |
* @return HTML $output Menu contents. |
| 212 |
*/ |
| 213 |
public function register_menu_shortcode( $attrs = array() ) { |
| 214 |
$attrs = shortcode_atts( array( 'id' => '' ), $attrs ); |
| 215 |
|
| 216 |
$attrs = array_change_key_case( (array) $attrs, CASE_LOWER ); |
| 217 |
|
| 218 |
// Check given id is valid. |
| 219 |
if ( empty( $attrs['id'] ) ) { |
| 220 |
return __( 'Please pass menu id as attribute.', 'responsive-menu' ); |
| 221 |
} |
| 222 |
|
| 223 |
$menu_id = $attrs['id']; |
| 224 |
if ( 'publish' !== get_post_status( $menu_id ) ) { |
| 225 |
/* translators: %d: Menu id */ |
| 226 |
return sprintf( __( 'Shortcode with menu id %d is not published.', 'responsive-menu' ), esc_html( $menu_id ) ); |
| 227 |
} |
| 228 |
|
| 229 |
// Check shortcode option is activated or not. |
| 230 |
$option_manager = Option_Manager::get_instance(); |
| 231 |
$option = $option_manager->get_option( $menu_id, 'menu_display_on' ); |
| 232 |
|
| 233 |
if ( 'shortcode' !== $option ) { |
| 234 |
return __( 'Shortcode deactivated', 'responsive-menu' ); |
| 235 |
} |
| 236 |
|
| 237 |
ob_start(); |
| 238 |
|
| 239 |
$menu = new RMP_Menu( $menu_id ); |
| 240 |
$menu->build_menu(); |
| 241 |
|
| 242 |
return ob_get_clean(); |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* This function register the shortcode for responsive_menu. |
| 247 |
* |
| 248 |
* @since 4.1.7 |
| 249 |
* |
| 250 |
* @return HTML $output Menu contents. |
| 251 |
*/ |
| 252 |
public function responsive_menu_shortcode() { |
| 253 |
|
| 254 |
// Check shortcode option is activated or not. |
| 255 |
$options = Option_Manager::get_instance(); |
| 256 |
$menu_ids = get_all_rmp_menu_ids(); |
| 257 |
|
| 258 |
if ( ! empty( $menu_ids ) ) { |
| 259 |
foreach ( $menu_ids as $menu_id ) { |
| 260 |
|
| 261 |
$menu_show_on = $options->get_option( $menu_id, 'menu_display_on' ); |
| 262 |
if ( ! empty( $menu_show_on ) && 'shortcode' !== $menu_show_on ) { |
| 263 |
continue; |
| 264 |
} |
| 265 |
|
| 266 |
ob_start(); |
| 267 |
$menu = new RMP_Menu( $menu_id ); |
| 268 |
$menu->build_menu(); |
| 269 |
return ob_get_clean(); |
| 270 |
} |
| 271 |
} else { |
| 272 |
return __( 'Shortcode deactivated', 'responsive-menu' ); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Function to update the global options. |
| 278 |
* |
| 279 |
* @since 4.0.0 |
| 280 |
* |
| 281 |
* @return void |
| 282 |
*/ |
| 283 |
public function set_global_options() { |
| 284 |
$global_settings = get_option( 'rmp_global_setting_options' ); |
| 285 |
if ( empty( $global_settings ) ) { |
| 286 |
$default_options = rmp_global_default_setting_options(); |
| 287 |
update_option( 'rmp_global_setting_options', $default_options ); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Add sub menu pages in responsive menu admin. |
| 293 |
* |
| 294 |
* @since 4.0.0 |
| 295 |
*/ |
| 296 |
public function rmp_register_submenu_page() { |
| 297 |
add_submenu_page( |
| 298 |
'edit.php?post_type=rmp_menu', |
| 299 |
__( 'Settings', 'responsive-menu' ), |
| 300 |
__( 'Settings', 'responsive-menu' ), |
| 301 |
'manage_options', |
| 302 |
'settings', |
| 303 |
array( $this, 'rmp_global_settings_page' ) |
| 304 |
); |
| 305 |
|
| 306 |
add_submenu_page( |
| 307 |
'edit.php?post_type=rmp_menu', |
| 308 |
__( 'Themes', 'responsive-menu' ), |
| 309 |
__( 'Themes', 'responsive-menu' ), |
| 310 |
'manage_options', |
| 311 |
'themes', |
| 312 |
array( $this, 'rmp_theme_admin_page' ) |
| 313 |
); |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Add template to the themes page. |
| 318 |
* |
| 319 |
* @since 4.0.0 |
| 320 |
*/ |
| 321 |
public function rmp_theme_admin_page() { |
| 322 |
|
| 323 |
// Check user capabilities. |
| 324 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 325 |
return; |
| 326 |
} |
| 327 |
|
| 328 |
include_once RMP_PLUGIN_PATH_V4 . '/templates/rmp-themes.php'; |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* Add template to the setting page. |
| 333 |
* |
| 334 |
* @since 4.0.0 |
| 335 |
* |
| 336 |
* @return void |
| 337 |
*/ |
| 338 |
public function rmp_global_settings_page() { |
| 339 |
|
| 340 |
// Check user capabilities. |
| 341 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 342 |
return; |
| 343 |
} |
| 344 |
|
| 345 |
include_once RMP_PLUGIN_PATH_V4 . '/templates/rmp-settings.php'; |
| 346 |
} |
| 347 |
|
| 348 |
/** |
| 349 |
* Remove create new menu default link of rmp_menu post type. |
| 350 |
* |
| 351 |
* @since 4.0.0 |
| 352 |
* |
| 353 |
* @return void |
| 354 |
*/ |
| 355 |
public function remove_default_add_cpt_page() { |
| 356 |
remove_submenu_page( 'edit.php?post_type=rmp_menu', 'post-new.php?post_type=rmp_menu' ); |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* Function to add the new menu wizard template. |
| 361 |
* |
| 362 |
* @since 4.0.0 |
| 363 |
* |
| 364 |
* @return void |
| 365 |
*/ |
| 366 |
public function add_new_menu_widget() { |
| 367 |
$screen = get_current_screen(); |
| 368 |
if ( 'edit-rmp_menu' === $screen->id ) { |
| 369 |
include_once RMP_PLUGIN_PATH_V4 . '/templates/new-menu-wizard.php'; |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
/** |
| 374 |
* Function to change the edit label and url. |
| 375 |
* |
| 376 |
* @since 4.0.0 |
| 377 |
* |
| 378 |
* @param array $actions List of post row actions. |
| 379 |
* @param Object $post Post object |
| 380 |
* |
| 381 |
* @return array $actions |
| 382 |
*/ |
| 383 |
public function rmp_menu_row_actions( $actions, $post ) { |
| 384 |
if ( 'rmp_menu' == $post->post_type ) { |
| 385 |
$actions['edit'] = sprintf( |
| 386 |
'<a href="%s" aria-label="Edit">%s</a>', |
| 387 |
esc_url( get_edit_post_link( $post->ID ) ), |
| 388 |
__( 'Customize', 'responsive-menu' ) |
| 389 |
); |
| 390 |
} |
| 391 |
|
| 392 |
return $actions; |
| 393 |
} |
| 394 |
|
| 395 |
/** |
| 396 |
* Function to add the custom column. |
| 397 |
* |
| 398 |
* @since 4.0.0 |
| 399 |
* |
| 400 |
* @param array $columns List of columns. |
| 401 |
* |
| 402 |
* @return array $columns Edited columns list. |
| 403 |
*/ |
| 404 |
public function set_custom_edit_menu_columns( $columns ) { |
| 405 |
unset( $columns['date'] ); |
| 406 |
$columns['shortcode_place'] = __( 'Shortcode', 'responsive-menu' ); |
| 407 |
$columns['actions'] = __( 'Actions', 'responsive-menu' ); |
| 408 |
$columns['date'] = __( 'Date', 'responsive-menu' ); |
| 409 |
|
| 410 |
return $columns; |
| 411 |
} |
| 412 |
|
| 413 |
/** |
| 414 |
* Function to change the edit url of post type rmp_menu |
| 415 |
* |
| 416 |
* @since 4.0.0 |
| 417 |
* |
| 418 |
* @param string $url Post edit URL. |
| 419 |
* @param int $post_id Post ID |
| 420 |
* |
| 421 |
* @return string $url Edited post url URL |
| 422 |
*/ |
| 423 |
public function rmp_edit_post_link( $url, $post_id ) { |
| 424 |
if ( 'rmp_menu' === get_post_type() && current_user_can( 'edit_post', $post_id ) ) { |
| 425 |
$url = get_admin_url() . 'post.php?post=' . $post_id . '&action=edit&editor=true'; |
| 426 |
} |
| 427 |
|
| 428 |
return $url; |
| 429 |
} |
| 430 |
|
| 431 |
/** |
| 432 |
* Function to add the data to the custom columns for the rmp_menu post type. |
| 433 |
* |
| 434 |
* @since 4.0.0 |
| 435 |
* |
| 436 |
* @param string $column Column Name |
| 437 |
* @param int $post_id Post ID |
| 438 |
* |
| 439 |
* @return void |
| 440 |
*/ |
| 441 |
public function add_custom_columns( $column, $post_id ) { |
| 442 |
$option_manager = Option_Manager::get_instance(); |
| 443 |
|
| 444 |
switch ( $column ) { |
| 445 |
|
| 446 |
case 'actions': |
| 447 |
?><a href="<?php echo esc_url( get_edit_post_link( $post_id ) ); ?>" class="button" aria-label="Customize"><?php esc_html_e( 'Customize', 'responsive-menu' ); ?></a> |
| 448 |
<?php |
| 449 |
break; |
| 450 |
case 'shortcode_place': |
| 451 |
$option = $option_manager->get_option( $post_id, 'menu_display_on' ); |
| 452 |
if ( 'shortcode' === $option ) { |
| 453 |
?> |
| 454 |
<code>[rmp_menu id="<?php echo esc_attr( $post_id ); ?>"]</code> |
| 455 |
<?php |
| 456 |
} else { |
| 457 |
esc_html_e( 'Shortcode deactivated', 'responsive-menu' ); |
| 458 |
} |
| 459 |
break; |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
/** |
| 464 |
* Register rmp_menu custom post type. |
| 465 |
* |
| 466 |
* @since 4.0.0 |
| 467 |
*/ |
| 468 |
public function rmp_menu_cpt() { |
| 469 |
|
| 470 |
// Check user capabilities. |
| 471 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 472 |
return; |
| 473 |
} |
| 474 |
|
| 475 |
$labels = array( |
| 476 |
'name' => __( 'Responsive Menu', 'responsive-menu' ), |
| 477 |
'singular_name' => __( 'Responsive Menu', 'responsive-menu' ), |
| 478 |
'menu_name' => __( 'Responsive Menu', 'responsive-menu' ), |
| 479 |
'parent_item_colon' => __( 'Parent Menu', 'responsive-menu' ), |
| 480 |
'all_items' => __( 'Menus', 'responsive-menu' ), |
| 481 |
'view_item' => __( 'View Menu', 'responsive-menu' ), |
| 482 |
'add_new_item' => __( 'Add New Menu', 'responsive-menu' ), |
| 483 |
'add_new' => __( 'Create New Menu', 'responsive-menu' ), |
| 484 |
'edit_item' => __( 'Edit Menu', 'responsive-menu' ), |
| 485 |
'update_item' => __( 'Update Menu', 'responsive-menu' ), |
| 486 |
'search_items' => __( 'Search Menu', 'responsive-menu' ), |
| 487 |
'not_found' => __( 'Not Found', 'responsive-menu' ), |
| 488 |
'not_found_in_trash' => __( 'Not found in Trash', 'responsive-menu' ), |
| 489 |
); |
| 490 |
|
| 491 |
$args = array( |
| 492 |
'label' => __( 'Responsive Menu', 'responsive-menu' ), |
| 493 |
'description' => __( 'Responsive Menu', 'responsive-menu' ), |
| 494 |
'labels' => $labels, |
| 495 |
'supports' => array( 'title', 'author' ), |
| 496 |
'public' => false, |
| 497 |
'hierarchical' => false, |
| 498 |
'show_ui' => true, |
| 499 |
'show_in_menu' => true, |
| 500 |
'show_in_nav_menus' => false, |
| 501 |
'show_in_admin_bar' => false, |
| 502 |
'has_archive' => false, |
| 503 |
'can_export' => false, |
| 504 |
'exclude_from_search' => true, |
| 505 |
'taxonomies' => array(), |
| 506 |
'publicly_queryable' => false, |
| 507 |
'capability_type' => 'post', |
| 508 |
'menu_icon' => RMP_PLUGIN_URL_V4 . '/assets/images/rmp-logo.png', |
| 509 |
); |
| 510 |
|
| 511 |
register_post_type( 'rmp_menu', $args ); |
| 512 |
|
| 513 |
/** |
| 514 |
* This action will be useful when need hooks after cpt register. |
| 515 |
* |
| 516 |
* @param CPT rmp_menu |
| 517 |
*/ |
| 518 |
do_action( 'rmp_after_cpt_registered', 'rmp_menu' ); |
| 519 |
} |
| 520 |
|
| 521 |
/** |
| 522 |
* Function to export the menu |
| 523 |
* |
| 524 |
* @since 4.0.0 |
| 525 |
* |
| 526 |
* @return json |
| 527 |
*/ |
| 528 |
public function rmp_export_menu() { |
| 529 |
check_ajax_referer( 'rmp_nonce', 'ajax_nonce' ); |
| 530 |
|
| 531 |
$menu_id = isset( $_POST['menu_id'] ) ? sanitize_text_field( wp_unslash( $_POST['menu_id'] ) ) : ''; |
| 532 |
|
| 533 |
if ( empty( $menu_id ) ) { |
| 534 |
wp_send_json_error( array( 'message' => __( 'Select menu !', 'responsive-menu' ) ) ); |
| 535 |
} |
| 536 |
|
| 537 |
if ( ! current_user_can( 'edit_post', $menu_id ) ) { |
| 538 |
wp_send_json_error( array( 'message' => __( 'You can not export menu !', 'responsive-menu' ) ) ); |
| 539 |
} |
| 540 |
|
| 541 |
$option_manager = Option_Manager::get_instance(); |
| 542 |
$option = $option_manager->get_options( $menu_id ); |
| 543 |
|
| 544 |
wp_send_json_success( wp_json_encode( $option ) ); |
| 545 |
} |
| 546 |
|
| 547 |
/** |
| 548 |
* Function to import the menu settings. |
| 549 |
* |
| 550 |
* @since 4.0.0 |
| 551 |
* |
| 552 |
* @return json |
| 553 |
*/ |
| 554 |
public function rmp_import_menu() { |
| 555 |
check_ajax_referer( 'rmp_nonce', 'ajax_nonce' ); |
| 556 |
|
| 557 |
if ( empty( $_FILES['file']['name'] ) ) { |
| 558 |
wp_send_json_error( array( 'message' => __( 'Please add file !', 'responsive-menu' ) ) ); |
| 559 |
} |
| 560 |
|
| 561 |
if ( empty( $_FILES['file']['type'] ) || 'application/json' != $_FILES['file']['type'] ) { |
| 562 |
wp_send_json_error( array( 'message' => __( 'Please add json file !', 'responsive-menu' ) ) ); |
| 563 |
} |
| 564 |
|
| 565 |
$menu_id = isset( $_POST['menu_id'] ) ? sanitize_text_field( wp_unslash( $_POST['menu_id'] ) ) : ''; |
| 566 |
if ( empty( $menu_id ) ) { |
| 567 |
wp_send_json_error( array( 'message' => __( 'Select menu !', 'responsive-menu' ) ) ); |
| 568 |
} |
| 569 |
|
| 570 |
if ( ! current_user_can( 'edit_post', $menu_id ) ) { |
| 571 |
wp_send_json_error( array( 'message' => __( 'You can not import menu !', 'responsive-menu' ) ) ); |
| 572 |
} |
| 573 |
|
| 574 |
global $wp_filesystem; |
| 575 |
if ( empty( $wp_filesystem ) ) { |
| 576 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 577 |
} |
| 578 |
WP_Filesystem(); |
| 579 |
|
| 580 |
$file_contents = isset( $_FILES['file']['tmp_name'] ) ? $wp_filesystem->get_contents( wp_unslash( $_FILES['file']['tmp_name'] ) ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 581 |
$import_options = json_decode( $file_contents, true ); |
| 582 |
|
| 583 |
if ( ! is_array( $import_options ) ) { |
| 584 |
wp_send_json_error( array( 'message' => __( 'Invalid import file format!', 'responsive-menu' ) ) ); |
| 585 |
} |
| 586 |
|
| 587 |
// Sanitize all imported options — treat imported JSON as untrusted input. |
| 588 |
// Use the same sanitization as the normal save path (rm_sanitize_rec_array with HTML allowed). |
| 589 |
$import_options = rm_sanitize_rec_array( $import_options, true ); |
| 590 |
|
| 591 |
$option_manager = Option_Manager::get_instance(); |
| 592 |
$exist_option = $option_manager->get_options( $menu_id ); |
| 593 |
|
| 594 |
// Some required options replced in imported settings with existing menu settings. |
| 595 |
$import_options['menu_name'] = $exist_option['menu_name']; |
| 596 |
$import_options['theme_type'] = 'default'; |
| 597 |
$import_options['menu_theme'] = null; |
| 598 |
$import_options['menu_to_use'] = $exist_option['menu_to_use']; |
| 599 |
$import_options['menu_to_use_in_mobile'] = $exist_option['menu_to_use_in_mobile']; |
| 600 |
|
| 601 |
update_post_meta( $menu_id, 'rmp_menu_meta', $import_options ); |
| 602 |
/** |
| 603 |
* Fires when menu is imported. |
| 604 |
* |
| 605 |
* @since 4.0.0 |
| 606 |
* |
| 607 |
* @param int $menu_id |
| 608 |
*/ |
| 609 |
do_action( 'rmp_import_menu', $menu_id ); |
| 610 |
|
| 611 |
wp_send_json_success( array( 'message' => __( 'Menu settings imported successfully!', 'responsive-menu' ) ) ); |
| 612 |
} |
| 613 |
} |
| 614 |
|