| 1 |
<?php |
| 2 |
/** |
| 3 |
* Preview class. |
| 4 |
* This class is responsible for preview related 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 |
|
| 17 |
// Disable the direct access to this class. |
| 18 |
if ( ! defined( 'ABSPATH' ) ) { |
| 19 |
exit; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Class Preview |
| 24 |
*/ |
| 25 |
class Preview { |
| 26 |
|
| 27 |
use Singleton; |
| 28 |
|
| 29 |
/** |
| 30 |
* Construct method. |
| 31 |
*/ |
| 32 |
protected function __construct() { |
| 33 |
$this->setup_hooks(); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* To setup action/filter. |
| 38 |
* |
| 39 |
* @version 4.0.0 |
| 40 |
* |
| 41 |
* @return void |
| 42 |
*/ |
| 43 |
protected function setup_hooks() { |
| 44 |
add_action( 'wp_ajax_rmp_enable_menu_item', array( $this, 'enable_menu_item' ) ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Menu elements the live preview can render, mapped to the RMP_Menu |
| 49 |
* method that renders each one. |
| 50 |
* |
| 51 |
* The keys match the `data-toggle` values on the item-order checkboxes in |
| 52 |
* templates/menu-elements/. |
| 53 |
* |
| 54 |
* @since 4.7.4 |
| 55 |
* |
| 56 |
* @var array |
| 57 |
*/ |
| 58 |
const PREVIEW_ELEMENTS = array( |
| 59 |
'menu' => 'menu', |
| 60 |
'search' => 'menu_search_box', |
| 61 |
'title' => 'menu_title', |
| 62 |
'social-icons' => 'menu_social_icons', |
| 63 |
'additional-content' => 'menu_additional_content', |
| 64 |
); |
| 65 |
|
| 66 |
/** |
| 67 |
* This function get the content of menu item for live preview element. |
| 68 |
* |
| 69 |
* @return HTML |
| 70 |
*/ |
| 71 |
public function enable_menu_item() { |
| 72 |
check_ajax_referer( 'rmp_nonce', 'ajax_nonce' ); |
| 73 |
|
| 74 |
$menu_id = isset( $_POST['menu_id'] ) ? absint( wp_unslash( $_POST['menu_id'] ) ) : 0; |
| 75 |
|
| 76 |
if ( empty( $menu_id ) ) { |
| 77 |
wp_send_json_error( array( 'message' => esc_html__( 'Menu ID missing !', 'responsive-menu' ) ) ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* The element name is a slug, not a number — intval() collapsed every |
| 82 |
* value except 'menu' to 0, so no branch below ever matched and the |
| 83 |
* preview always rendered the additional-content element. |
| 84 |
*/ |
| 85 |
$menu_element = isset( $_POST['menu_element'] ) ? sanitize_key( wp_unslash( $_POST['menu_element'] ) ) : ''; |
| 86 |
|
| 87 |
if ( ! isset( self::PREVIEW_ELEMENTS[ $menu_element ] ) ) { |
| 88 |
wp_send_json_error( array( 'message' => esc_html__( 'Unknown menu element !', 'responsive-menu' ) ) ); |
| 89 |
} |
| 90 |
|
| 91 |
$menu = new RMP_Menu( $menu_id ); |
| 92 |
$method = self::PREVIEW_ELEMENTS[ $menu_element ]; |
| 93 |
|
| 94 |
/** |
| 95 |
* Every render method echoes its markup and returns nothing, so the |
| 96 |
* output has to be captured — otherwise it is emitted before the JSON |
| 97 |
* body and the response is not parseable. |
| 98 |
*/ |
| 99 |
ob_start(); |
| 100 |
$menu->$method(); |
| 101 |
$html = ob_get_clean(); |
| 102 |
|
| 103 |
wp_send_json_success( array( 'markup' => $html ) ); |
| 104 |
} |
| 105 |
} |
| 106 |
|