PluginProbe ʕ •ᴥ•ʔ
ShopPress – Shop Builder for Elementor and WooCommerce / trunk
ShopPress – Shop Builder for Elementor and WooCommerce vtrunk
shop-press / Elementor / control / custom-css / CustomCSS.php
shop-press / Elementor / control / custom-css Last commit date
CustomCSS.php 2 weeks ago
CustomCSS.php
142 lines
1 <?php
2 /**
3 * Custom CSS Control.
4 *
5 * @package ShopPress
6 * @since 1.2.0
7 */
8
9 // Don't load directly.
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13
14 use Elementor\Base_Data_Control;
15 use Elementor\Plugin;
16
17 class ShopPressElementorCustomCSS extends Base_Data_Control {
18 /**
19 * Control type name.
20 *
21 * @since 1.2.0
22 */
23 public function get_type() {
24 return 'shoppress_custom_code';
25 }
26
27 /**
28 * Control enqueue live assets.
29 *
30 * @since 1.2.0
31 */
32 public function enqueue() {
33 // Use WordPress core CodeMirror editor.
34 $editor_settings = wp_enqueue_code_editor(
35 array(
36 'type' => 'text/css',
37 'codemirror' => array(
38 'indentUnit' => 4,
39 'tabSize' => 4,
40 'lineNumbers' => true,
41 'lineWrapping' => true,
42 'matchBrackets' => true,
43 'autoCloseTags' => true,
44 'autoCloseBrackets' => true,
45 ),
46 )
47 );
48
49 // Enqueue custom script to initialize CodeMirror for Elementor controls.
50 if ( false !== $editor_settings ) {
51 wp_enqueue_script(
52 'shoppress-codemirror-init',
53 SHOPPRESS_URL . 'public/admin/elementor/codemirror-init.js',
54 array( 'jquery', 'code-editor', 'elementor-editor' ),
55 SHOPPRESS_VERSION,
56 true
57 );
58 }
59 }
60
61 /**
62 * Control HTML structure.
63 *
64 * @since 1.2.0
65 */
66 public function content_template() {
67 $control_uid = $this->get_control_uid();
68 ?>
69 <div class="elementor-control-field">
70 <label for="<?php echo esc_attr( $control_uid ); ?>" class="elementor-control-title">{{{ data.label }}}</label>
71 <div class="sp-custom-css-field">
72 <form>
73 <textarea id="<?php echo esc_attr( $control_uid ); ?>" class="elementor-input-style" rows="{{ data.rows }}" data-setting="{{ data.name }}" placeholder="{{ data.placeholder }}"></textarea>
74 </form>
75 </div>
76 </div>
77 <# if ( data.description ) { #>
78 <div class="elementor-control-field-description">{{ data.description }}</div>
79 <# } #>
80 <?php
81 }
82 }
83
84 /**
85 * Add custom css control to widgets
86 *
87 * @param \ShopPress\Elementor\ShopPressWidgets $widget_class
88 *
89 * @since 1.2.0
90 *
91 * @return void
92 */
93 function shop_press_add_custom_css_control_to_widgets( $widget_class ) {
94
95 $widget_class->start_controls_section(
96 'shoppress_custom_css_section',
97 array(
98 'tab' => \Elementor\Controls_Manager::TAB_STYLE,
99 'label' => esc_html__( 'Custom CSS', 'shop-press' ),
100 )
101 );
102
103 $widget_class->add_control(
104 'shoppress_custom_css',
105 array(
106 'type' => 'shoppress_custom_code',
107 )
108 );
109
110 $widget_class->end_controls_section();
111 }
112
113 add_action( 'shoppress/elementor/widget/register_controls_init', 'shop_press_add_custom_css_control_to_widgets' );
114
115 function shoppress_elementor_custom_css_output( $settings ) {
116 shoppress_module_custom_css_editor( isset( $settings['shoppress_custom_css'] ) ? $settings['shoppress_custom_css'] : '' );
117 }
118 add_action( 'shoppress/widget/before_render', 'shoppress_elementor_custom_css_output', 10, 1 );
119
120
121 function shoppress_module_custom_css_editor( $custom_css ) {
122 if ( ! empty( $custom_css ) && Plugin::$instance->editor->is_edit_mode() ) {
123 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- CSS from trusted editor.
124 echo '<style>' . $custom_css . '</style>';
125 }
126 }
127
128
129 function shoppress_module_custom_css_frontend( $post_css, $element ) {
130 if ( $post_css instanceof Dynamic_CSS ) {
131 return;
132 }
133 $element_settings = $element->get_settings();
134 $custom_css = ! empty( $element_settings['shoppress_custom_css'] ) ? trim( $element_settings['shoppress_custom_css'] ) : '';
135 if ( ! empty( $custom_css ) ) {
136 $post_css->get_stylesheet()->add_raw_css( $custom_css );
137 } else {
138 return;
139 }
140 }
141 add_action( 'elementor/element/parse_css', 'shoppress_module_custom_css_frontend', 10, 2 );
142