| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Code Snippets. |
| 5 |
* |
| 6 |
* @package Merchant |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Code Snippets Class. |
| 15 |
* |
| 16 |
*/ |
| 17 |
class Merchant_Code_Snippets extends Merchant_Add_Module { |
| 18 |
|
| 19 |
/** |
| 20 |
* Module ID. |
| 21 |
* |
| 22 |
*/ |
| 23 |
const MODULE_ID = 'code-snippets'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Is module preview. |
| 27 |
* |
| 28 |
*/ |
| 29 |
public static $is_module_preview = false; |
| 30 |
|
| 31 |
/** |
| 32 |
* Constructor. |
| 33 |
* |
| 34 |
*/ |
| 35 |
public function __construct() { |
| 36 |
parent::__construct(); |
| 37 |
|
| 38 |
// Module section. |
| 39 |
$this->module_section = 'improve-experience'; |
| 40 |
|
| 41 |
// Module id. |
| 42 |
$this->module_id = self::MODULE_ID; |
| 43 |
|
| 44 |
// Module default settings. |
| 45 |
$this->module_default_settings = array(); |
| 46 |
|
| 47 |
// Mount preview url. |
| 48 |
$preview_url = site_url( '/' ); |
| 49 |
|
| 50 |
if ( function_exists( 'wc_get_page_id' ) ) { |
| 51 |
$preview_url = get_permalink( wc_get_page_id( 'shop' ) ); |
| 52 |
} |
| 53 |
|
| 54 |
// Module data. |
| 55 |
$this->module_data = Merchant_Admin_Modules::$modules_data[ self::MODULE_ID ]; |
| 56 |
$this->module_data[ 'preview_url' ] = $preview_url; |
| 57 |
|
| 58 |
// Module options path. |
| 59 |
$this->module_options_path = MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID . '/admin/options.php'; |
| 60 |
|
| 61 |
// Is module preview page. |
| 62 |
if ( is_admin() && parent::is_module_settings_page() ) { |
| 63 |
self::$is_module_preview = true; |
| 64 |
|
| 65 |
// Enqueue admin styles. |
| 66 |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_css' ) ); |
| 67 |
|
| 68 |
// Admin preview box. |
| 69 |
add_filter( 'merchant_module_preview', array( $this, 'render_admin_preview' ), 10, 2 ); |
| 70 |
} |
| 71 |
|
| 72 |
if ( ! Merchant_Modules::is_module_active( self::MODULE_ID ) ) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
// Return early if it's on admin but not in the respective module settings page. |
| 77 |
if ( is_admin() && ! parent::is_module_settings_page() ) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
// Snippets on head. |
| 82 |
add_action( 'wp_head', array( $this, 'header_code_snippet' ) ); |
| 83 |
|
| 84 |
// Snippets on body. |
| 85 |
add_action( 'wp_body_open', array( $this, 'body_code_snippet' ) ); |
| 86 |
|
| 87 |
// Snippets on footer. |
| 88 |
add_action( 'wp_footer', array( $this, 'footer_code_snippet' ), 99 ); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Admin enqueue CSS. |
| 93 |
* |
| 94 |
* @return void |
| 95 |
*/ |
| 96 |
public function admin_enqueue_css() { |
| 97 |
$page = ( ! empty( $_GET['page'] ) ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 98 |
$module = ( ! empty( $_GET['module'] ) ) ? sanitize_text_field( wp_unslash( $_GET['module'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 99 |
|
| 100 |
if ( 'merchant' === $page && self::MODULE_ID === $module ) { |
| 101 |
wp_enqueue_style( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', array(), MERCHANT_VERSION ); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Render admin preview |
| 107 |
* |
| 108 |
* @param Merchant_Admin_Preview $preview |
| 109 |
* @param string $module |
| 110 |
* |
| 111 |
* @return Merchant_Admin_Preview |
| 112 |
*/ |
| 113 |
public function render_admin_preview( $preview, $module ) { |
| 114 |
if ( self::MODULE_ID === $module ) { |
| 115 |
ob_start(); |
| 116 |
self::admin_preview_content(); |
| 117 |
$content = ob_get_clean(); |
| 118 |
|
| 119 |
// HTML. |
| 120 |
$preview->set_html( $content ); |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
return $preview; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Admin preview content. |
| 129 |
* |
| 130 |
* @return void |
| 131 |
*/ |
| 132 |
public function admin_preview_content() { |
| 133 |
?> |
| 134 |
|
| 135 |
<div class="mrc-code-snippets-preview"> |
| 136 |
<img src="<?php echo esc_url( MERCHANT_URI . 'inc/modules/' . self::MODULE_ID . '/admin/images/preview-code-snippets.png' ); ?>" /> |
| 137 |
</div> |
| 138 |
|
| 139 |
<?php |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Get header code snippet. |
| 144 |
* |
| 145 |
* @return void |
| 146 |
*/ |
| 147 |
function header_code_snippet() { |
| 148 |
$snippet = Merchant_Option::get( 'code-snippets', 'header_code_snippets', '' ); |
| 149 |
|
| 150 |
if ( ! empty( $snippet ) ) { |
| 151 |
echo wp_kses( $snippet, merchant_kses_allowed_tags_for_code_snippets() ); |
| 152 |
} |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Get body code snippet. |
| 158 |
* |
| 159 |
* @return void |
| 160 |
*/ |
| 161 |
function body_code_snippet() { |
| 162 |
$snippet = Merchant_Option::get( 'code-snippets', 'body_code_snippets', '' ); |
| 163 |
|
| 164 |
if ( ! empty( $snippet ) ) { |
| 165 |
echo wp_kses( $snippet, merchant_kses_allowed_tags_for_code_snippets() ); |
| 166 |
} |
| 167 |
|
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Get footer code snippet. |
| 172 |
* |
| 173 |
* @return void |
| 174 |
*/ |
| 175 |
function footer_code_snippet() { |
| 176 |
$snippet = Merchant_Option::get( 'code-snippets', 'footer_code_snippets', '' ); |
| 177 |
|
| 178 |
if ( ! empty( $snippet ) ) { |
| 179 |
echo wp_kses( $snippet, merchant_kses_allowed_tags_for_code_snippets() ); |
| 180 |
} |
| 181 |
|
| 182 |
} |
| 183 |
|
| 184 |
} |
| 185 |
|
| 186 |
// Initialize the module. |
| 187 |
add_action( 'init', function() { |
| 188 |
new Merchant_Code_Snippets(); |
| 189 |
} ); |
| 190 |
|