| 1 |
<?php |
| 2 |
/** |
| 3 |
* Elementor Template widget file. |
| 4 |
* |
| 5 |
* @package Smart_Post_Show_Pro |
| 6 |
* @subpackage Smart_Post_Show_Pro/admin/ElementAddons |
| 7 |
*/ |
| 8 |
|
| 9 |
use SmartPostShow\Blocks\Helper; |
| 10 |
|
| 11 |
/** |
| 12 |
* Elementor smart post show pro Template Widget. |
| 13 |
* |
| 14 |
* @since 2.4.1 |
| 15 |
*/ |
| 16 |
class Smart_Post_Show_Template_Widget extends \Elementor\Widget_Base { |
| 17 |
/** |
| 18 |
* Get widget name. |
| 19 |
* |
| 20 |
* @since 2.4.1 |
| 21 |
* @access public |
| 22 |
* |
| 23 |
* @return string Widget name. |
| 24 |
*/ |
| 25 |
public function get_name() { |
| 26 |
return 'smart_post_show_pro_template'; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Get widget title. |
| 31 |
* |
| 32 |
* @since 2.4.1 |
| 33 |
* @access public |
| 34 |
* |
| 35 |
* @return string Widget title. |
| 36 |
*/ |
| 37 |
public function get_title() { |
| 38 |
return __( 'Smart Post Saved Template', 'post-carousel' ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Get widget icon. |
| 43 |
* |
| 44 |
* @since 2.2.5 |
| 45 |
* @access public |
| 46 |
* |
| 47 |
* @return string Widget icon. |
| 48 |
*/ |
| 49 |
public function get_icon() { |
| 50 |
return 'sps-icon-elementor'; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Get widget categories. |
| 55 |
* |
| 56 |
* @since 2.2.5 |
| 57 |
* @access public |
| 58 |
* |
| 59 |
* @return array Widget categories. |
| 60 |
*/ |
| 61 |
public function get_categories() { |
| 62 |
return array( 'basic' ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Enqueue scripts for editor preview. |
| 67 |
* |
| 68 |
* @return array Script handles. |
| 69 |
*/ |
| 70 |
public function get_script_depends() { |
| 71 |
return array( 'sp_smart_post_blocks_script_js' ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Enqueue styles for editor preview. |
| 76 |
* |
| 77 |
* @return array Style handles. |
| 78 |
*/ |
| 79 |
public function get_style_depends() { |
| 80 |
return array( |
| 81 |
'sp_smart_post_blocks_css', |
| 82 |
'sp_smart_post_blocks_social_icons_style', |
| 83 |
'sp_smart_post_blocks_google_fonts', |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Controls register. |
| 89 |
* |
| 90 |
* @return void |
| 91 |
*/ |
| 92 |
protected function register_controls() { |
| 93 |
$this->start_controls_section( |
| 94 |
'content_section', |
| 95 |
array( |
| 96 |
'label' => __( 'Content', 'post-carousel' ), |
| 97 |
'tab' => \Elementor\Controls_Manager::TAB_CONTENT, |
| 98 |
) |
| 99 |
); |
| 100 |
|
| 101 |
$this->add_control( |
| 102 |
'sp_smart_post_show_pro_template', |
| 103 |
array( |
| 104 |
'label' => __( 'Saved Template', 'post-carousel' ), |
| 105 |
'type' => \Elementor\Controls_Manager::SELECT2, |
| 106 |
'label_block' => true, |
| 107 |
'default' => '0', |
| 108 |
'options' => Helper::get_save_template_list(), |
| 109 |
) |
| 110 |
); |
| 111 |
|
| 112 |
// Edit This Template button. |
| 113 |
$this->add_control( |
| 114 |
'sp_pcp_edit_template', |
| 115 |
array( |
| 116 |
'type' => \Elementor\Controls_Manager::RAW_HTML, |
| 117 |
'raw' => $this->get_edit_template_button(), |
| 118 |
'content_classes' => 'sp-pcp-elementor-template-actions', |
| 119 |
) |
| 120 |
); |
| 121 |
|
| 122 |
$this->end_controls_section(); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Render the template content by querying the sp_post_template post |
| 127 |
* and applying the_content filter so Gutenberg blocks render properly. |
| 128 |
* |
| 129 |
* @since 2.4.1 |
| 130 |
* @param int $template_id Template post ID. |
| 131 |
* @return void |
| 132 |
*/ |
| 133 |
protected function render_template_content( $template_id ) { |
| 134 |
$args = array( |
| 135 |
'p' => $template_id, |
| 136 |
'post_type' => 'sp_post_template', |
| 137 |
'post_status' => 'publish', |
| 138 |
'posts_per_page' => 1, |
| 139 |
); |
| 140 |
$the_query = new \WP_Query( $args ); |
| 141 |
|
| 142 |
$dynamic_css = get_post_meta( $template_id, '_sp_smart_css', true ); // phpcs:ignore WordPress.WP.DeprecatedParameters.Get_post_meta_param2 |
| 143 |
|
| 144 |
if ( $the_query->have_posts() ) { |
| 145 |
echo '<style>' . $dynamic_css . '</style>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 146 |
echo '<div class="sp-smart-post-wrapper" data-postid="' . esc_attr( $template_id ) . '">'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 147 |
while ( $the_query->have_posts() ) { |
| 148 |
$the_query->the_post(); |
| 149 |
echo apply_filters( 'the_content', get_the_content() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 150 |
} |
| 151 |
echo '</div>'; |
| 152 |
wp_reset_postdata(); |
| 153 |
} else { |
| 154 |
echo '<div style="text-align: center; padding: 10px;">' |
| 155 |
. esc_html__( 'Template not found.', 'post-carousel' ) |
| 156 |
. '</div>'; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Render smart post show pro shortcode widget output on the frontend. |
| 162 |
* |
| 163 |
* @since 2.4.1 |
| 164 |
* @access protected |
| 165 |
*/ |
| 166 |
protected function render() { |
| 167 |
|
| 168 |
$settings = $this->get_settings_for_display(); |
| 169 |
$sp_pcp_template = $settings['sp_smart_post_show_pro_template'] ?? ''; |
| 170 |
$template_id = (int) $sp_pcp_template; |
| 171 |
|
| 172 |
if ( empty( $template_id ) ) { |
| 173 |
echo '<div style=" |
| 174 |
text-align: center; |
| 175 |
padding: 20px; |
| 176 |
border: 2px dashed #ccc; |
| 177 |
color: #999; |
| 178 |
font-size: 14px; |
| 179 |
"> |
| 180 |
Please select a Saved Template |
| 181 |
</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 182 |
return; |
| 183 |
} |
| 184 |
|
| 185 |
if ( $template_id ) { |
| 186 |
|
| 187 |
if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) { |
| 188 |
|
| 189 |
$upload_dir = wp_upload_dir(); |
| 190 |
$css_file = $upload_dir['basedir'] . '/smart-post-show/static/sp-post-' . $template_id . '.css'; |
| 191 |
$css_url = $upload_dir['baseurl'] . '/smart-post-show/static/sp-post-' . $template_id . '.css'; |
| 192 |
|
| 193 |
if ( file_exists( $css_file ) ) { |
| 194 |
echo '<link rel="stylesheet" href="' . esc_url( $css_url ) . '?v=' . filemtime( $css_file ) . '">'; // phpcs:ignore |
| 195 |
} |
| 196 |
|
| 197 |
$dynamic_css = get_post_meta( $template_id, '_sp_smart_css', true ); // phpcs:ignore WordPress.WP.DeprecatedParameters.Get_post_meta_param2 |
| 198 |
|
| 199 |
echo '<style>' . $dynamic_css . '</style>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 200 |
|
| 201 |
echo do_shortcode( '[smart_post id="' . $template_id . '"]' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 202 |
|
| 203 |
?> |
| 204 |
|
| 205 |
<?php |
| 206 |
|
| 207 |
} else { |
| 208 |
echo '<div class="sp-smart-post-builder-wrap" data-builderTemplateId="' . esc_attr( $template_id ) . '">'; |
| 209 |
echo do_shortcode( '[smart_post id="' . $template_id . '"]' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 210 |
echo '</div>'; |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Get edit template and add new template buttons HTML. |
| 217 |
* |
| 218 |
* @since 2.5.0 |
| 219 |
* |
| 220 |
* @return string Buttons HTML. |
| 221 |
*/ |
| 222 |
protected function get_edit_template_button() { |
| 223 |
$template_url = admin_url( 'edit.php?post_type=sp_post_template&page=sp_post_carousel&page=pcp_help#savedTemplate' ); |
| 224 |
|
| 225 |
$new_template_url = admin_url( 'post-new.php?post_type=sp_post_template&spblock_inserter' ); |
| 226 |
|
| 227 |
ob_start(); |
| 228 |
?> |
| 229 |
|
| 230 |
<div class="sp-pcp-elementor-template-buttons"> |
| 231 |
<a class="sp-pcp-edit-template-btn" href="<?php echo esc_url( $template_url ); ?>" style="color:#fff; background-color:#3e3e40; padding:12px 24px; border-radius:4px; display:inline-block; font-size: 14px" onmouseover="this.style.backgroundColor='#4b4b4d'" onmouseout="this.style.backgroundColor='#3e3e40'"> |
| 232 |
<span style="display:inline-block; transform: rotate(70deg); margin-right: 4px">✎</span> |
| 233 |
<span><?php echo esc_html__( 'Edit This Template', 'post-carousel' ); ?></span> |
| 234 |
</a> |
| 235 |
<a href="<?php echo esc_url( $new_template_url ); ?>" class="sp-pcp-add-template-btn" style="color:#fff; background-color:#641DD7; padding: 10px 23px; border-radius:4px; display:inline-block; margin-top: 15px; font-size: 14px" onmouseover="this.style.backgroundColor='#641DD7'" onmouseout="this.style.backgroundColor='#5a18c4'"> |
| 236 |
<span style="display:inline-block; font-size: 18px; margin-right: 4px;">+</span> |
| 237 |
<span><?php echo esc_html__( 'Add New Template', 'post-carousel' ); ?></span> |
| 238 |
</a> |
| 239 |
</div> |
| 240 |
<?php |
| 241 |
return ob_get_clean(); |
| 242 |
} |
| 243 |
} |
| 244 |
|