| 1 |
<?php |
| 2 |
/** |
| 3 |
* Oxygen Builder Integration for Smart Post Show. |
| 4 |
* |
| 5 |
* @link https://shapedplugin.com/ |
| 6 |
* |
| 7 |
* @package Smart_Post_Show |
| 8 |
* @author ShapedPlugin <[email protected]> |
| 9 |
*/ |
| 10 |
|
| 11 |
use SmartPostShow\Blocks\Helper; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; // Exit if accessed directly. |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Smart Post Oxygen Element Class. |
| 19 |
* |
| 20 |
* Integrates Smart Post Show with Oxygen page builder. |
| 21 |
* |
| 22 |
* @since 4.0.0 |
| 23 |
*/ |
| 24 |
class SmartPostOxygenElement extends OxyEl { |
| 25 |
|
| 26 |
|
| 27 |
/** |
| 28 |
* Get the element name. |
| 29 |
* |
| 30 |
* @return string Element name. |
| 31 |
*/ |
| 32 |
public function name() { |
| 33 |
return __( 'Saved Templates', 'post-carousel' ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Get the element slug. |
| 38 |
* |
| 39 |
* @return string Element slug. |
| 40 |
*/ |
| 41 |
public function slug() { |
| 42 |
return 'smart-post-templates'; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Get the element icon URL. |
| 47 |
* |
| 48 |
* @return string Icon URL. |
| 49 |
*/ |
| 50 |
public function icon() { |
| 51 |
return SP_PC_URL . 'admin/page-builder/oxygen/icon.svg'; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Get the button priority. |
| 56 |
* |
| 57 |
* @return int Button priority. |
| 58 |
*/ |
| 59 |
public function button_priority() { |
| 60 |
return 9; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Get the element HTML tag. |
| 65 |
* |
| 66 |
* @return string HTML tag name. |
| 67 |
*/ |
| 68 |
public function tag() { |
| 69 |
return 'div'; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Enable full CSS for the element. |
| 74 |
* |
| 75 |
* @return bool True to enable full CSS. |
| 76 |
*/ |
| 77 |
public function enableFullCSS() { |
| 78 |
return true; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Callback after element initialization. |
| 83 |
* |
| 84 |
* Enqueues scripts for the page builder. |
| 85 |
* |
| 86 |
* @return void |
| 87 |
*/ |
| 88 |
public function afterInit() { |
| 89 |
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_page_builder_scripts' ) ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Enqueue page builder scripts. |
| 94 |
* |
| 95 |
* Registers and enqueues JavaScript files for the page builder functionality. |
| 96 |
* |
| 97 |
* @return void |
| 98 |
*/ |
| 99 |
public function enqueue_page_builder_scripts() { |
| 100 |
$scripts = array( |
| 101 |
'sp_pc_blocks_script_js' => 'blocks/assets/js/builder-scripts.js', |
| 102 |
'sp_pc_news_ticker_script' => 'blocks/assets/js/news-ticker.min.js', |
| 103 |
'sp_pc_video_gallery_slider' => 'blocks/assets/js/video-and-gallery-slider.min.js', |
| 104 |
'sp_pc_smart_search_script' => 'blocks/assets/js/smart-search.min.js', |
| 105 |
'sp_pc_smart_toc_script' => 'blocks/assets/js/table-of-content.min.js', |
| 106 |
); |
| 107 |
|
| 108 |
foreach ( $scripts as $handle => $path ) { |
| 109 |
wp_enqueue_script( |
| 110 |
$handle, |
| 111 |
SP_PC_URL . $path, |
| 112 |
array( 'jquery' ), |
| 113 |
SMART_POST_SHOW_VERSION, |
| 114 |
true |
| 115 |
); |
| 116 |
} |
| 117 |
|
| 118 |
// Add inline script to reinitialize after builder updates. |
| 119 |
wp_add_inline_script( |
| 120 |
'sp_pc_blocks_script_js', |
| 121 |
'jQuery(document).ready(function($) { |
| 122 |
// Re-initialize when Oxygen builder updates elements |
| 123 |
$(document).on("oxygen-ajax-element-loaded", function() { |
| 124 |
if (typeof window.init === "function") { |
| 125 |
window.init(); |
| 126 |
} |
| 127 |
}); |
| 128 |
});' |
| 129 |
); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Render the element output. |
| 134 |
* |
| 135 |
* Displays the Smart Post template content with optional CSS. |
| 136 |
* |
| 137 |
* @param array $options Element options. |
| 138 |
* @param array $defaults Default values. |
| 139 |
* @param string $content Inner content. |
| 140 |
* |
| 141 |
* @return void |
| 142 |
*/ |
| 143 |
public function render( $options, $defaults, $content ) { |
| 144 |
$template_id = isset( $options['template_id'] ) ? absint( $options['template_id'] ) : 0; |
| 145 |
|
| 146 |
// Validate template_id is a valid published post. |
| 147 |
if ( $template_id ) { |
| 148 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 149 |
if ( isset( $_GET['action'] ) && strpos( sanitize_key( $_GET['action'] ), 'oxy_render_oxy' ) !== false ) { |
| 150 |
|
| 151 |
$upload_dir = wp_upload_dir(); |
| 152 |
$css_file = $upload_dir['basedir'] . '/smart-post-show/static/sp-post-' . $template_id . '.css'; |
| 153 |
$css_url = $upload_dir['baseurl'] . '/smart-post-show/static/sp-post-' . $template_id . '.css'; |
| 154 |
|
| 155 |
// Enqueue CSS file if exists. |
| 156 |
if ( file_exists( $css_file ) ) { |
| 157 |
echo '<link rel="stylesheet" href="' . esc_url( $css_url ) . '?v=' . filemtime( $css_file ) . '">'; // phpcs:ignore |
| 158 |
} |
| 159 |
|
| 160 |
$dynamic_css = get_post_meta( $template_id, '_sp_smart_css', true ); // phpcs:ignore WordPress.WP.DeprecatedParameters.Get_post_meta_param2 |
| 161 |
if ( ! empty( $dynamic_css ) ) { |
| 162 |
echo '<style>' . $dynamic_css . '</style>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 163 |
} |
| 164 |
} |
| 165 |
echo '<div class="sp-smart-post-builder-wrap" data-builderTemplateId="' . esc_attr( $template_id ) . '">'; |
| 166 |
echo do_shortcode( '[smart_post id="' . $template_id . '"]' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 167 |
echo '</div>'; |
| 168 |
|
| 169 |
} elseif ( isset( $_GET['action'] ) && strpos( sanitize_key( $_GET['action'] ), 'oxy_render_oxy' ) !== false ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 170 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 171 |
echo '<div style=" |
| 172 |
text-align: center; |
| 173 |
padding: 20px; |
| 174 |
border: 2px dashed #ccc; |
| 175 |
color: #999; |
| 176 |
font-size: 14px; |
| 177 |
"> |
| 178 |
Please Select a Smart Post Saved Templates |
| 179 |
</div>'; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Render element controls in Oxygen builder. |
| 185 |
* |
| 186 |
* Adds template selection dropdown control. |
| 187 |
* |
| 188 |
* @return void |
| 189 |
*/ |
| 190 |
public function controls() { |
| 191 |
$this->addOptionControl( |
| 192 |
array( |
| 193 |
'type' => 'dropdown', |
| 194 |
'name' => esc_html__( 'Select Your Template', 'post-carousel' ), |
| 195 |
'slug' => 'template_id', |
| 196 |
'default' => 'none', |
| 197 |
) |
| 198 |
)->setValue( Helper::get_save_template_list() )->rebuildElementOnChange(); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
new SmartPostOxygenElement(); |
| 203 |
|