@@ -1,262 +1,262 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | if ( ! defined( 'ABSPATH' ) ) { |
3 | - exit; |
|
3 | + exit; |
|
4 | 4 | } |
5 | 5 | |
6 | 6 | if ( ! class_exists( 'WP_Super_Duper' ) ) { |
7 | 7 | |
8 | 8 | |
9 | - /** |
|
10 | - * A Class to be able to create a Widget, Shortcode or Block to be able to output content for WordPress. |
|
11 | - * |
|
12 | - * Should not be called direct but extended instead. |
|
13 | - * |
|
14 | - * Class WP_Super_Duper |
|
15 | - * @since 1.0.16 change log moved to file change-log.txt - CHANGED |
|
16 | - * @ver 1.0.19 |
|
17 | - */ |
|
18 | - class WP_Super_Duper extends WP_Widget { |
|
19 | - |
|
20 | - public $version = "1.0.22"; |
|
21 | - public $font_awesome_icon_version = "5.11.2"; |
|
22 | - public $block_code; |
|
23 | - public $options; |
|
24 | - public $base_id; |
|
25 | - public $settings_hash; |
|
26 | - public $arguments = array(); |
|
27 | - public $instance = array(); |
|
28 | - private $class_name; |
|
29 | - |
|
30 | - /** |
|
31 | - * The relative url to the current folder. |
|
32 | - * |
|
33 | - * @var string |
|
34 | - */ |
|
35 | - public $url = ''; |
|
36 | - |
|
37 | - /** |
|
38 | - * Take the array options and use them to build. |
|
39 | - */ |
|
40 | - public function __construct( $options ) { |
|
41 | - global $sd_widgets; |
|
42 | - |
|
43 | - $sd_widgets[ $options['base_id'] ] = array( |
|
44 | - 'name' => $options['name'], |
|
45 | - 'class_name' => $options['class_name'] |
|
46 | - ); |
|
47 | - $this->base_id = $options['base_id']; |
|
48 | - // lets filter the options before we do anything |
|
49 | - $options = apply_filters( "wp_super_duper_options", $options ); |
|
50 | - $options = apply_filters( "wp_super_duper_options_{$this->base_id}", $options ); |
|
51 | - $options = $this->add_name_from_key( $options ); |
|
52 | - $this->options = $options; |
|
53 | - |
|
54 | - $this->base_id = $options['base_id']; |
|
55 | - $this->arguments = isset( $options['arguments'] ) ? $options['arguments'] : array(); |
|
56 | - |
|
57 | - // init parent |
|
58 | - parent::__construct( $options['base_id'], $options['name'], $options['widget_ops'] ); |
|
59 | - |
|
60 | - if ( isset( $options['class_name'] ) ) { |
|
61 | - // register widget |
|
62 | - $this->class_name = $options['class_name']; |
|
63 | - |
|
64 | - // register shortcode |
|
65 | - $this->register_shortcode(); |
|
66 | - |
|
67 | - // Fusion Builder (avada) support |
|
68 | - if ( function_exists( 'fusion_builder_map' ) ) { |
|
69 | - add_action( 'init', array( $this, 'register_fusion_element' ) ); |
|
70 | - } |
|
71 | - |
|
72 | - // register block |
|
73 | - add_action( 'admin_enqueue_scripts', array( $this, 'register_block' ) ); |
|
74 | - } |
|
75 | - |
|
76 | - // add the CSS and JS we need ONCE |
|
77 | - global $sd_widget_scripts; |
|
78 | - |
|
79 | - if ( ! $sd_widget_scripts ) { |
|
80 | - wp_add_inline_script( 'admin-widgets', $this->widget_js() ); |
|
81 | - wp_add_inline_script( 'customize-controls', $this->widget_js() ); |
|
82 | - wp_add_inline_style( 'widgets', $this->widget_css() ); |
|
83 | - |
|
84 | - // maybe add elementor editor styles |
|
85 | - add_action( 'elementor/editor/after_enqueue_styles', array( $this, 'elementor_editor_styles' ) ); |
|
86 | - |
|
87 | - $sd_widget_scripts = true; |
|
88 | - |
|
89 | - // add shortcode insert button once |
|
90 | - add_action( 'media_buttons', array( $this, 'shortcode_insert_button' ) ); |
|
91 | - // generatepress theme sections compatibility |
|
92 | - if ( function_exists( 'generate_sections_sections_metabox' ) ) { |
|
93 | - add_action( 'generate_sections_metabox', array( $this, 'shortcode_insert_button_script' ) ); |
|
94 | - } |
|
95 | - if ( $this->is_preview() ) { |
|
96 | - add_action( 'wp_footer', array( $this, 'shortcode_insert_button_script' ) ); |
|
97 | - // this makes the insert button work for elementor |
|
98 | - add_action( 'elementor/editor/after_enqueue_scripts', array( |
|
99 | - $this, |
|
100 | - 'shortcode_insert_button_script' |
|
101 | - ) ); // for elementor |
|
102 | - } |
|
103 | - // this makes the insert button work for cornerstone |
|
104 | - add_action( 'wp_print_footer_scripts', array( __CLASS__, 'maybe_cornerstone_builder' ) ); |
|
105 | - |
|
106 | - add_action( 'wp_ajax_super_duper_get_widget_settings', array( __CLASS__, 'get_widget_settings' ) ); |
|
107 | - add_action( 'wp_ajax_super_duper_get_picker', array( __CLASS__, 'get_picker' ) ); |
|
108 | - |
|
109 | - // add generator text to admin head |
|
110 | - add_action( 'admin_head', array( $this, 'generator' ) ); |
|
111 | - } |
|
112 | - |
|
113 | - do_action( 'wp_super_duper_widget_init', $options, $this ); |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Add our widget CSS to elementor editor. |
|
118 | - */ |
|
119 | - public function elementor_editor_styles() { |
|
120 | - wp_add_inline_style( 'elementor-editor', $this->widget_css( false ) ); |
|
121 | - } |
|
122 | - |
|
123 | - public function register_fusion_element() { |
|
124 | - |
|
125 | - $options = $this->options; |
|
126 | - |
|
127 | - if ( $this->base_id ) { |
|
128 | - |
|
129 | - $params = $this->get_fusion_params(); |
|
130 | - |
|
131 | - $args = array( |
|
132 | - 'name' => $options['name'], |
|
133 | - 'shortcode' => $this->base_id, |
|
134 | - 'icon' => $options['block-icon'] ? $options['block-icon'] : 'far fa-square', |
|
135 | - 'allow_generator' => true, |
|
136 | - ); |
|
137 | - |
|
138 | - if ( ! empty( $params ) ) { |
|
139 | - $args['params'] = $params; |
|
140 | - } |
|
141 | - |
|
142 | - fusion_builder_map( $args ); |
|
143 | - } |
|
144 | - |
|
145 | - } |
|
146 | - |
|
147 | - public function get_fusion_params() { |
|
148 | - $params = array(); |
|
149 | - $arguments = $this->get_arguments(); |
|
150 | - |
|
151 | - if ( ! empty( $arguments ) ) { |
|
152 | - foreach ( $arguments as $key => $val ) { |
|
153 | - $param = array(); |
|
154 | - // type |
|
155 | - $param['type'] = str_replace( |
|
156 | - array( |
|
157 | - "text", |
|
158 | - "number", |
|
159 | - "email", |
|
160 | - "color", |
|
161 | - "checkbox" |
|
162 | - ), |
|
163 | - array( |
|
164 | - "textfield", |
|
165 | - "textfield", |
|
166 | - "textfield", |
|
167 | - "colorpicker", |
|
168 | - "select", |
|
169 | - |
|
170 | - ), |
|
171 | - $val['type'] ); |
|
172 | - |
|
173 | - // multiselect |
|
174 | - if ( $val['type'] == 'multiselect' || ( ( $param['type'] == 'select' || $val['type'] == 'select' ) && ! empty( $val['multiple'] ) ) ) { |
|
175 | - $param['type'] = 'multiple_select'; |
|
176 | - $param['multiple'] = true; |
|
177 | - } |
|
178 | - |
|
179 | - // heading |
|
180 | - $param['heading'] = $val['title']; |
|
181 | - |
|
182 | - // description |
|
183 | - $param['description'] = isset( $val['desc'] ) ? $val['desc'] : ''; |
|
184 | - |
|
185 | - // param_name |
|
186 | - $param['param_name'] = $key; |
|
187 | - |
|
188 | - // Default |
|
189 | - $param['default'] = isset( $val['default'] ) ? $val['default'] : ''; |
|
190 | - |
|
191 | - // Group |
|
192 | - if ( isset( $val['group'] ) ) { |
|
193 | - $param['group'] = $val['group']; |
|
194 | - } |
|
195 | - |
|
196 | - // value |
|
197 | - if ( $val['type'] == 'checkbox' ) { |
|
198 | - if ( isset( $val['default'] ) && $val['default'] == '0' ) { |
|
199 | - unset( $param['default'] ); |
|
200 | - } |
|
201 | - $param['value'] = array( '' => __( "No" ), '1' => __( "Yes" ) ); |
|
202 | - } elseif ( $param['type'] == 'select' || $param['type'] == 'multiple_select' ) { |
|
203 | - $param['value'] = isset( $val['options'] ) ? $val['options'] : array(); |
|
204 | - } else { |
|
205 | - $param['value'] = isset( $val['default'] ) ? $val['default'] : ''; |
|
206 | - } |
|
207 | - |
|
208 | - // setup the param |
|
209 | - $params[] = $param; |
|
210 | - |
|
211 | - } |
|
212 | - } |
|
213 | - |
|
214 | - |
|
215 | - return $params; |
|
216 | - } |
|
217 | - |
|
218 | - /** |
|
219 | - * Maybe insert the shortcode inserter button in the footer if we are in the cornerstone builder |
|
220 | - */ |
|
221 | - public static function maybe_cornerstone_builder() { |
|
222 | - if ( did_action( 'cornerstone_before_boot_app' ) ) { |
|
223 | - self::shortcode_insert_button_script(); |
|
224 | - } |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * A function to ge the shortcode builder picker html. |
|
229 | - * |
|
230 | - * @param string $editor_id |
|
231 | - * |
|
232 | - * @return string |
|
233 | - */ |
|
234 | - public static function get_picker( $editor_id = '' ) { |
|
235 | - |
|
236 | - ob_start(); |
|
237 | - if ( isset( $_POST['editor_id'] ) ) { |
|
238 | - $editor_id = esc_attr( $_POST['editor_id'] ); |
|
239 | - } elseif ( isset( $_REQUEST['et_fb'] ) ) { |
|
240 | - $editor_id = 'main_content_content_vb_tiny_mce'; |
|
241 | - } |
|
242 | - |
|
243 | - global $sd_widgets; |
|
244 | - ?> |
|
9 | + /** |
|
10 | + * A Class to be able to create a Widget, Shortcode or Block to be able to output content for WordPress. |
|
11 | + * |
|
12 | + * Should not be called direct but extended instead. |
|
13 | + * |
|
14 | + * Class WP_Super_Duper |
|
15 | + * @since 1.0.16 change log moved to file change-log.txt - CHANGED |
|
16 | + * @ver 1.0.19 |
|
17 | + */ |
|
18 | + class WP_Super_Duper extends WP_Widget { |
|
19 | + |
|
20 | + public $version = "1.0.22"; |
|
21 | + public $font_awesome_icon_version = "5.11.2"; |
|
22 | + public $block_code; |
|
23 | + public $options; |
|
24 | + public $base_id; |
|
25 | + public $settings_hash; |
|
26 | + public $arguments = array(); |
|
27 | + public $instance = array(); |
|
28 | + private $class_name; |
|
29 | + |
|
30 | + /** |
|
31 | + * The relative url to the current folder. |
|
32 | + * |
|
33 | + * @var string |
|
34 | + */ |
|
35 | + public $url = ''; |
|
36 | + |
|
37 | + /** |
|
38 | + * Take the array options and use them to build. |
|
39 | + */ |
|
40 | + public function __construct( $options ) { |
|
41 | + global $sd_widgets; |
|
42 | + |
|
43 | + $sd_widgets[ $options['base_id'] ] = array( |
|
44 | + 'name' => $options['name'], |
|
45 | + 'class_name' => $options['class_name'] |
|
46 | + ); |
|
47 | + $this->base_id = $options['base_id']; |
|
48 | + // lets filter the options before we do anything |
|
49 | + $options = apply_filters( "wp_super_duper_options", $options ); |
|
50 | + $options = apply_filters( "wp_super_duper_options_{$this->base_id}", $options ); |
|
51 | + $options = $this->add_name_from_key( $options ); |
|
52 | + $this->options = $options; |
|
53 | + |
|
54 | + $this->base_id = $options['base_id']; |
|
55 | + $this->arguments = isset( $options['arguments'] ) ? $options['arguments'] : array(); |
|
56 | + |
|
57 | + // init parent |
|
58 | + parent::__construct( $options['base_id'], $options['name'], $options['widget_ops'] ); |
|
59 | + |
|
60 | + if ( isset( $options['class_name'] ) ) { |
|
61 | + // register widget |
|
62 | + $this->class_name = $options['class_name']; |
|
63 | + |
|
64 | + // register shortcode |
|
65 | + $this->register_shortcode(); |
|
66 | + |
|
67 | + // Fusion Builder (avada) support |
|
68 | + if ( function_exists( 'fusion_builder_map' ) ) { |
|
69 | + add_action( 'init', array( $this, 'register_fusion_element' ) ); |
|
70 | + } |
|
71 | + |
|
72 | + // register block |
|
73 | + add_action( 'admin_enqueue_scripts', array( $this, 'register_block' ) ); |
|
74 | + } |
|
75 | + |
|
76 | + // add the CSS and JS we need ONCE |
|
77 | + global $sd_widget_scripts; |
|
78 | + |
|
79 | + if ( ! $sd_widget_scripts ) { |
|
80 | + wp_add_inline_script( 'admin-widgets', $this->widget_js() ); |
|
81 | + wp_add_inline_script( 'customize-controls', $this->widget_js() ); |
|
82 | + wp_add_inline_style( 'widgets', $this->widget_css() ); |
|
83 | + |
|
84 | + // maybe add elementor editor styles |
|
85 | + add_action( 'elementor/editor/after_enqueue_styles', array( $this, 'elementor_editor_styles' ) ); |
|
86 | + |
|
87 | + $sd_widget_scripts = true; |
|
88 | + |
|
89 | + // add shortcode insert button once |
|
90 | + add_action( 'media_buttons', array( $this, 'shortcode_insert_button' ) ); |
|
91 | + // generatepress theme sections compatibility |
|
92 | + if ( function_exists( 'generate_sections_sections_metabox' ) ) { |
|
93 | + add_action( 'generate_sections_metabox', array( $this, 'shortcode_insert_button_script' ) ); |
|
94 | + } |
|
95 | + if ( $this->is_preview() ) { |
|
96 | + add_action( 'wp_footer', array( $this, 'shortcode_insert_button_script' ) ); |
|
97 | + // this makes the insert button work for elementor |
|
98 | + add_action( 'elementor/editor/after_enqueue_scripts', array( |
|
99 | + $this, |
|
100 | + 'shortcode_insert_button_script' |
|
101 | + ) ); // for elementor |
|
102 | + } |
|
103 | + // this makes the insert button work for cornerstone |
|
104 | + add_action( 'wp_print_footer_scripts', array( __CLASS__, 'maybe_cornerstone_builder' ) ); |
|
105 | + |
|
106 | + add_action( 'wp_ajax_super_duper_get_widget_settings', array( __CLASS__, 'get_widget_settings' ) ); |
|
107 | + add_action( 'wp_ajax_super_duper_get_picker', array( __CLASS__, 'get_picker' ) ); |
|
108 | + |
|
109 | + // add generator text to admin head |
|
110 | + add_action( 'admin_head', array( $this, 'generator' ) ); |
|
111 | + } |
|
112 | + |
|
113 | + do_action( 'wp_super_duper_widget_init', $options, $this ); |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Add our widget CSS to elementor editor. |
|
118 | + */ |
|
119 | + public function elementor_editor_styles() { |
|
120 | + wp_add_inline_style( 'elementor-editor', $this->widget_css( false ) ); |
|
121 | + } |
|
122 | + |
|
123 | + public function register_fusion_element() { |
|
124 | + |
|
125 | + $options = $this->options; |
|
126 | + |
|
127 | + if ( $this->base_id ) { |
|
128 | + |
|
129 | + $params = $this->get_fusion_params(); |
|
130 | + |
|
131 | + $args = array( |
|
132 | + 'name' => $options['name'], |
|
133 | + 'shortcode' => $this->base_id, |
|
134 | + 'icon' => $options['block-icon'] ? $options['block-icon'] : 'far fa-square', |
|
135 | + 'allow_generator' => true, |
|
136 | + ); |
|
137 | + |
|
138 | + if ( ! empty( $params ) ) { |
|
139 | + $args['params'] = $params; |
|
140 | + } |
|
141 | + |
|
142 | + fusion_builder_map( $args ); |
|
143 | + } |
|
144 | + |
|
145 | + } |
|
146 | + |
|
147 | + public function get_fusion_params() { |
|
148 | + $params = array(); |
|
149 | + $arguments = $this->get_arguments(); |
|
150 | + |
|
151 | + if ( ! empty( $arguments ) ) { |
|
152 | + foreach ( $arguments as $key => $val ) { |
|
153 | + $param = array(); |
|
154 | + // type |
|
155 | + $param['type'] = str_replace( |
|
156 | + array( |
|
157 | + "text", |
|
158 | + "number", |
|
159 | + "email", |
|
160 | + "color", |
|
161 | + "checkbox" |
|
162 | + ), |
|
163 | + array( |
|
164 | + "textfield", |
|
165 | + "textfield", |
|
166 | + "textfield", |
|
167 | + "colorpicker", |
|
168 | + "select", |
|
169 | + |
|
170 | + ), |
|
171 | + $val['type'] ); |
|
172 | + |
|
173 | + // multiselect |
|
174 | + if ( $val['type'] == 'multiselect' || ( ( $param['type'] == 'select' || $val['type'] == 'select' ) && ! empty( $val['multiple'] ) ) ) { |
|
175 | + $param['type'] = 'multiple_select'; |
|
176 | + $param['multiple'] = true; |
|
177 | + } |
|
178 | + |
|
179 | + // heading |
|
180 | + $param['heading'] = $val['title']; |
|
181 | + |
|
182 | + // description |
|
183 | + $param['description'] = isset( $val['desc'] ) ? $val['desc'] : ''; |
|
184 | + |
|
185 | + // param_name |
|
186 | + $param['param_name'] = $key; |
|
187 | + |
|
188 | + // Default |
|
189 | + $param['default'] = isset( $val['default'] ) ? $val['default'] : ''; |
|
190 | + |
|
191 | + // Group |
|
192 | + if ( isset( $val['group'] ) ) { |
|
193 | + $param['group'] = $val['group']; |
|
194 | + } |
|
195 | + |
|
196 | + // value |
|
197 | + if ( $val['type'] == 'checkbox' ) { |
|
198 | + if ( isset( $val['default'] ) && $val['default'] == '0' ) { |
|
199 | + unset( $param['default'] ); |
|
200 | + } |
|
201 | + $param['value'] = array( '' => __( "No" ), '1' => __( "Yes" ) ); |
|
202 | + } elseif ( $param['type'] == 'select' || $param['type'] == 'multiple_select' ) { |
|
203 | + $param['value'] = isset( $val['options'] ) ? $val['options'] : array(); |
|
204 | + } else { |
|
205 | + $param['value'] = isset( $val['default'] ) ? $val['default'] : ''; |
|
206 | + } |
|
207 | + |
|
208 | + // setup the param |
|
209 | + $params[] = $param; |
|
210 | + |
|
211 | + } |
|
212 | + } |
|
213 | + |
|
214 | + |
|
215 | + return $params; |
|
216 | + } |
|
217 | + |
|
218 | + /** |
|
219 | + * Maybe insert the shortcode inserter button in the footer if we are in the cornerstone builder |
|
220 | + */ |
|
221 | + public static function maybe_cornerstone_builder() { |
|
222 | + if ( did_action( 'cornerstone_before_boot_app' ) ) { |
|
223 | + self::shortcode_insert_button_script(); |
|
224 | + } |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * A function to ge the shortcode builder picker html. |
|
229 | + * |
|
230 | + * @param string $editor_id |
|
231 | + * |
|
232 | + * @return string |
|
233 | + */ |
|
234 | + public static function get_picker( $editor_id = '' ) { |
|
235 | + |
|
236 | + ob_start(); |
|
237 | + if ( isset( $_POST['editor_id'] ) ) { |
|
238 | + $editor_id = esc_attr( $_POST['editor_id'] ); |
|
239 | + } elseif ( isset( $_REQUEST['et_fb'] ) ) { |
|
240 | + $editor_id = 'main_content_content_vb_tiny_mce'; |
|
241 | + } |
|
242 | + |
|
243 | + global $sd_widgets; |
|
244 | + ?> |
|
245 | 245 | |
246 | 246 | <div class="sd-shortcode-left-wrap"> |
247 | 247 | <?php |
248 | - ksort( $sd_widgets ); |
|
249 | - // print_r($sd_widgets);exit; |
|
250 | - if ( ! empty( $sd_widgets ) ) { |
|
251 | - echo '<select class="widefat" onchange="sd_get_shortcode_options(this);">'; |
|
252 | - echo "<option>" . __( 'Select shortcode' ) . "</option>"; |
|
253 | - foreach ( $sd_widgets as $shortcode => $class ) { |
|
254 | - echo "<option value='" . esc_attr( $shortcode ) . "'>" . esc_attr( $shortcode ) . " (" . esc_attr( $class['name'] ) . ")</option>"; |
|
255 | - } |
|
256 | - echo "</select>"; |
|
257 | - |
|
258 | - } |
|
259 | - ?> |
|
248 | + ksort( $sd_widgets ); |
|
249 | + // print_r($sd_widgets);exit; |
|
250 | + if ( ! empty( $sd_widgets ) ) { |
|
251 | + echo '<select class="widefat" onchange="sd_get_shortcode_options(this);">'; |
|
252 | + echo "<option>" . __( 'Select shortcode' ) . "</option>"; |
|
253 | + foreach ( $sd_widgets as $shortcode => $class ) { |
|
254 | + echo "<option value='" . esc_attr( $shortcode ) . "'>" . esc_attr( $shortcode ) . " (" . esc_attr( $class['name'] ) . ")</option>"; |
|
255 | + } |
|
256 | + echo "</select>"; |
|
257 | + |
|
258 | + } |
|
259 | + ?> |
|
260 | 260 | <div class="sd-shortcode-settings"></div> |
261 | 261 | |
262 | 262 | </div> |
@@ -267,8 +267,8 @@ discard block |
||
267 | 267 | <?php if ( $editor_id != '' ) { ?> |
268 | 268 | <button class="button sd-insert-shortcode-button" |
269 | 269 | onclick="sd_insert_shortcode(<?php if ( ! empty( $editor_id ) ) { |
270 | - echo "'" . $editor_id . "'"; |
|
271 | - } ?>)"><?php _e( 'Insert shortcode' ); ?></button> |
|
270 | + echo "'" . $editor_id . "'"; |
|
271 | + } ?>)"><?php _e( 'Insert shortcode' ); ?></button> |
|
272 | 272 | <?php } ?> |
273 | 273 | <button class="button" |
274 | 274 | onclick="sd_copy_to_clipboard()"><?php _e( 'Copy shortcode' ); ?></button> |
@@ -276,134 +276,134 @@ discard block |
||
276 | 276 | </div> |
277 | 277 | <?php |
278 | 278 | |
279 | - $html = ob_get_clean(); |
|
280 | - |
|
281 | - if ( wp_doing_ajax() ) { |
|
282 | - echo $html; |
|
283 | - $should_die = true; |
|
284 | - |
|
285 | - // some builder get the editor via ajax so we should not die on those ocasions |
|
286 | - $dont_die = array( |
|
287 | - 'parent_tag',// WP Bakery |
|
288 | - 'avia_request' // enfold |
|
289 | - ); |
|
290 | - |
|
291 | - foreach ( $dont_die as $request ) { |
|
292 | - if ( isset( $_REQUEST[ $request ] ) ) { |
|
293 | - $should_die = false; |
|
294 | - } |
|
295 | - } |
|
296 | - |
|
297 | - if ( $should_die ) { |
|
298 | - wp_die(); |
|
299 | - } |
|
300 | - |
|
301 | - } else { |
|
302 | - return $html; |
|
303 | - } |
|
304 | - |
|
305 | - return ''; |
|
306 | - |
|
307 | - } |
|
308 | - |
|
309 | - /** |
|
310 | - * Output the version in the admin header. |
|
311 | - */ |
|
312 | - public function generator() { |
|
313 | - echo '<meta name="generator" content="WP Super Duper v' . $this->version . '" />'; |
|
314 | - } |
|
315 | - |
|
316 | - /** |
|
317 | - * Get widget settings. |
|
318 | - * |
|
319 | - * @since 1.0.0 |
|
320 | - */ |
|
321 | - public static function get_widget_settings() { |
|
322 | - global $sd_widgets; |
|
323 | - |
|
324 | - $shortcode = isset( $_REQUEST['shortcode'] ) && $_REQUEST['shortcode'] ? sanitize_title_with_dashes( $_REQUEST['shortcode'] ) : ''; |
|
325 | - if ( ! $shortcode ) { |
|
326 | - wp_die(); |
|
327 | - } |
|
328 | - $widget_args = isset( $sd_widgets[ $shortcode ] ) ? $sd_widgets[ $shortcode ] : ''; |
|
329 | - if ( ! $widget_args ) { |
|
330 | - wp_die(); |
|
331 | - } |
|
332 | - $class_name = isset( $widget_args['class_name'] ) && $widget_args['class_name'] ? $widget_args['class_name'] : ''; |
|
333 | - if ( ! $class_name ) { |
|
334 | - wp_die(); |
|
335 | - } |
|
336 | - |
|
337 | - // invoke an instance method |
|
338 | - $widget = new $class_name; |
|
339 | - |
|
340 | - ob_start(); |
|
341 | - $widget->form( array() ); |
|
342 | - $form = ob_get_clean(); |
|
343 | - echo "<form id='$shortcode'>" . $form . "<div class=\"widget-control-save\"></div></form>"; |
|
344 | - echo "<style>" . $widget->widget_css() . "</style>"; |
|
345 | - echo "<script>" . $widget->widget_js() . "</script>"; |
|
346 | - ?> |
|
279 | + $html = ob_get_clean(); |
|
280 | + |
|
281 | + if ( wp_doing_ajax() ) { |
|
282 | + echo $html; |
|
283 | + $should_die = true; |
|
284 | + |
|
285 | + // some builder get the editor via ajax so we should not die on those ocasions |
|
286 | + $dont_die = array( |
|
287 | + 'parent_tag',// WP Bakery |
|
288 | + 'avia_request' // enfold |
|
289 | + ); |
|
290 | + |
|
291 | + foreach ( $dont_die as $request ) { |
|
292 | + if ( isset( $_REQUEST[ $request ] ) ) { |
|
293 | + $should_die = false; |
|
294 | + } |
|
295 | + } |
|
296 | + |
|
297 | + if ( $should_die ) { |
|
298 | + wp_die(); |
|
299 | + } |
|
300 | + |
|
301 | + } else { |
|
302 | + return $html; |
|
303 | + } |
|
304 | + |
|
305 | + return ''; |
|
306 | + |
|
307 | + } |
|
308 | + |
|
309 | + /** |
|
310 | + * Output the version in the admin header. |
|
311 | + */ |
|
312 | + public function generator() { |
|
313 | + echo '<meta name="generator" content="WP Super Duper v' . $this->version . '" />'; |
|
314 | + } |
|
315 | + |
|
316 | + /** |
|
317 | + * Get widget settings. |
|
318 | + * |
|
319 | + * @since 1.0.0 |
|
320 | + */ |
|
321 | + public static function get_widget_settings() { |
|
322 | + global $sd_widgets; |
|
323 | + |
|
324 | + $shortcode = isset( $_REQUEST['shortcode'] ) && $_REQUEST['shortcode'] ? sanitize_title_with_dashes( $_REQUEST['shortcode'] ) : ''; |
|
325 | + if ( ! $shortcode ) { |
|
326 | + wp_die(); |
|
327 | + } |
|
328 | + $widget_args = isset( $sd_widgets[ $shortcode ] ) ? $sd_widgets[ $shortcode ] : ''; |
|
329 | + if ( ! $widget_args ) { |
|
330 | + wp_die(); |
|
331 | + } |
|
332 | + $class_name = isset( $widget_args['class_name'] ) && $widget_args['class_name'] ? $widget_args['class_name'] : ''; |
|
333 | + if ( ! $class_name ) { |
|
334 | + wp_die(); |
|
335 | + } |
|
336 | + |
|
337 | + // invoke an instance method |
|
338 | + $widget = new $class_name; |
|
339 | + |
|
340 | + ob_start(); |
|
341 | + $widget->form( array() ); |
|
342 | + $form = ob_get_clean(); |
|
343 | + echo "<form id='$shortcode'>" . $form . "<div class=\"widget-control-save\"></div></form>"; |
|
344 | + echo "<style>" . $widget->widget_css() . "</style>"; |
|
345 | + echo "<script>" . $widget->widget_js() . "</script>"; |
|
346 | + ?> |
|
347 | 347 | <?php |
348 | - wp_die(); |
|
349 | - } |
|
350 | - |
|
351 | - /** |
|
352 | - * Insert shortcode builder button to classic editor (not inside Gutenberg, not needed). |
|
353 | - * |
|
354 | - * @since 1.0.0 |
|
355 | - * |
|
356 | - * @param string $editor_id Optional. Shortcode editor id. Default null. |
|
357 | - * @param string $insert_shortcode_function Optional. Insert shotcode function. Default null. |
|
358 | - */ |
|
359 | - public static function shortcode_insert_button( $editor_id = '', $insert_shortcode_function = '' ) { |
|
360 | - global $sd_widgets, $shortcode_insert_button_once; |
|
361 | - if ( $shortcode_insert_button_once ) { |
|
362 | - return; |
|
363 | - } |
|
364 | - add_thickbox(); |
|
365 | - |
|
366 | - |
|
367 | - /** |
|
368 | - * Cornerstone makes us play dirty tricks :/ |
|
369 | - * All media_buttons are removed via JS unless they are two specific id's so we wrap our content in this ID so it is not removed. |
|
370 | - */ |
|
371 | - if ( function_exists( 'cornerstone_plugin_init' ) && ! is_admin() ) { |
|
372 | - echo '<span id="insert-media-button">'; |
|
373 | - } |
|
374 | - |
|
375 | - echo self::shortcode_button( 'this', 'true' ); |
|
376 | - |
|
377 | - // see opening note |
|
378 | - if ( function_exists( 'cornerstone_plugin_init' ) && ! is_admin() ) { |
|
379 | - echo '</span>'; // end #insert-media-button |
|
380 | - } |
|
381 | - |
|
382 | - // Add separate script for generatepress theme sections |
|
383 | - if ( function_exists( 'generate_sections_sections_metabox' ) && did_action( 'generate_sections_metabox' ) ) { |
|
384 | - } else { |
|
385 | - self::shortcode_insert_button_script( $editor_id, $insert_shortcode_function ); |
|
386 | - } |
|
387 | - |
|
388 | - $shortcode_insert_button_once = true; |
|
389 | - } |
|
390 | - |
|
391 | - /** |
|
392 | - * Gets the shortcode insert button html. |
|
393 | - * |
|
394 | - * @param string $id |
|
395 | - * @param string $search_for_id |
|
396 | - * |
|
397 | - * @return mixed |
|
398 | - */ |
|
399 | - public static function shortcode_button( $id = '', $search_for_id = '' ) { |
|
400 | - ob_start(); |
|
401 | - ?> |
|
348 | + wp_die(); |
|
349 | + } |
|
350 | + |
|
351 | + /** |
|
352 | + * Insert shortcode builder button to classic editor (not inside Gutenberg, not needed). |
|
353 | + * |
|
354 | + * @since 1.0.0 |
|
355 | + * |
|
356 | + * @param string $editor_id Optional. Shortcode editor id. Default null. |
|
357 | + * @param string $insert_shortcode_function Optional. Insert shotcode function. Default null. |
|
358 | + */ |
|
359 | + public static function shortcode_insert_button( $editor_id = '', $insert_shortcode_function = '' ) { |
|
360 | + global $sd_widgets, $shortcode_insert_button_once; |
|
361 | + if ( $shortcode_insert_button_once ) { |
|
362 | + return; |
|
363 | + } |
|
364 | + add_thickbox(); |
|
365 | + |
|
366 | + |
|
367 | + /** |
|
368 | + * Cornerstone makes us play dirty tricks :/ |
|
369 | + * All media_buttons are removed via JS unless they are two specific id's so we wrap our content in this ID so it is not removed. |
|
370 | + */ |
|
371 | + if ( function_exists( 'cornerstone_plugin_init' ) && ! is_admin() ) { |
|
372 | + echo '<span id="insert-media-button">'; |
|
373 | + } |
|
374 | + |
|
375 | + echo self::shortcode_button( 'this', 'true' ); |
|
376 | + |
|
377 | + // see opening note |
|
378 | + if ( function_exists( 'cornerstone_plugin_init' ) && ! is_admin() ) { |
|
379 | + echo '</span>'; // end #insert-media-button |
|
380 | + } |
|
381 | + |
|
382 | + // Add separate script for generatepress theme sections |
|
383 | + if ( function_exists( 'generate_sections_sections_metabox' ) && did_action( 'generate_sections_metabox' ) ) { |
|
384 | + } else { |
|
385 | + self::shortcode_insert_button_script( $editor_id, $insert_shortcode_function ); |
|
386 | + } |
|
387 | + |
|
388 | + $shortcode_insert_button_once = true; |
|
389 | + } |
|
390 | + |
|
391 | + /** |
|
392 | + * Gets the shortcode insert button html. |
|
393 | + * |
|
394 | + * @param string $id |
|
395 | + * @param string $search_for_id |
|
396 | + * |
|
397 | + * @return mixed |
|
398 | + */ |
|
399 | + public static function shortcode_button( $id = '', $search_for_id = '' ) { |
|
400 | + ob_start(); |
|
401 | + ?> |
|
402 | 402 | <span class="sd-lable-shortcode-inserter"> |
403 | 403 | <a onclick="sd_ajax_get_picker(<?php echo $id; |
404 | - if ( $search_for_id ) { |
|
405 | - echo "," . $search_for_id; |
|
406 | - } ?>);" href="#TB_inline?width=100%&height=550&inlineId=super-duper-content-ajaxed" |
|
404 | + if ( $search_for_id ) { |
|
405 | + echo "," . $search_for_id; |
|
406 | + } ?>);" href="#TB_inline?width=100%&height=550&inlineId=super-duper-content-ajaxed" |
|
407 | 407 | class="thickbox button super-duper-content-open" title="Add Shortcode"> |
408 | 408 | <span style="vertical-align: middle;line-height: 18px;font-size: 20px;" |
409 | 409 | class="dashicons dashicons-screenoptions"></span> |
@@ -414,21 +414,21 @@ discard block |
||
414 | 414 | </span> |
415 | 415 | |
416 | 416 | <?php |
417 | - $html = ob_get_clean(); |
|
418 | - |
|
419 | - // remove line breaks so we can use it in js |
|
420 | - return preg_replace( "/\r|\n/", "", trim( $html ) ); |
|
421 | - } |
|
422 | - |
|
423 | - /** |
|
424 | - * Makes SD work with the siteOrigin page builder. |
|
425 | - * |
|
426 | - * @since 1.0.6 |
|
427 | - * @return mixed |
|
428 | - */ |
|
429 | - public static function siteorigin_js() { |
|
430 | - ob_start(); |
|
431 | - ?> |
|
417 | + $html = ob_get_clean(); |
|
418 | + |
|
419 | + // remove line breaks so we can use it in js |
|
420 | + return preg_replace( "/\r|\n/", "", trim( $html ) ); |
|
421 | + } |
|
422 | + |
|
423 | + /** |
|
424 | + * Makes SD work with the siteOrigin page builder. |
|
425 | + * |
|
426 | + * @since 1.0.6 |
|
427 | + * @return mixed |
|
428 | + */ |
|
429 | + public static function siteorigin_js() { |
|
430 | + ob_start(); |
|
431 | + ?> |
|
432 | 432 | <script> |
433 | 433 | /** |
434 | 434 | * Check a form to see what items shoudl be shown or hidden. |
@@ -506,28 +506,28 @@ discard block |
||
506 | 506 | }); |
507 | 507 | </script> |
508 | 508 | <?php |
509 | - $output = ob_get_clean(); |
|
509 | + $output = ob_get_clean(); |
|
510 | 510 | |
511 | - /* |
|
511 | + /* |
|
512 | 512 | * We only add the <script> tags for code highlighting, so we strip them from the output. |
513 | 513 | */ |
514 | 514 | |
515 | - return str_replace( array( |
|
516 | - '<script>', |
|
517 | - '</script>' |
|
518 | - ), '', $output ); |
|
519 | - } |
|
520 | - |
|
521 | - /** |
|
522 | - * Output the JS and CSS for the shortcode insert button. |
|
523 | - * |
|
524 | - * @since 1.0.6 |
|
525 | - * |
|
526 | - * @param string $editor_id |
|
527 | - * @param string $insert_shortcode_function |
|
528 | - */ |
|
529 | - public static function shortcode_insert_button_script( $editor_id = '', $insert_shortcode_function = '' ) { |
|
530 | - ?> |
|
515 | + return str_replace( array( |
|
516 | + '<script>', |
|
517 | + '</script>' |
|
518 | + ), '', $output ); |
|
519 | + } |
|
520 | + |
|
521 | + /** |
|
522 | + * Output the JS and CSS for the shortcode insert button. |
|
523 | + * |
|
524 | + * @since 1.0.6 |
|
525 | + * |
|
526 | + * @param string $editor_id |
|
527 | + * @param string $insert_shortcode_function |
|
528 | + */ |
|
529 | + public static function shortcode_insert_button_script( $editor_id = '', $insert_shortcode_function = '' ) { |
|
530 | + ?> |
|
531 | 531 | <style> |
532 | 532 | .sd-shortcode-left-wrap { |
533 | 533 | float: left; |
@@ -653,35 +653,35 @@ discard block |
||
653 | 653 | <?php } ?> |
654 | 654 | </style> |
655 | 655 | <?php |
656 | - if ( class_exists( 'SiteOrigin_Panels' ) ) { |
|
657 | - echo "<script>" . self::siteorigin_js() . "</script>"; |
|
658 | - } |
|
659 | - ?> |
|
656 | + if ( class_exists( 'SiteOrigin_Panels' ) ) { |
|
657 | + echo "<script>" . self::siteorigin_js() . "</script>"; |
|
658 | + } |
|
659 | + ?> |
|
660 | 660 | <script> |
661 | 661 | <?php |
662 | - if(! empty( $insert_shortcode_function )){ |
|
663 | - echo $insert_shortcode_function; |
|
664 | - }else{ |
|
665 | - |
|
666 | - /** |
|
667 | - * Function for super duper insert shortcode. |
|
668 | - * |
|
669 | - * @since 1.0.0 |
|
670 | - */ |
|
671 | - ?> |
|
662 | + if(! empty( $insert_shortcode_function )){ |
|
663 | + echo $insert_shortcode_function; |
|
664 | + }else{ |
|
665 | + |
|
666 | + /** |
|
667 | + * Function for super duper insert shortcode. |
|
668 | + * |
|
669 | + * @since 1.0.0 |
|
670 | + */ |
|
671 | + ?> |
|
672 | 672 | function sd_insert_shortcode($editor_id) { |
673 | 673 | $shortcode = jQuery('#TB_ajaxContent #sd-shortcode-output').val(); |
674 | 674 | if ($shortcode) { |
675 | 675 | if (!$editor_id) { |
676 | 676 | <?php |
677 | - if ( isset( $_REQUEST['et_fb'] ) ) { |
|
678 | - echo '$editor_id = "#main_content_content_vb_tiny_mce";'; |
|
679 | - } elseif ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor' ) { |
|
680 | - echo '$editor_id = "#elementor-controls .wp-editor-container textarea";'; |
|
681 | - } else { |
|
682 | - echo '$editor_id = "#wp-content-editor-container textarea";'; |
|
683 | - } |
|
684 | - ?> |
|
677 | + if ( isset( $_REQUEST['et_fb'] ) ) { |
|
678 | + echo '$editor_id = "#main_content_content_vb_tiny_mce";'; |
|
679 | + } elseif ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor' ) { |
|
680 | + echo '$editor_id = "#elementor-controls .wp-editor-container textarea";'; |
|
681 | + } else { |
|
682 | + echo '$editor_id = "#wp-content-editor-container textarea";'; |
|
683 | + } |
|
684 | + ?> |
|
685 | 685 | } else { |
686 | 686 | $editor_id = '#' + $editor_id; |
687 | 687 | } |
@@ -1007,18 +1007,18 @@ discard block |
||
1007 | 1007 | |
1008 | 1008 | </script> |
1009 | 1009 | <?php |
1010 | - } |
|
1011 | - |
|
1012 | - /** |
|
1013 | - * Gets some CSS for the widgets screen. |
|
1014 | - * |
|
1015 | - * @param bool $advanced If we should include advanced CSS. |
|
1016 | - * |
|
1017 | - * @return mixed |
|
1018 | - */ |
|
1019 | - public function widget_css( $advanced = true ) { |
|
1020 | - ob_start(); |
|
1021 | - ?> |
|
1010 | + } |
|
1011 | + |
|
1012 | + /** |
|
1013 | + * Gets some CSS for the widgets screen. |
|
1014 | + * |
|
1015 | + * @param bool $advanced If we should include advanced CSS. |
|
1016 | + * |
|
1017 | + * @return mixed |
|
1018 | + */ |
|
1019 | + public function widget_css( $advanced = true ) { |
|
1020 | + ob_start(); |
|
1021 | + ?> |
|
1022 | 1022 | <style> |
1023 | 1023 | <?php if( $advanced ){ ?> |
1024 | 1024 | .sd-advanced-setting { |
@@ -1056,26 +1056,26 @@ discard block |
||
1056 | 1056 | } |
1057 | 1057 | </style> |
1058 | 1058 | <?php |
1059 | - $output = ob_get_clean(); |
|
1059 | + $output = ob_get_clean(); |
|
1060 | 1060 | |
1061 | - /* |
|
1061 | + /* |
|
1062 | 1062 | * We only add the <script> tags for code highlighting, so we strip them from the output. |
1063 | 1063 | */ |
1064 | 1064 | |
1065 | - return str_replace( array( |
|
1066 | - '<style>', |
|
1067 | - '</style>' |
|
1068 | - ), '', $output ); |
|
1069 | - } |
|
1070 | - |
|
1071 | - /** |
|
1072 | - * Gets some JS for the widgets screen. |
|
1073 | - * |
|
1074 | - * @return mixed |
|
1075 | - */ |
|
1076 | - public function widget_js() { |
|
1077 | - ob_start(); |
|
1078 | - ?> |
|
1065 | + return str_replace( array( |
|
1066 | + '<style>', |
|
1067 | + '</style>' |
|
1068 | + ), '', $output ); |
|
1069 | + } |
|
1070 | + |
|
1071 | + /** |
|
1072 | + * Gets some JS for the widgets screen. |
|
1073 | + * |
|
1074 | + * @return mixed |
|
1075 | + */ |
|
1076 | + public function widget_js() { |
|
1077 | + ob_start(); |
|
1078 | + ?> |
|
1079 | 1079 | <script> |
1080 | 1080 | |
1081 | 1081 | /** |
@@ -1230,402 +1230,402 @@ discard block |
||
1230 | 1230 | <?php do_action( 'wp_super_duper_widget_js', $this ); ?> |
1231 | 1231 | </script> |
1232 | 1232 | <?php |
1233 | - $output = ob_get_clean(); |
|
1233 | + $output = ob_get_clean(); |
|
1234 | 1234 | |
1235 | - /* |
|
1235 | + /* |
|
1236 | 1236 | * We only add the <script> tags for code highlighting, so we strip them from the output. |
1237 | 1237 | */ |
1238 | 1238 | |
1239 | - return str_replace( array( |
|
1240 | - '<script>', |
|
1241 | - '</script>' |
|
1242 | - ), '', $output ); |
|
1243 | - } |
|
1244 | - |
|
1245 | - |
|
1246 | - /** |
|
1247 | - * Set the name from the argument key. |
|
1248 | - * |
|
1249 | - * @param $options |
|
1250 | - * |
|
1251 | - * @return mixed |
|
1252 | - */ |
|
1253 | - private function add_name_from_key( $options, $arguments = false ) { |
|
1254 | - if ( ! empty( $options['arguments'] ) ) { |
|
1255 | - foreach ( $options['arguments'] as $key => $val ) { |
|
1256 | - $options['arguments'][ $key ]['name'] = $key; |
|
1257 | - } |
|
1258 | - } elseif ( $arguments && is_array( $options ) && ! empty( $options ) ) { |
|
1259 | - foreach ( $options as $key => $val ) { |
|
1260 | - $options[ $key ]['name'] = $key; |
|
1261 | - } |
|
1262 | - } |
|
1263 | - |
|
1264 | - return $options; |
|
1265 | - } |
|
1266 | - |
|
1267 | - /** |
|
1268 | - * Register the parent shortcode. |
|
1269 | - * |
|
1270 | - * @since 1.0.0 |
|
1271 | - */ |
|
1272 | - public function register_shortcode() { |
|
1273 | - add_shortcode( $this->base_id, array( $this, 'shortcode_output' ) ); |
|
1274 | - add_action( 'wp_ajax_super_duper_output_shortcode', array( __CLASS__, 'render_shortcode' ) ); |
|
1275 | - } |
|
1276 | - |
|
1277 | - /** |
|
1278 | - * Render the shortcode via ajax so we can return it to Gutenberg. |
|
1279 | - * |
|
1280 | - * @since 1.0.0 |
|
1281 | - */ |
|
1282 | - public static function render_shortcode() { |
|
1283 | - |
|
1284 | - check_ajax_referer( 'super_duper_output_shortcode', '_ajax_nonce', true ); |
|
1285 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
1286 | - wp_die(); |
|
1287 | - } |
|
1288 | - |
|
1289 | - // we might need the $post value here so lets set it. |
|
1290 | - if ( isset( $_POST['post_id'] ) && $_POST['post_id'] ) { |
|
1291 | - $post_obj = get_post( absint( $_POST['post_id'] ) ); |
|
1292 | - if ( ! empty( $post_obj ) && empty( $post ) ) { |
|
1293 | - global $post; |
|
1294 | - $post = $post_obj; |
|
1295 | - } |
|
1296 | - } |
|
1297 | - |
|
1298 | - if ( isset( $_POST['shortcode'] ) && $_POST['shortcode'] ) { |
|
1299 | - $shortcode_name = sanitize_title_with_dashes( $_POST['shortcode'] ); |
|
1300 | - $attributes_array = isset( $_POST['attributes'] ) && $_POST['attributes'] ? $_POST['attributes'] : array(); |
|
1301 | - $attributes = ''; |
|
1302 | - if ( ! empty( $attributes_array ) ) { |
|
1303 | - foreach ( $attributes_array as $key => $value ) { |
|
1304 | - $attributes .= " " . sanitize_title_with_dashes( $key ) . "='" . wp_slash( $value ) . "' "; |
|
1305 | - } |
|
1306 | - } |
|
1307 | - |
|
1308 | - $shortcode = "[" . $shortcode_name . " " . $attributes . "]"; |
|
1309 | - |
|
1310 | - echo do_shortcode( $shortcode ); |
|
1311 | - |
|
1312 | - } |
|
1313 | - wp_die(); |
|
1314 | - } |
|
1315 | - |
|
1316 | - /** |
|
1317 | - * Output the shortcode. |
|
1318 | - * |
|
1319 | - * @param array $args |
|
1320 | - * @param string $content |
|
1321 | - * |
|
1322 | - * @return string |
|
1323 | - */ |
|
1324 | - public function shortcode_output( $args = array(), $content = '' ) { |
|
1325 | - $args = $this->argument_values( $args ); |
|
1326 | - |
|
1327 | - // add extra argument so we know its a output to gutenberg |
|
1328 | - //$args |
|
1329 | - $args = $this->string_to_bool( $args ); |
|
1330 | - |
|
1331 | - // if we have a enclosed shortcode we add it to the special `html` argument |
|
1332 | - if ( ! empty( $content ) ) { |
|
1333 | - $args['html'] = $content; |
|
1334 | - } |
|
1335 | - |
|
1336 | - $class = isset( $this->options['widget_ops']['classname'] ) ? esc_attr( $this->options['widget_ops']['classname'] ) : ''; |
|
1337 | - $class .= " sdel-".$this->get_instance_hash(); |
|
1338 | - |
|
1339 | - $class = apply_filters( 'wp_super_duper_div_classname', $class, $args, $this ); |
|
1340 | - $class = apply_filters( 'wp_super_duper_div_classname_' . $this->base_id, $class, $args, $this ); |
|
1341 | - |
|
1342 | - $attrs = apply_filters( 'wp_super_duper_div_attrs', '', $args, $this ); |
|
1343 | - $attrs = apply_filters( 'wp_super_duper_div_attrs_' . $this->base_id, '', $args, $this ); //@todo this does not seem right @kiran? |
|
1344 | - |
|
1345 | - $shortcode_args = array(); |
|
1346 | - $output = ''; |
|
1347 | - $no_wrap = isset( $this->options['no_wrap'] ) && $this->options['no_wrap'] ? true : false; |
|
1348 | - if ( isset( $args['no_wrap'] ) && $args['no_wrap'] ) { |
|
1349 | - $no_wrap = true; |
|
1350 | - } |
|
1351 | - $main_content = $this->output( $args, $shortcode_args, $content ); |
|
1352 | - if ( $main_content && ! $no_wrap ) { |
|
1353 | - // wrap the shortcode in a div with the same class as the widget |
|
1354 | - $output .= '<div class="' . $class . '" ' . $attrs . '>'; |
|
1355 | - if ( ! empty( $args['title'] ) ) { |
|
1356 | - // if its a shortcode and there is a title try to grab the title wrappers |
|
1357 | - $shortcode_args = array( 'before_title' => '', 'after_title' => '' ); |
|
1358 | - if ( empty( $instance ) ) { |
|
1359 | - global $wp_registered_sidebars; |
|
1360 | - if ( ! empty( $wp_registered_sidebars ) ) { |
|
1361 | - foreach ( $wp_registered_sidebars as $sidebar ) { |
|
1362 | - if ( ! empty( $sidebar['before_title'] ) ) { |
|
1363 | - $shortcode_args['before_title'] = $sidebar['before_title']; |
|
1364 | - $shortcode_args['after_title'] = $sidebar['after_title']; |
|
1365 | - break; |
|
1366 | - } |
|
1367 | - } |
|
1368 | - } |
|
1369 | - } |
|
1370 | - $output .= $this->output_title( $shortcode_args, $args ); |
|
1371 | - } |
|
1372 | - $output .= $main_content; |
|
1373 | - $output .= '</div>'; |
|
1374 | - } elseif ( $main_content && $no_wrap ) { |
|
1375 | - $output .= $main_content; |
|
1376 | - } |
|
1377 | - |
|
1378 | - // if preview show a placeholder if empty |
|
1379 | - if ( $this->is_preview() && $output == '' ) { |
|
1380 | - $output = $this->preview_placeholder_text( "{{" . $this->base_id . "}}" ); |
|
1381 | - } |
|
1382 | - |
|
1383 | - return apply_filters( 'wp_super_duper_widget_output', $output, $args, $shortcode_args, $this ); |
|
1384 | - } |
|
1385 | - |
|
1386 | - /** |
|
1387 | - * Placeholder text to show if output is empty and we are on a preview/builder page. |
|
1388 | - * |
|
1389 | - * @param string $name |
|
1390 | - * |
|
1391 | - * @return string |
|
1392 | - */ |
|
1393 | - public function preview_placeholder_text( $name = '' ) { |
|
1394 | - return "<div style='background:#0185ba33;padding: 10px;border: 4px #ccc dashed;'>" . sprintf( __( 'Placeholder for: %s' ), $name ) . "</div>"; |
|
1395 | - } |
|
1396 | - |
|
1397 | - /** |
|
1398 | - * Sometimes booleans values can be turned to strings, so we fix that. |
|
1399 | - * |
|
1400 | - * @param $options |
|
1401 | - * |
|
1402 | - * @return mixed |
|
1403 | - */ |
|
1404 | - public function string_to_bool( $options ) { |
|
1405 | - // convert bool strings to booleans |
|
1406 | - foreach ( $options as $key => $val ) { |
|
1407 | - if ( $val == 'false' ) { |
|
1408 | - $options[ $key ] = false; |
|
1409 | - } elseif ( $val == 'true' ) { |
|
1410 | - $options[ $key ] = true; |
|
1411 | - } |
|
1412 | - } |
|
1413 | - |
|
1414 | - return $options; |
|
1415 | - } |
|
1416 | - |
|
1417 | - /** |
|
1418 | - * Get the argument values that are also filterable. |
|
1419 | - * |
|
1420 | - * @param $instance |
|
1421 | - * |
|
1422 | - * @since 1.0.12 Don't set checkbox default value if the value is empty. |
|
1423 | - * |
|
1424 | - * @return array |
|
1425 | - */ |
|
1426 | - public function argument_values( $instance ) { |
|
1427 | - $argument_values = array(); |
|
1428 | - |
|
1429 | - // set widget instance |
|
1430 | - $this->instance = $instance; |
|
1431 | - |
|
1432 | - if ( empty( $this->arguments ) ) { |
|
1433 | - $this->arguments = $this->get_arguments(); |
|
1434 | - } |
|
1435 | - |
|
1436 | - if ( ! empty( $this->arguments ) ) { |
|
1437 | - foreach ( $this->arguments as $key => $args ) { |
|
1438 | - // set the input name from the key |
|
1439 | - $args['name'] = $key; |
|
1440 | - // |
|
1441 | - $argument_values[ $key ] = isset( $instance[ $key ] ) ? $instance[ $key ] : ''; |
|
1442 | - if ( $args['type'] == 'checkbox' && $argument_values[ $key ] == '' ) { |
|
1443 | - // don't set default for an empty checkbox |
|
1444 | - } elseif ( $argument_values[ $key ] == '' && isset( $args['default'] ) ) { |
|
1445 | - $argument_values[ $key ] = $args['default']; |
|
1446 | - } |
|
1447 | - } |
|
1448 | - } |
|
1449 | - |
|
1450 | - return $argument_values; |
|
1451 | - } |
|
1452 | - |
|
1453 | - /** |
|
1454 | - * Set arguments in super duper. |
|
1455 | - * |
|
1456 | - * @since 1.0.0 |
|
1457 | - * |
|
1458 | - * @return array Set arguments. |
|
1459 | - */ |
|
1460 | - public function set_arguments() { |
|
1461 | - return $this->arguments; |
|
1462 | - } |
|
1463 | - |
|
1464 | - /** |
|
1465 | - * Get arguments in super duper. |
|
1466 | - * |
|
1467 | - * @since 1.0.0 |
|
1468 | - * |
|
1469 | - * @return array Get arguments. |
|
1470 | - */ |
|
1471 | - public function get_arguments() { |
|
1472 | - if ( empty( $this->arguments ) ) { |
|
1473 | - $this->arguments = $this->set_arguments(); |
|
1474 | - } |
|
1475 | - |
|
1476 | - $this->arguments = apply_filters( 'wp_super_duper_arguments', $this->arguments, $this->options, $this->instance ); |
|
1477 | - $this->arguments = $this->add_name_from_key( $this->arguments, true ); |
|
1478 | - |
|
1479 | - return $this->arguments; |
|
1480 | - } |
|
1481 | - |
|
1482 | - /** |
|
1483 | - * This is the main output class for all 3 items, widget, shortcode and block, it is extended in the calling class. |
|
1484 | - * |
|
1485 | - * @param array $args |
|
1486 | - * @param array $widget_args |
|
1487 | - * @param string $content |
|
1488 | - */ |
|
1489 | - public function output( $args = array(), $widget_args = array(), $content = '' ) { |
|
1490 | - |
|
1491 | - } |
|
1492 | - |
|
1493 | - /** |
|
1494 | - * Add the dynamic block code inline when the wp-block in enqueued. |
|
1495 | - */ |
|
1496 | - public function register_block() { |
|
1497 | - wp_add_inline_script( 'wp-blocks', $this->block() ); |
|
1498 | - if ( class_exists( 'SiteOrigin_Panels' ) ) { |
|
1499 | - wp_add_inline_script( 'wp-blocks', $this->siteorigin_js() ); |
|
1500 | - } |
|
1501 | - } |
|
1502 | - |
|
1503 | - /** |
|
1504 | - * Check if we need to show advanced options. |
|
1505 | - * |
|
1506 | - * @return bool |
|
1507 | - */ |
|
1508 | - public function block_show_advanced() { |
|
1509 | - |
|
1510 | - $show = false; |
|
1511 | - $arguments = $this->arguments; |
|
1512 | - |
|
1513 | - if ( empty( $arguments ) ) { |
|
1514 | - $arguments = $this->get_arguments(); |
|
1515 | - } |
|
1516 | - |
|
1517 | - if ( ! empty( $arguments ) ) { |
|
1518 | - foreach ( $arguments as $argument ) { |
|
1519 | - if ( isset( $argument['advanced'] ) && $argument['advanced'] ) { |
|
1520 | - $show = true; |
|
1521 | - break; // no need to continue if we know we have it |
|
1522 | - } |
|
1523 | - } |
|
1524 | - } |
|
1525 | - |
|
1526 | - return $show; |
|
1527 | - } |
|
1528 | - |
|
1529 | - /** |
|
1530 | - * Get the url path to the current folder. |
|
1531 | - * |
|
1532 | - * @return string |
|
1533 | - */ |
|
1534 | - public function get_url() { |
|
1535 | - |
|
1536 | - $url = $this->url; |
|
1537 | - |
|
1538 | - if ( ! $url ) { |
|
1539 | - // check if we are inside a plugin |
|
1540 | - $file_dir = str_replace( "/includes", "", dirname( __FILE__ ) ); |
|
1541 | - |
|
1542 | - $dir_parts = explode( "/wp-content/", $file_dir ); |
|
1543 | - $url_parts = explode( "/wp-content/", plugins_url() ); |
|
1544 | - |
|
1545 | - if ( ! empty( $url_parts[0] ) && ! empty( $dir_parts[1] ) ) { |
|
1546 | - $url = trailingslashit( $url_parts[0] . "/wp-content/" . $dir_parts[1] ); |
|
1547 | - $this->url = $url; |
|
1548 | - } |
|
1549 | - } |
|
1550 | - |
|
1551 | - |
|
1552 | - return $url; |
|
1553 | - } |
|
1554 | - |
|
1555 | - /** |
|
1556 | - * Generate the block icon. |
|
1557 | - * |
|
1558 | - * Enables the use of Font Awesome icons. |
|
1559 | - * |
|
1560 | - * @note xlink:href is actually deprecated but href is not supported by all so we use both. |
|
1561 | - * |
|
1562 | - * @param $icon |
|
1563 | - * |
|
1564 | - * @since 1.1.0 |
|
1565 | - * @return string |
|
1566 | - */ |
|
1567 | - public function get_block_icon( $icon ) { |
|
1568 | - |
|
1569 | - // check if we have a Font Awesome icon |
|
1570 | - $fa_type = ''; |
|
1571 | - if ( substr( $icon, 0, 7 ) === "fas fa-" ) { |
|
1572 | - $fa_type = 'solid'; |
|
1573 | - } elseif ( substr( $icon, 0, 7 ) === "far fa-" ) { |
|
1574 | - $fa_type = 'regular'; |
|
1575 | - } elseif ( substr( $icon, 0, 7 ) === "fab fa-" ) { |
|
1576 | - $fa_type = 'brands'; |
|
1577 | - } else { |
|
1578 | - $icon = "'" . $icon . "'"; |
|
1579 | - } |
|
1580 | - |
|
1581 | - // set the icon if we found one |
|
1582 | - if ( $fa_type ) { |
|
1583 | - $fa_icon = str_replace( array( "fas fa-", "far fa-", "fab fa-" ), "", $icon ); |
|
1584 | - $icon = "el('svg',{width: 20, height: 20, viewBox: '0 0 20 20'},el('use', {'xlink:href': '" . $this->get_url() . "icons/" . $fa_type . ".svg#" . $fa_icon . "','href': '" . $this->get_url() . "icons/" . $fa_type . ".svg#" . $fa_icon . "'}))"; |
|
1585 | - } |
|
1586 | - |
|
1587 | - return $icon; |
|
1588 | - } |
|
1589 | - |
|
1590 | - public function group_arguments( $arguments ) { |
|
1239 | + return str_replace( array( |
|
1240 | + '<script>', |
|
1241 | + '</script>' |
|
1242 | + ), '', $output ); |
|
1243 | + } |
|
1244 | + |
|
1245 | + |
|
1246 | + /** |
|
1247 | + * Set the name from the argument key. |
|
1248 | + * |
|
1249 | + * @param $options |
|
1250 | + * |
|
1251 | + * @return mixed |
|
1252 | + */ |
|
1253 | + private function add_name_from_key( $options, $arguments = false ) { |
|
1254 | + if ( ! empty( $options['arguments'] ) ) { |
|
1255 | + foreach ( $options['arguments'] as $key => $val ) { |
|
1256 | + $options['arguments'][ $key ]['name'] = $key; |
|
1257 | + } |
|
1258 | + } elseif ( $arguments && is_array( $options ) && ! empty( $options ) ) { |
|
1259 | + foreach ( $options as $key => $val ) { |
|
1260 | + $options[ $key ]['name'] = $key; |
|
1261 | + } |
|
1262 | + } |
|
1263 | + |
|
1264 | + return $options; |
|
1265 | + } |
|
1266 | + |
|
1267 | + /** |
|
1268 | + * Register the parent shortcode. |
|
1269 | + * |
|
1270 | + * @since 1.0.0 |
|
1271 | + */ |
|
1272 | + public function register_shortcode() { |
|
1273 | + add_shortcode( $this->base_id, array( $this, 'shortcode_output' ) ); |
|
1274 | + add_action( 'wp_ajax_super_duper_output_shortcode', array( __CLASS__, 'render_shortcode' ) ); |
|
1275 | + } |
|
1276 | + |
|
1277 | + /** |
|
1278 | + * Render the shortcode via ajax so we can return it to Gutenberg. |
|
1279 | + * |
|
1280 | + * @since 1.0.0 |
|
1281 | + */ |
|
1282 | + public static function render_shortcode() { |
|
1283 | + |
|
1284 | + check_ajax_referer( 'super_duper_output_shortcode', '_ajax_nonce', true ); |
|
1285 | + if ( ! current_user_can( 'manage_options' ) ) { |
|
1286 | + wp_die(); |
|
1287 | + } |
|
1288 | + |
|
1289 | + // we might need the $post value here so lets set it. |
|
1290 | + if ( isset( $_POST['post_id'] ) && $_POST['post_id'] ) { |
|
1291 | + $post_obj = get_post( absint( $_POST['post_id'] ) ); |
|
1292 | + if ( ! empty( $post_obj ) && empty( $post ) ) { |
|
1293 | + global $post; |
|
1294 | + $post = $post_obj; |
|
1295 | + } |
|
1296 | + } |
|
1297 | + |
|
1298 | + if ( isset( $_POST['shortcode'] ) && $_POST['shortcode'] ) { |
|
1299 | + $shortcode_name = sanitize_title_with_dashes( $_POST['shortcode'] ); |
|
1300 | + $attributes_array = isset( $_POST['attributes'] ) && $_POST['attributes'] ? $_POST['attributes'] : array(); |
|
1301 | + $attributes = ''; |
|
1302 | + if ( ! empty( $attributes_array ) ) { |
|
1303 | + foreach ( $attributes_array as $key => $value ) { |
|
1304 | + $attributes .= " " . sanitize_title_with_dashes( $key ) . "='" . wp_slash( $value ) . "' "; |
|
1305 | + } |
|
1306 | + } |
|
1307 | + |
|
1308 | + $shortcode = "[" . $shortcode_name . " " . $attributes . "]"; |
|
1309 | + |
|
1310 | + echo do_shortcode( $shortcode ); |
|
1311 | + |
|
1312 | + } |
|
1313 | + wp_die(); |
|
1314 | + } |
|
1315 | + |
|
1316 | + /** |
|
1317 | + * Output the shortcode. |
|
1318 | + * |
|
1319 | + * @param array $args |
|
1320 | + * @param string $content |
|
1321 | + * |
|
1322 | + * @return string |
|
1323 | + */ |
|
1324 | + public function shortcode_output( $args = array(), $content = '' ) { |
|
1325 | + $args = $this->argument_values( $args ); |
|
1326 | + |
|
1327 | + // add extra argument so we know its a output to gutenberg |
|
1328 | + //$args |
|
1329 | + $args = $this->string_to_bool( $args ); |
|
1330 | + |
|
1331 | + // if we have a enclosed shortcode we add it to the special `html` argument |
|
1332 | + if ( ! empty( $content ) ) { |
|
1333 | + $args['html'] = $content; |
|
1334 | + } |
|
1335 | + |
|
1336 | + $class = isset( $this->options['widget_ops']['classname'] ) ? esc_attr( $this->options['widget_ops']['classname'] ) : ''; |
|
1337 | + $class .= " sdel-".$this->get_instance_hash(); |
|
1338 | + |
|
1339 | + $class = apply_filters( 'wp_super_duper_div_classname', $class, $args, $this ); |
|
1340 | + $class = apply_filters( 'wp_super_duper_div_classname_' . $this->base_id, $class, $args, $this ); |
|
1341 | + |
|
1342 | + $attrs = apply_filters( 'wp_super_duper_div_attrs', '', $args, $this ); |
|
1343 | + $attrs = apply_filters( 'wp_super_duper_div_attrs_' . $this->base_id, '', $args, $this ); //@todo this does not seem right @kiran? |
|
1344 | + |
|
1345 | + $shortcode_args = array(); |
|
1346 | + $output = ''; |
|
1347 | + $no_wrap = isset( $this->options['no_wrap'] ) && $this->options['no_wrap'] ? true : false; |
|
1348 | + if ( isset( $args['no_wrap'] ) && $args['no_wrap'] ) { |
|
1349 | + $no_wrap = true; |
|
1350 | + } |
|
1351 | + $main_content = $this->output( $args, $shortcode_args, $content ); |
|
1352 | + if ( $main_content && ! $no_wrap ) { |
|
1353 | + // wrap the shortcode in a div with the same class as the widget |
|
1354 | + $output .= '<div class="' . $class . '" ' . $attrs . '>'; |
|
1355 | + if ( ! empty( $args['title'] ) ) { |
|
1356 | + // if its a shortcode and there is a title try to grab the title wrappers |
|
1357 | + $shortcode_args = array( 'before_title' => '', 'after_title' => '' ); |
|
1358 | + if ( empty( $instance ) ) { |
|
1359 | + global $wp_registered_sidebars; |
|
1360 | + if ( ! empty( $wp_registered_sidebars ) ) { |
|
1361 | + foreach ( $wp_registered_sidebars as $sidebar ) { |
|
1362 | + if ( ! empty( $sidebar['before_title'] ) ) { |
|
1363 | + $shortcode_args['before_title'] = $sidebar['before_title']; |
|
1364 | + $shortcode_args['after_title'] = $sidebar['after_title']; |
|
1365 | + break; |
|
1366 | + } |
|
1367 | + } |
|
1368 | + } |
|
1369 | + } |
|
1370 | + $output .= $this->output_title( $shortcode_args, $args ); |
|
1371 | + } |
|
1372 | + $output .= $main_content; |
|
1373 | + $output .= '</div>'; |
|
1374 | + } elseif ( $main_content && $no_wrap ) { |
|
1375 | + $output .= $main_content; |
|
1376 | + } |
|
1377 | + |
|
1378 | + // if preview show a placeholder if empty |
|
1379 | + if ( $this->is_preview() && $output == '' ) { |
|
1380 | + $output = $this->preview_placeholder_text( "{{" . $this->base_id . "}}" ); |
|
1381 | + } |
|
1382 | + |
|
1383 | + return apply_filters( 'wp_super_duper_widget_output', $output, $args, $shortcode_args, $this ); |
|
1384 | + } |
|
1385 | + |
|
1386 | + /** |
|
1387 | + * Placeholder text to show if output is empty and we are on a preview/builder page. |
|
1388 | + * |
|
1389 | + * @param string $name |
|
1390 | + * |
|
1391 | + * @return string |
|
1392 | + */ |
|
1393 | + public function preview_placeholder_text( $name = '' ) { |
|
1394 | + return "<div style='background:#0185ba33;padding: 10px;border: 4px #ccc dashed;'>" . sprintf( __( 'Placeholder for: %s' ), $name ) . "</div>"; |
|
1395 | + } |
|
1396 | + |
|
1397 | + /** |
|
1398 | + * Sometimes booleans values can be turned to strings, so we fix that. |
|
1399 | + * |
|
1400 | + * @param $options |
|
1401 | + * |
|
1402 | + * @return mixed |
|
1403 | + */ |
|
1404 | + public function string_to_bool( $options ) { |
|
1405 | + // convert bool strings to booleans |
|
1406 | + foreach ( $options as $key => $val ) { |
|
1407 | + if ( $val == 'false' ) { |
|
1408 | + $options[ $key ] = false; |
|
1409 | + } elseif ( $val == 'true' ) { |
|
1410 | + $options[ $key ] = true; |
|
1411 | + } |
|
1412 | + } |
|
1413 | + |
|
1414 | + return $options; |
|
1415 | + } |
|
1416 | + |
|
1417 | + /** |
|
1418 | + * Get the argument values that are also filterable. |
|
1419 | + * |
|
1420 | + * @param $instance |
|
1421 | + * |
|
1422 | + * @since 1.0.12 Don't set checkbox default value if the value is empty. |
|
1423 | + * |
|
1424 | + * @return array |
|
1425 | + */ |
|
1426 | + public function argument_values( $instance ) { |
|
1427 | + $argument_values = array(); |
|
1428 | + |
|
1429 | + // set widget instance |
|
1430 | + $this->instance = $instance; |
|
1431 | + |
|
1432 | + if ( empty( $this->arguments ) ) { |
|
1433 | + $this->arguments = $this->get_arguments(); |
|
1434 | + } |
|
1435 | + |
|
1436 | + if ( ! empty( $this->arguments ) ) { |
|
1437 | + foreach ( $this->arguments as $key => $args ) { |
|
1438 | + // set the input name from the key |
|
1439 | + $args['name'] = $key; |
|
1440 | + // |
|
1441 | + $argument_values[ $key ] = isset( $instance[ $key ] ) ? $instance[ $key ] : ''; |
|
1442 | + if ( $args['type'] == 'checkbox' && $argument_values[ $key ] == '' ) { |
|
1443 | + // don't set default for an empty checkbox |
|
1444 | + } elseif ( $argument_values[ $key ] == '' && isset( $args['default'] ) ) { |
|
1445 | + $argument_values[ $key ] = $args['default']; |
|
1446 | + } |
|
1447 | + } |
|
1448 | + } |
|
1449 | + |
|
1450 | + return $argument_values; |
|
1451 | + } |
|
1452 | + |
|
1453 | + /** |
|
1454 | + * Set arguments in super duper. |
|
1455 | + * |
|
1456 | + * @since 1.0.0 |
|
1457 | + * |
|
1458 | + * @return array Set arguments. |
|
1459 | + */ |
|
1460 | + public function set_arguments() { |
|
1461 | + return $this->arguments; |
|
1462 | + } |
|
1463 | + |
|
1464 | + /** |
|
1465 | + * Get arguments in super duper. |
|
1466 | + * |
|
1467 | + * @since 1.0.0 |
|
1468 | + * |
|
1469 | + * @return array Get arguments. |
|
1470 | + */ |
|
1471 | + public function get_arguments() { |
|
1472 | + if ( empty( $this->arguments ) ) { |
|
1473 | + $this->arguments = $this->set_arguments(); |
|
1474 | + } |
|
1475 | + |
|
1476 | + $this->arguments = apply_filters( 'wp_super_duper_arguments', $this->arguments, $this->options, $this->instance ); |
|
1477 | + $this->arguments = $this->add_name_from_key( $this->arguments, true ); |
|
1478 | + |
|
1479 | + return $this->arguments; |
|
1480 | + } |
|
1481 | + |
|
1482 | + /** |
|
1483 | + * This is the main output class for all 3 items, widget, shortcode and block, it is extended in the calling class. |
|
1484 | + * |
|
1485 | + * @param array $args |
|
1486 | + * @param array $widget_args |
|
1487 | + * @param string $content |
|
1488 | + */ |
|
1489 | + public function output( $args = array(), $widget_args = array(), $content = '' ) { |
|
1490 | + |
|
1491 | + } |
|
1492 | + |
|
1493 | + /** |
|
1494 | + * Add the dynamic block code inline when the wp-block in enqueued. |
|
1495 | + */ |
|
1496 | + public function register_block() { |
|
1497 | + wp_add_inline_script( 'wp-blocks', $this->block() ); |
|
1498 | + if ( class_exists( 'SiteOrigin_Panels' ) ) { |
|
1499 | + wp_add_inline_script( 'wp-blocks', $this->siteorigin_js() ); |
|
1500 | + } |
|
1501 | + } |
|
1502 | + |
|
1503 | + /** |
|
1504 | + * Check if we need to show advanced options. |
|
1505 | + * |
|
1506 | + * @return bool |
|
1507 | + */ |
|
1508 | + public function block_show_advanced() { |
|
1509 | + |
|
1510 | + $show = false; |
|
1511 | + $arguments = $this->arguments; |
|
1512 | + |
|
1513 | + if ( empty( $arguments ) ) { |
|
1514 | + $arguments = $this->get_arguments(); |
|
1515 | + } |
|
1516 | + |
|
1517 | + if ( ! empty( $arguments ) ) { |
|
1518 | + foreach ( $arguments as $argument ) { |
|
1519 | + if ( isset( $argument['advanced'] ) && $argument['advanced'] ) { |
|
1520 | + $show = true; |
|
1521 | + break; // no need to continue if we know we have it |
|
1522 | + } |
|
1523 | + } |
|
1524 | + } |
|
1525 | + |
|
1526 | + return $show; |
|
1527 | + } |
|
1528 | + |
|
1529 | + /** |
|
1530 | + * Get the url path to the current folder. |
|
1531 | + * |
|
1532 | + * @return string |
|
1533 | + */ |
|
1534 | + public function get_url() { |
|
1535 | + |
|
1536 | + $url = $this->url; |
|
1537 | + |
|
1538 | + if ( ! $url ) { |
|
1539 | + // check if we are inside a plugin |
|
1540 | + $file_dir = str_replace( "/includes", "", dirname( __FILE__ ) ); |
|
1541 | + |
|
1542 | + $dir_parts = explode( "/wp-content/", $file_dir ); |
|
1543 | + $url_parts = explode( "/wp-content/", plugins_url() ); |
|
1544 | + |
|
1545 | + if ( ! empty( $url_parts[0] ) && ! empty( $dir_parts[1] ) ) { |
|
1546 | + $url = trailingslashit( $url_parts[0] . "/wp-content/" . $dir_parts[1] ); |
|
1547 | + $this->url = $url; |
|
1548 | + } |
|
1549 | + } |
|
1550 | + |
|
1551 | + |
|
1552 | + return $url; |
|
1553 | + } |
|
1554 | + |
|
1555 | + /** |
|
1556 | + * Generate the block icon. |
|
1557 | + * |
|
1558 | + * Enables the use of Font Awesome icons. |
|
1559 | + * |
|
1560 | + * @note xlink:href is actually deprecated but href is not supported by all so we use both. |
|
1561 | + * |
|
1562 | + * @param $icon |
|
1563 | + * |
|
1564 | + * @since 1.1.0 |
|
1565 | + * @return string |
|
1566 | + */ |
|
1567 | + public function get_block_icon( $icon ) { |
|
1568 | + |
|
1569 | + // check if we have a Font Awesome icon |
|
1570 | + $fa_type = ''; |
|
1571 | + if ( substr( $icon, 0, 7 ) === "fas fa-" ) { |
|
1572 | + $fa_type = 'solid'; |
|
1573 | + } elseif ( substr( $icon, 0, 7 ) === "far fa-" ) { |
|
1574 | + $fa_type = 'regular'; |
|
1575 | + } elseif ( substr( $icon, 0, 7 ) === "fab fa-" ) { |
|
1576 | + $fa_type = 'brands'; |
|
1577 | + } else { |
|
1578 | + $icon = "'" . $icon . "'"; |
|
1579 | + } |
|
1580 | + |
|
1581 | + // set the icon if we found one |
|
1582 | + if ( $fa_type ) { |
|
1583 | + $fa_icon = str_replace( array( "fas fa-", "far fa-", "fab fa-" ), "", $icon ); |
|
1584 | + $icon = "el('svg',{width: 20, height: 20, viewBox: '0 0 20 20'},el('use', {'xlink:href': '" . $this->get_url() . "icons/" . $fa_type . ".svg#" . $fa_icon . "','href': '" . $this->get_url() . "icons/" . $fa_type . ".svg#" . $fa_icon . "'}))"; |
|
1585 | + } |
|
1586 | + |
|
1587 | + return $icon; |
|
1588 | + } |
|
1589 | + |
|
1590 | + public function group_arguments( $arguments ) { |
|
1591 | 1591 | // echo '###';print_r($arguments); |
1592 | - if ( ! empty( $arguments ) ) { |
|
1593 | - $temp_arguments = array(); |
|
1594 | - $general = __( "General" ); |
|
1595 | - $add_sections = false; |
|
1596 | - foreach ( $arguments as $key => $args ) { |
|
1597 | - if ( isset( $args['group'] ) ) { |
|
1598 | - $temp_arguments[ $args['group'] ][ $key ] = $args; |
|
1599 | - $add_sections = true; |
|
1600 | - } else { |
|
1601 | - $temp_arguments[ $general ][ $key ] = $args; |
|
1602 | - } |
|
1603 | - } |
|
1604 | - |
|
1605 | - // only add sections if more than one |
|
1606 | - if ( $add_sections ) { |
|
1607 | - $arguments = $temp_arguments; |
|
1608 | - } |
|
1609 | - } |
|
1592 | + if ( ! empty( $arguments ) ) { |
|
1593 | + $temp_arguments = array(); |
|
1594 | + $general = __( "General" ); |
|
1595 | + $add_sections = false; |
|
1596 | + foreach ( $arguments as $key => $args ) { |
|
1597 | + if ( isset( $args['group'] ) ) { |
|
1598 | + $temp_arguments[ $args['group'] ][ $key ] = $args; |
|
1599 | + $add_sections = true; |
|
1600 | + } else { |
|
1601 | + $temp_arguments[ $general ][ $key ] = $args; |
|
1602 | + } |
|
1603 | + } |
|
1604 | + |
|
1605 | + // only add sections if more than one |
|
1606 | + if ( $add_sections ) { |
|
1607 | + $arguments = $temp_arguments; |
|
1608 | + } |
|
1609 | + } |
|
1610 | 1610 | |
1611 | 1611 | // echo '###';print_r($arguments); |
1612 | - return $arguments; |
|
1613 | - } |
|
1614 | - |
|
1615 | - |
|
1616 | - /** |
|
1617 | - * Output the JS for building the dynamic Guntenberg block. |
|
1618 | - * |
|
1619 | - * @since 1.0.4 Added block_wrap property which will set the block wrapping output element ie: div, span, p or empty for no wrap. |
|
1620 | - * @since 1.0.9 Save numbers as numbers and not strings. |
|
1621 | - * @since 1.1.0 Font Awesome classes can be used for icons. |
|
1622 | - * @return mixed |
|
1623 | - */ |
|
1624 | - public function block() { |
|
1625 | - ob_start(); |
|
1626 | - |
|
1627 | - $show_advanced = $this->block_show_advanced(); |
|
1628 | - ?> |
|
1612 | + return $arguments; |
|
1613 | + } |
|
1614 | + |
|
1615 | + |
|
1616 | + /** |
|
1617 | + * Output the JS for building the dynamic Guntenberg block. |
|
1618 | + * |
|
1619 | + * @since 1.0.4 Added block_wrap property which will set the block wrapping output element ie: div, span, p or empty for no wrap. |
|
1620 | + * @since 1.0.9 Save numbers as numbers and not strings. |
|
1621 | + * @since 1.1.0 Font Awesome classes can be used for icons. |
|
1622 | + * @return mixed |
|
1623 | + */ |
|
1624 | + public function block() { |
|
1625 | + ob_start(); |
|
1626 | + |
|
1627 | + $show_advanced = $this->block_show_advanced(); |
|
1628 | + ?> |
|
1629 | 1629 | <script> |
1630 | 1630 | /** |
1631 | 1631 | * BLOCK: Basic |
@@ -1669,97 +1669,97 @@ discard block |
||
1669 | 1669 | icon: <?php echo $this->get_block_icon( $this->options['block-icon'] );?>,//'<?php echo isset( $this->options['block-icon'] ) ? esc_attr( $this->options['block-icon'] ) : 'shield-alt';?>', // Block icon from Dashicons → https://developer.wordpress.org/resource/dashicons/. |
1670 | 1670 | supports: { |
1671 | 1671 | <?php |
1672 | - if ( isset( $this->options['block-supports'] ) ) { |
|
1673 | - echo $this->array_to_attributes( $this->options['block-supports'] ); |
|
1674 | - } |
|
1675 | - ?> |
|
1672 | + if ( isset( $this->options['block-supports'] ) ) { |
|
1673 | + echo $this->array_to_attributes( $this->options['block-supports'] ); |
|
1674 | + } |
|
1675 | + ?> |
|
1676 | 1676 | }, |
1677 | 1677 | category: '<?php echo isset( $this->options['block-category'] ) ? esc_attr( $this->options['block-category'] ) : 'common';?>', // Block category — Group blocks together based on common traits E.g. common, formatting, layout widgets, embed. |
1678 | 1678 | <?php if ( isset( $this->options['block-keywords'] ) ) { |
1679 | - echo "keywords : " . $this->options['block-keywords'] . ","; |
|
1680 | - }?> |
|
1679 | + echo "keywords : " . $this->options['block-keywords'] . ","; |
|
1680 | + }?> |
|
1681 | 1681 | |
1682 | 1682 | <?php |
1683 | 1683 | |
1684 | - // maybe set no_wrap |
|
1685 | - $no_wrap = isset( $this->options['no_wrap'] ) && $this->options['no_wrap'] ? true : false; |
|
1686 | - if ( isset( $this->arguments['no_wrap'] ) && $this->arguments['no_wrap'] ) { |
|
1687 | - $no_wrap = true; |
|
1688 | - } |
|
1689 | - if ( $no_wrap ) { |
|
1690 | - $this->options['block-wrap'] = ''; |
|
1691 | - } |
|
1684 | + // maybe set no_wrap |
|
1685 | + $no_wrap = isset( $this->options['no_wrap'] ) && $this->options['no_wrap'] ? true : false; |
|
1686 | + if ( isset( $this->arguments['no_wrap'] ) && $this->arguments['no_wrap'] ) { |
|
1687 | + $no_wrap = true; |
|
1688 | + } |
|
1689 | + if ( $no_wrap ) { |
|
1690 | + $this->options['block-wrap'] = ''; |
|
1691 | + } |
|
1692 | 1692 | |
1693 | 1693 | |
1694 | 1694 | |
1695 | - $show_alignment = false; |
|
1696 | - // align feature |
|
1697 | - /*echo "supports: {"; |
|
1695 | + $show_alignment = false; |
|
1696 | + // align feature |
|
1697 | + /*echo "supports: {"; |
|
1698 | 1698 | echo " align: true,"; |
1699 | 1699 | echo " html: false"; |
1700 | 1700 | echo "},";*/ |
1701 | 1701 | |
1702 | - if ( ! empty( $this->arguments ) ) { |
|
1703 | - echo "attributes : {"; |
|
1704 | - |
|
1705 | - if ( $show_advanced ) { |
|
1706 | - echo "show_advanced: {"; |
|
1707 | - echo " type: 'boolean',"; |
|
1708 | - echo " default: false,"; |
|
1709 | - echo "},"; |
|
1710 | - } |
|
1711 | - |
|
1712 | - // block wrap element |
|
1713 | - if ( ! empty( $this->options['block-wrap'] ) ) { //@todo we should validate this? |
|
1714 | - echo "block_wrap: {"; |
|
1715 | - echo " type: 'string',"; |
|
1716 | - echo " default: '" . esc_attr( $this->options['block-wrap'] ) . "',"; |
|
1717 | - echo "},"; |
|
1718 | - } |
|
1719 | - |
|
1720 | - foreach ( $this->arguments as $key => $args ) { |
|
1721 | - |
|
1722 | - // set if we should show alignment |
|
1723 | - if ( $key == 'alignment' ) { |
|
1724 | - $show_alignment = true; |
|
1725 | - } |
|
1726 | - |
|
1727 | - $extra = ''; |
|
1728 | - |
|
1729 | - if ( $args['type'] == 'checkbox' ) { |
|
1730 | - $type = 'boolean'; |
|
1731 | - $default = isset( $args['default'] ) && $args['default'] ? 'true' : 'false'; |
|
1732 | - } elseif ( $args['type'] == 'number' ) { |
|
1733 | - $type = 'number'; |
|
1734 | - $default = isset( $args['default'] ) ? "'" . $args['default'] . "'" : "''"; |
|
1735 | - } elseif ( $args['type'] == 'select' && ! empty( $args['multiple'] ) ) { |
|
1736 | - $type = 'array'; |
|
1737 | - if ( is_array( $args['default'] ) ) { |
|
1738 | - $default = isset( $args['default'] ) ? "['" . implode( "','", $args['default'] ) . "']" : "[]"; |
|
1739 | - } else { |
|
1740 | - $default = isset( $args['default'] ) ? "'" . $args['default'] . "'" : "''"; |
|
1741 | - } |
|
1742 | - } elseif ( $args['type'] == 'multiselect' ) { |
|
1743 | - $type = 'array'; |
|
1744 | - $default = isset( $args['default'] ) ? "'" . $args['default'] . "'" : "''"; |
|
1745 | - } else { |
|
1746 | - $type = 'string'; |
|
1747 | - $default = isset( $args['default'] ) ? "'" . $args['default'] . "'" : "''"; |
|
1748 | - } |
|
1749 | - echo $key . " : {"; |
|
1750 | - echo "type : '$type',"; |
|
1751 | - echo "default : $default,"; |
|
1752 | - echo "},"; |
|
1753 | - } |
|
1754 | - |
|
1755 | - echo "content : {type : 'string',default: 'Please select the attributes in the block settings'},"; |
|
1756 | - echo "className: { type: 'string', default: '' },"; |
|
1757 | - |
|
1758 | - echo "},"; |
|
1759 | - |
|
1760 | - } |
|
1761 | - |
|
1762 | - ?> |
|
1702 | + if ( ! empty( $this->arguments ) ) { |
|
1703 | + echo "attributes : {"; |
|
1704 | + |
|
1705 | + if ( $show_advanced ) { |
|
1706 | + echo "show_advanced: {"; |
|
1707 | + echo " type: 'boolean',"; |
|
1708 | + echo " default: false,"; |
|
1709 | + echo "},"; |
|
1710 | + } |
|
1711 | + |
|
1712 | + // block wrap element |
|
1713 | + if ( ! empty( $this->options['block-wrap'] ) ) { //@todo we should validate this? |
|
1714 | + echo "block_wrap: {"; |
|
1715 | + echo " type: 'string',"; |
|
1716 | + echo " default: '" . esc_attr( $this->options['block-wrap'] ) . "',"; |
|
1717 | + echo "},"; |
|
1718 | + } |
|
1719 | + |
|
1720 | + foreach ( $this->arguments as $key => $args ) { |
|
1721 | + |
|
1722 | + // set if we should show alignment |
|
1723 | + if ( $key == 'alignment' ) { |
|
1724 | + $show_alignment = true; |
|
1725 | + } |
|
1726 | + |
|
1727 | + $extra = ''; |
|
1728 | + |
|
1729 | + if ( $args['type'] == 'checkbox' ) { |
|
1730 | + $type = 'boolean'; |
|
1731 | + $default = isset( $args['default'] ) && $args['default'] ? 'true' : 'false'; |
|
1732 | + } elseif ( $args['type'] == 'number' ) { |
|
1733 | + $type = 'number'; |
|
1734 | + $default = isset( $args['default'] ) ? "'" . $args['default'] . "'" : "''"; |
|
1735 | + } elseif ( $args['type'] == 'select' && ! empty( $args['multiple'] ) ) { |
|
1736 | + $type = 'array'; |
|
1737 | + if ( is_array( $args['default'] ) ) { |
|
1738 | + $default = isset( $args['default'] ) ? "['" . implode( "','", $args['default'] ) . "']" : "[]"; |
|
1739 | + } else { |
|
1740 | + $default = isset( $args['default'] ) ? "'" . $args['default'] . "'" : "''"; |
|
1741 | + } |
|
1742 | + } elseif ( $args['type'] == 'multiselect' ) { |
|
1743 | + $type = 'array'; |
|
1744 | + $default = isset( $args['default'] ) ? "'" . $args['default'] . "'" : "''"; |
|
1745 | + } else { |
|
1746 | + $type = 'string'; |
|
1747 | + $default = isset( $args['default'] ) ? "'" . $args['default'] . "'" : "''"; |
|
1748 | + } |
|
1749 | + echo $key . " : {"; |
|
1750 | + echo "type : '$type',"; |
|
1751 | + echo "default : $default,"; |
|
1752 | + echo "},"; |
|
1753 | + } |
|
1754 | + |
|
1755 | + echo "content : {type : 'string',default: 'Please select the attributes in the block settings'},"; |
|
1756 | + echo "className: { type: 'string', default: '' },"; |
|
1757 | + |
|
1758 | + echo "},"; |
|
1759 | + |
|
1760 | + } |
|
1761 | + |
|
1762 | + ?> |
|
1763 | 1763 | |
1764 | 1764 | // The "edit" property must be a valid function. |
1765 | 1765 | edit: function (props) { |
@@ -1767,9 +1767,9 @@ discard block |
||
1767 | 1767 | |
1768 | 1768 | var $value = ''; |
1769 | 1769 | <?php |
1770 | - // if we have a post_type and a category then link them |
|
1771 | - if( isset($this->arguments['post_type']) && isset($this->arguments['category']) && !empty($this->arguments['category']['post_type_linked']) ){ |
|
1772 | - ?> |
|
1770 | + // if we have a post_type and a category then link them |
|
1771 | + if( isset($this->arguments['post_type']) && isset($this->arguments['category']) && !empty($this->arguments['category']['post_type_linked']) ){ |
|
1772 | + ?> |
|
1773 | 1773 | if(typeof(prev_attributes[props.id]) != 'undefined' ){ |
1774 | 1774 | $pt = props.attributes.post_type; |
1775 | 1775 | if(post_type_rest_slugs.length){ |
@@ -1853,8 +1853,8 @@ discard block |
||
1853 | 1853 | 'shortcode': '<?php echo $this->options['base_id'];?>', |
1854 | 1854 | 'attributes': props.attributes, |
1855 | 1855 | 'post_id': <?php global $post; if ( isset( $post->ID ) ) { |
1856 | - echo $post->ID; |
|
1857 | - }?>, |
|
1856 | + echo $post->ID; |
|
1857 | + }?>, |
|
1858 | 1858 | '_ajax_nonce': '<?php echo wp_create_nonce( 'super_duper_output_shortcode' );?>' |
1859 | 1859 | }; |
1860 | 1860 | |
@@ -1906,10 +1906,10 @@ discard block |
||
1906 | 1906 | |
1907 | 1907 | <?php |
1908 | 1908 | |
1909 | - if(! empty( $this->arguments )){ |
|
1909 | + if(! empty( $this->arguments )){ |
|
1910 | 1910 | |
1911 | - if ( $show_advanced ) { |
|
1912 | - ?> |
|
1911 | + if ( $show_advanced ) { |
|
1912 | + ?> |
|
1913 | 1913 | el('div', { |
1914 | 1914 | style: {'padding-left': '16px','padding-right': '16px'} |
1915 | 1915 | }, |
@@ -1927,72 +1927,72 @@ discard block |
||
1927 | 1927 | , |
1928 | 1928 | <?php |
1929 | 1929 | |
1930 | - } |
|
1930 | + } |
|
1931 | 1931 | |
1932 | - $arguments = $this->group_arguments( $this->arguments ); |
|
1932 | + $arguments = $this->group_arguments( $this->arguments ); |
|
1933 | 1933 | |
1934 | - // Do we have sections? |
|
1935 | - $has_sections = $arguments == $this->arguments ? false : true; |
|
1934 | + // Do we have sections? |
|
1935 | + $has_sections = $arguments == $this->arguments ? false : true; |
|
1936 | 1936 | |
1937 | 1937 | |
1938 | - if($has_sections){ |
|
1939 | - $panel_count = 0; |
|
1940 | - foreach($arguments as $key => $args){ |
|
1941 | - ?> |
|
1938 | + if($has_sections){ |
|
1939 | + $panel_count = 0; |
|
1940 | + foreach($arguments as $key => $args){ |
|
1941 | + ?> |
|
1942 | 1942 | el(wp.components.PanelBody, { |
1943 | 1943 | title: '<?php esc_attr_e( $key ); ?>', |
1944 | 1944 | initialOpen: <?php if ( $panel_count ) { |
1945 | - echo "false"; |
|
1946 | - } else { |
|
1947 | - echo "true"; |
|
1948 | - }?> |
|
1945 | + echo "false"; |
|
1946 | + } else { |
|
1947 | + echo "true"; |
|
1948 | + }?> |
|
1949 | 1949 | }, |
1950 | 1950 | <?php |
1951 | 1951 | |
1952 | - foreach ( $args as $k => $a ) { |
|
1953 | - $this->build_block_arguments( $k, $a ); |
|
1954 | - } |
|
1955 | - ?> |
|
1952 | + foreach ( $args as $k => $a ) { |
|
1953 | + $this->build_block_arguments( $k, $a ); |
|
1954 | + } |
|
1955 | + ?> |
|
1956 | 1956 | ), |
1957 | 1957 | <?php |
1958 | - $panel_count ++; |
|
1958 | + $panel_count ++; |
|
1959 | 1959 | |
1960 | - } |
|
1961 | - }else { |
|
1962 | - ?> |
|
1960 | + } |
|
1961 | + }else { |
|
1962 | + ?> |
|
1963 | 1963 | el(wp.components.PanelBody, { |
1964 | 1964 | title: '<?php esc_attr_e( "Settings" ); ?>', |
1965 | 1965 | initialOpen: true |
1966 | 1966 | }, |
1967 | 1967 | <?php |
1968 | - foreach ( $this->arguments as $key => $args ) { |
|
1969 | - $this->build_block_arguments( $key, $args ); |
|
1970 | - } |
|
1971 | - ?> |
|
1968 | + foreach ( $this->arguments as $key => $args ) { |
|
1969 | + $this->build_block_arguments( $key, $args ); |
|
1970 | + } |
|
1971 | + ?> |
|
1972 | 1972 | ), |
1973 | 1973 | <?php |
1974 | - } |
|
1974 | + } |
|
1975 | 1975 | |
1976 | - } |
|
1977 | - ?> |
|
1976 | + } |
|
1977 | + ?> |
|
1978 | 1978 | |
1979 | 1979 | ), |
1980 | 1980 | |
1981 | 1981 | <?php |
1982 | - // If the user sets block-output array then build it |
|
1983 | - if ( ! empty( $this->options['block-output'] ) ) { |
|
1984 | - $this->block_element( $this->options['block-output'] ); |
|
1985 | - }else{ |
|
1986 | - // if no block-output is set then we try and get the shortcode html output via ajax. |
|
1987 | - ?> |
|
1982 | + // If the user sets block-output array then build it |
|
1983 | + if ( ! empty( $this->options['block-output'] ) ) { |
|
1984 | + $this->block_element( $this->options['block-output'] ); |
|
1985 | + }else{ |
|
1986 | + // if no block-output is set then we try and get the shortcode html output via ajax. |
|
1987 | + ?> |
|
1988 | 1988 | el('div', { |
1989 | 1989 | dangerouslySetInnerHTML: {__html: onChangeContent()}, |
1990 | 1990 | className: props.className, |
1991 | 1991 | style: {'minHeight': '30px'} |
1992 | 1992 | }) |
1993 | 1993 | <?php |
1994 | - } |
|
1995 | - ?> |
|
1994 | + } |
|
1995 | + ?> |
|
1996 | 1996 | ]; // end return |
1997 | 1997 | }, |
1998 | 1998 | |
@@ -2010,10 +2010,10 @@ discard block |
||
2010 | 2010 | $html = ''; |
2011 | 2011 | <?php |
2012 | 2012 | |
2013 | - if(! empty( $this->arguments )){ |
|
2013 | + if(! empty( $this->arguments )){ |
|
2014 | 2014 | |
2015 | - foreach($this->arguments as $key => $args){ |
|
2016 | - ?> |
|
2015 | + foreach($this->arguments as $key => $args){ |
|
2016 | + ?> |
|
2017 | 2017 | if (attr.hasOwnProperty("<?php echo esc_attr( $key );?>")) { |
2018 | 2018 | if ('<?php echo esc_attr( $key );?>' == 'html') { |
2019 | 2019 | $html = attr.<?php echo esc_attr( $key );?>; |
@@ -2022,10 +2022,10 @@ discard block |
||
2022 | 2022 | } |
2023 | 2023 | } |
2024 | 2024 | <?php |
2025 | - } |
|
2026 | - } |
|
2025 | + } |
|
2026 | + } |
|
2027 | 2027 | |
2028 | - ?> |
|
2028 | + ?> |
|
2029 | 2029 | content += "]"; |
2030 | 2030 | |
2031 | 2031 | // if has html element |
@@ -2048,20 +2048,20 @@ discard block |
||
2048 | 2048 | } |
2049 | 2049 | |
2050 | 2050 | <?php |
2051 | - if(isset( $this->options['block-wrap'] ) && $this->options['block-wrap'] == ''){ |
|
2052 | - ?> |
|
2051 | + if(isset( $this->options['block-wrap'] ) && $this->options['block-wrap'] == ''){ |
|
2052 | + ?> |
|
2053 | 2053 | return content; |
2054 | 2054 | <?php |
2055 | - }else{ |
|
2056 | - ?> |
|
2055 | + }else{ |
|
2056 | + ?> |
|
2057 | 2057 | var block_wrap = 'div'; |
2058 | 2058 | if (attr.hasOwnProperty("block_wrap")) { |
2059 | 2059 | block_wrap = attr.block_wrap; |
2060 | 2060 | } |
2061 | 2061 | return el(block_wrap, {dangerouslySetInnerHTML: {__html: content}, className: align}); |
2062 | 2062 | <?php |
2063 | - } |
|
2064 | - ?> |
|
2063 | + } |
|
2064 | + ?> |
|
2065 | 2065 | |
2066 | 2066 | |
2067 | 2067 | } |
@@ -2069,48 +2069,48 @@ discard block |
||
2069 | 2069 | })(); |
2070 | 2070 | </script> |
2071 | 2071 | <?php |
2072 | - $output = ob_get_clean(); |
|
2072 | + $output = ob_get_clean(); |
|
2073 | 2073 | |
2074 | - /* |
|
2074 | + /* |
|
2075 | 2075 | * We only add the <script> tags for code highlighting, so we strip them from the output. |
2076 | 2076 | */ |
2077 | 2077 | |
2078 | - return str_replace( array( |
|
2079 | - '<script>', |
|
2080 | - '</script>' |
|
2081 | - ), '', $output ); |
|
2082 | - } |
|
2083 | - |
|
2084 | - public function build_block_arguments( $key, $args ) { |
|
2085 | - $custom_attributes = ! empty( $args['custom_attributes'] ) ? $this->array_to_attributes( $args['custom_attributes'] ) : ''; |
|
2086 | - $options = ''; |
|
2087 | - $extra = ''; |
|
2088 | - $require = ''; |
|
2089 | - |
|
2090 | - // `content` is a protected and special argument |
|
2091 | - if ( $key == 'content' ) { |
|
2092 | - return; |
|
2093 | - } |
|
2094 | - |
|
2095 | - // require advanced |
|
2096 | - $require_advanced = ! empty( $args['advanced'] ) ? "props.attributes.show_advanced && " : ""; |
|
2097 | - |
|
2098 | - // element require |
|
2099 | - $element_require = ! empty( $args['element_require'] ) ? $this->block_props_replace( $args['element_require'], true ) . " && " : ""; |
|
2100 | - |
|
2101 | - |
|
2102 | - $onchange = "props.setAttributes({ $key: $key } )"; |
|
2103 | - $onchangecomplete = ""; |
|
2104 | - $value = "props.attributes.$key"; |
|
2105 | - $text_type = array( 'text', 'password', 'number', 'email', 'tel', 'url', 'colorx' ); |
|
2106 | - if ( in_array( $args['type'], $text_type ) ) { |
|
2107 | - $type = 'TextControl'; |
|
2108 | - // Save numbers as numbers and not strings |
|
2109 | - if ( $args['type'] == 'number' ) { |
|
2110 | - $onchange = "props.setAttributes({ $key: Number($key) } )"; |
|
2111 | - } |
|
2112 | - } |
|
2113 | - /* |
|
2078 | + return str_replace( array( |
|
2079 | + '<script>', |
|
2080 | + '</script>' |
|
2081 | + ), '', $output ); |
|
2082 | + } |
|
2083 | + |
|
2084 | + public function build_block_arguments( $key, $args ) { |
|
2085 | + $custom_attributes = ! empty( $args['custom_attributes'] ) ? $this->array_to_attributes( $args['custom_attributes'] ) : ''; |
|
2086 | + $options = ''; |
|
2087 | + $extra = ''; |
|
2088 | + $require = ''; |
|
2089 | + |
|
2090 | + // `content` is a protected and special argument |
|
2091 | + if ( $key == 'content' ) { |
|
2092 | + return; |
|
2093 | + } |
|
2094 | + |
|
2095 | + // require advanced |
|
2096 | + $require_advanced = ! empty( $args['advanced'] ) ? "props.attributes.show_advanced && " : ""; |
|
2097 | + |
|
2098 | + // element require |
|
2099 | + $element_require = ! empty( $args['element_require'] ) ? $this->block_props_replace( $args['element_require'], true ) . " && " : ""; |
|
2100 | + |
|
2101 | + |
|
2102 | + $onchange = "props.setAttributes({ $key: $key } )"; |
|
2103 | + $onchangecomplete = ""; |
|
2104 | + $value = "props.attributes.$key"; |
|
2105 | + $text_type = array( 'text', 'password', 'number', 'email', 'tel', 'url', 'colorx' ); |
|
2106 | + if ( in_array( $args['type'], $text_type ) ) { |
|
2107 | + $type = 'TextControl'; |
|
2108 | + // Save numbers as numbers and not strings |
|
2109 | + if ( $args['type'] == 'number' ) { |
|
2110 | + $onchange = "props.setAttributes({ $key: Number($key) } )"; |
|
2111 | + } |
|
2112 | + } |
|
2113 | + /* |
|
2114 | 2114 | * https://www.wptricks.com/question/set-current-tab-on-a-gutenberg-tabpanel-component-from-outside-that-component/ es5 layout |
2115 | 2115 | elseif($args['type']=='tabs'){ |
2116 | 2116 | ?> |
@@ -2144,80 +2144,80 @@ discard block |
||
2144 | 2144 | return; |
2145 | 2145 | } |
2146 | 2146 | */ |
2147 | - elseif ( $args['type'] == 'color' ) { |
|
2148 | - $type = 'ColorPicker'; |
|
2149 | - $onchange = ""; |
|
2150 | - $extra = "color: $value,"; |
|
2151 | - if(!empty($args['disable_alpha'])){ |
|
2152 | - $extra .= "disableAlpha: true,"; |
|
2153 | - } |
|
2154 | - $onchangecomplete = "onChangeComplete: function($key) { |
|
2147 | + elseif ( $args['type'] == 'color' ) { |
|
2148 | + $type = 'ColorPicker'; |
|
2149 | + $onchange = ""; |
|
2150 | + $extra = "color: $value,"; |
|
2151 | + if(!empty($args['disable_alpha'])){ |
|
2152 | + $extra .= "disableAlpha: true,"; |
|
2153 | + } |
|
2154 | + $onchangecomplete = "onChangeComplete: function($key) { |
|
2155 | 2155 | value = $key.rgb.a && $key.rgb.a < 1 ? \"rgba(\"+$key.rgb.r+\",\"+$key.rgb.g+\",\"+$key.rgb.b+\",\"+$key.rgb.a+\")\" : $key.hex; |
2156 | 2156 | props.setAttributes({ |
2157 | 2157 | $key: value |
2158 | 2158 | }); |
2159 | 2159 | },"; |
2160 | - } |
|
2161 | - elseif ( $args['type'] == 'checkbox' ) { |
|
2162 | - $type = 'CheckboxControl'; |
|
2163 | - $extra .= "checked: props.attributes.$key,"; |
|
2164 | - $onchange = "props.setAttributes({ $key: ! props.attributes.$key } )"; |
|
2165 | - } elseif ( $args['type'] == 'textarea' ) { |
|
2166 | - $type = 'TextareaControl'; |
|
2167 | - } elseif ( $args['type'] == 'select' || $args['type'] == 'multiselect' ) { |
|
2168 | - $type = 'SelectControl'; |
|
2169 | - |
|
2170 | - if($args['name'] == 'category' && !empty($args['post_type_linked'])){ |
|
2171 | - $options .= "options: taxonomies_".str_replace("-","_", $this->id).","; |
|
2172 | - }elseif($args['name'] == 'sort_by' && !empty($args['post_type_linked'])){ |
|
2173 | - $options .= "options: sort_by_".str_replace("-","_", $this->id).","; |
|
2174 | - }else { |
|
2175 | - |
|
2176 | - if ( ! empty( $args['options'] ) ) { |
|
2177 | - $options .= "options: ["; |
|
2178 | - foreach ( $args['options'] as $option_val => $option_label ) { |
|
2179 | - $options .= "{ value: '" . esc_attr( $option_val ) . "', label: '" . addslashes( $option_label ) . "' },"; |
|
2180 | - } |
|
2181 | - $options .= "],"; |
|
2182 | - } |
|
2183 | - } |
|
2184 | - if ( isset( $args['multiple'] ) && $args['multiple'] ) { //@todo multiselect does not work at the moment: https://github.com/WordPress/gutenberg/issues/5550 |
|
2185 | - $extra .= ' multiple: true, '; |
|
2186 | - } |
|
2187 | - } elseif ( $args['type'] == 'alignment' ) { |
|
2188 | - $type = 'AlignmentToolbar'; // @todo this does not seem to work but cant find a example |
|
2189 | - } else { |
|
2190 | - return;// if we have not implemented the control then don't break the JS. |
|
2191 | - } |
|
2192 | - |
|
2193 | - |
|
2194 | - |
|
2195 | - // color input does not show the labels so we add them |
|
2196 | - if($args['type']=='color'){ |
|
2197 | - // add show only if advanced |
|
2198 | - echo $require_advanced; |
|
2199 | - // add setting require if defined |
|
2200 | - echo $element_require; |
|
2201 | - echo "el('div', {style: {'marginBottom': '8px'}}, '".addslashes( $args['title'] )."'),"; |
|
2202 | - } |
|
2203 | - |
|
2204 | - // add show only if advanced |
|
2205 | - echo $require_advanced; |
|
2206 | - // add setting require if defined |
|
2207 | - echo $element_require; |
|
2208 | - ?> |
|
2160 | + } |
|
2161 | + elseif ( $args['type'] == 'checkbox' ) { |
|
2162 | + $type = 'CheckboxControl'; |
|
2163 | + $extra .= "checked: props.attributes.$key,"; |
|
2164 | + $onchange = "props.setAttributes({ $key: ! props.attributes.$key } )"; |
|
2165 | + } elseif ( $args['type'] == 'textarea' ) { |
|
2166 | + $type = 'TextareaControl'; |
|
2167 | + } elseif ( $args['type'] == 'select' || $args['type'] == 'multiselect' ) { |
|
2168 | + $type = 'SelectControl'; |
|
2169 | + |
|
2170 | + if($args['name'] == 'category' && !empty($args['post_type_linked'])){ |
|
2171 | + $options .= "options: taxonomies_".str_replace("-","_", $this->id).","; |
|
2172 | + }elseif($args['name'] == 'sort_by' && !empty($args['post_type_linked'])){ |
|
2173 | + $options .= "options: sort_by_".str_replace("-","_", $this->id).","; |
|
2174 | + }else { |
|
2175 | + |
|
2176 | + if ( ! empty( $args['options'] ) ) { |
|
2177 | + $options .= "options: ["; |
|
2178 | + foreach ( $args['options'] as $option_val => $option_label ) { |
|
2179 | + $options .= "{ value: '" . esc_attr( $option_val ) . "', label: '" . addslashes( $option_label ) . "' },"; |
|
2180 | + } |
|
2181 | + $options .= "],"; |
|
2182 | + } |
|
2183 | + } |
|
2184 | + if ( isset( $args['multiple'] ) && $args['multiple'] ) { //@todo multiselect does not work at the moment: https://github.com/WordPress/gutenberg/issues/5550 |
|
2185 | + $extra .= ' multiple: true, '; |
|
2186 | + } |
|
2187 | + } elseif ( $args['type'] == 'alignment' ) { |
|
2188 | + $type = 'AlignmentToolbar'; // @todo this does not seem to work but cant find a example |
|
2189 | + } else { |
|
2190 | + return;// if we have not implemented the control then don't break the JS. |
|
2191 | + } |
|
2192 | + |
|
2193 | + |
|
2194 | + |
|
2195 | + // color input does not show the labels so we add them |
|
2196 | + if($args['type']=='color'){ |
|
2197 | + // add show only if advanced |
|
2198 | + echo $require_advanced; |
|
2199 | + // add setting require if defined |
|
2200 | + echo $element_require; |
|
2201 | + echo "el('div', {style: {'marginBottom': '8px'}}, '".addslashes( $args['title'] )."'),"; |
|
2202 | + } |
|
2203 | + |
|
2204 | + // add show only if advanced |
|
2205 | + echo $require_advanced; |
|
2206 | + // add setting require if defined |
|
2207 | + echo $element_require; |
|
2208 | + ?> |
|
2209 | 2209 | el( wp.components.<?php echo $type; ?>, { |
2210 | 2210 | label: '<?php echo addslashes( $args['title'] ); ?>', |
2211 | 2211 | help: '<?php if ( isset( $args['desc'] ) ) { |
2212 | - echo addslashes( $args['desc'] ); |
|
2213 | - } ?>', |
|
2212 | + echo addslashes( $args['desc'] ); |
|
2213 | + } ?>', |
|
2214 | 2214 | value: <?php echo $value; ?>, |
2215 | 2215 | <?php if ( $type == 'TextControl' && $args['type'] != 'text' ) { |
2216 | - echo "type: '" . addslashes( $args['type'] ) . "',"; |
|
2217 | - } ?> |
|
2216 | + echo "type: '" . addslashes( $args['type'] ) . "',"; |
|
2217 | + } ?> |
|
2218 | 2218 | <?php if ( ! empty( $args['placeholder'] ) ) { |
2219 | - echo "placeholder: '" . addslashes( $args['placeholder'] ) . "',"; |
|
2220 | - } ?> |
|
2219 | + echo "placeholder: '" . addslashes( $args['placeholder'] ) . "',"; |
|
2220 | + } ?> |
|
2221 | 2221 | <?php echo $options; ?> |
2222 | 2222 | <?php echo $extra; ?> |
2223 | 2223 | <?php echo $custom_attributes; ?> |
@@ -2228,534 +2228,534 @@ discard block |
||
2228 | 2228 | } ), |
2229 | 2229 | <?php |
2230 | 2230 | |
2231 | - } |
|
2232 | - |
|
2233 | - /** |
|
2234 | - * Convert an array of attributes to block string. |
|
2235 | - * |
|
2236 | - * @todo there is prob a faster way to do this, also we could add some validation here. |
|
2237 | - * |
|
2238 | - * @param $custom_attributes |
|
2239 | - * |
|
2240 | - * @return string |
|
2241 | - */ |
|
2242 | - public function array_to_attributes( $custom_attributes, $html = false ) { |
|
2243 | - $attributes = ''; |
|
2244 | - if ( ! empty( $custom_attributes ) ) { |
|
2245 | - |
|
2246 | - if ( $html ) { |
|
2247 | - foreach ( $custom_attributes as $key => $val ) { |
|
2248 | - $attributes .= " $key='$val' "; |
|
2249 | - } |
|
2250 | - } else { |
|
2251 | - foreach ( $custom_attributes as $key => $val ) { |
|
2252 | - $attributes .= "'$key': '$val',"; |
|
2253 | - } |
|
2254 | - } |
|
2255 | - } |
|
2256 | - |
|
2257 | - return $attributes; |
|
2258 | - } |
|
2259 | - |
|
2260 | - /** |
|
2261 | - * A self looping function to create the output for JS block elements. |
|
2262 | - * |
|
2263 | - * This is what is output in the WP Editor visual view. |
|
2264 | - * |
|
2265 | - * @param $args |
|
2266 | - */ |
|
2267 | - public function block_element( $args ) { |
|
2268 | - |
|
2269 | - |
|
2270 | - if ( ! empty( $args ) ) { |
|
2271 | - foreach ( $args as $element => $new_args ) { |
|
2272 | - |
|
2273 | - if ( is_array( $new_args ) ) { // its an element |
|
2274 | - |
|
2275 | - |
|
2276 | - if ( isset( $new_args['element'] ) ) { |
|
2277 | - |
|
2278 | - if ( isset( $new_args['element_require'] ) ) { |
|
2279 | - echo str_replace( array( |
|
2280 | - "'+", |
|
2281 | - "+'" |
|
2282 | - ), '', $this->block_props_replace( $new_args['element_require'] ) ) . " && "; |
|
2283 | - unset( $new_args['element_require'] ); |
|
2284 | - } |
|
2285 | - |
|
2286 | - echo "\n el( '" . $new_args['element'] . "', {"; |
|
2287 | - |
|
2288 | - // get the attributes |
|
2289 | - foreach ( $new_args as $new_key => $new_value ) { |
|
2290 | - |
|
2291 | - |
|
2292 | - if ( $new_key == 'element' || $new_key == 'content' || $new_key == 'element_require' || $new_key == 'element_repeat' || is_array( $new_value ) ) { |
|
2293 | - // do nothing |
|
2294 | - } else { |
|
2295 | - echo $this->block_element( array( $new_key => $new_value ) ); |
|
2296 | - } |
|
2297 | - } |
|
2298 | - |
|
2299 | - echo "},";// end attributes |
|
2300 | - |
|
2301 | - // get the content |
|
2302 | - $first_item = 0; |
|
2303 | - foreach ( $new_args as $new_key => $new_value ) { |
|
2304 | - if ( $new_key === 'content' || is_array( $new_value ) ) { |
|
2305 | - |
|
2306 | - if ( $new_key === 'content' ) { |
|
2307 | - echo "'" . $this->block_props_replace( wp_slash( $new_value ) ) . "'"; |
|
2308 | - } |
|
2309 | - |
|
2310 | - if ( is_array( $new_value ) ) { |
|
2311 | - |
|
2312 | - if ( isset( $new_value['element_require'] ) ) { |
|
2313 | - echo str_replace( array( |
|
2314 | - "'+", |
|
2315 | - "+'" |
|
2316 | - ), '', $this->block_props_replace( $new_value['element_require'] ) ) . " && "; |
|
2317 | - unset( $new_value['element_require'] ); |
|
2318 | - } |
|
2319 | - |
|
2320 | - if ( isset( $new_value['element_repeat'] ) ) { |
|
2321 | - $x = 1; |
|
2322 | - while ( $x <= absint( $new_value['element_repeat'] ) ) { |
|
2323 | - $this->block_element( array( '' => $new_value ) ); |
|
2324 | - $x ++; |
|
2325 | - } |
|
2326 | - } else { |
|
2327 | - $this->block_element( array( '' => $new_value ) ); |
|
2328 | - } |
|
2329 | - } |
|
2330 | - $first_item ++; |
|
2331 | - } |
|
2332 | - } |
|
2333 | - |
|
2334 | - echo ")";// end content |
|
2335 | - |
|
2336 | - echo ", \n"; |
|
2337 | - |
|
2338 | - } |
|
2339 | - } else { |
|
2340 | - |
|
2341 | - if ( substr( $element, 0, 3 ) === "if_" ) { |
|
2342 | - echo str_replace( "if_", "", $element ) . ": " . $this->block_props_replace( $new_args, true ) . ","; |
|
2343 | - } elseif ( $element == 'style' ) { |
|
2344 | - echo $element . ": " . $this->block_props_replace( $new_args ) . ","; |
|
2345 | - } else { |
|
2346 | - echo $element . ": '" . $this->block_props_replace( $new_args ) . "',"; |
|
2347 | - } |
|
2348 | - |
|
2349 | - } |
|
2350 | - } |
|
2351 | - } |
|
2352 | - } |
|
2353 | - |
|
2354 | - /** |
|
2355 | - * Replace block attributes placeholders with the proper naming. |
|
2356 | - * |
|
2357 | - * @param $string |
|
2358 | - * |
|
2359 | - * @return mixed |
|
2360 | - */ |
|
2361 | - public function block_props_replace( $string, $no_wrap = false ) { |
|
2362 | - |
|
2363 | - if ( $no_wrap ) { |
|
2364 | - $string = str_replace( array( "[%", "%]" ), array( "props.attributes.", "" ), $string ); |
|
2365 | - } else { |
|
2366 | - $string = str_replace( array( "[%", "%]" ), array( "'+props.attributes.", "+'" ), $string ); |
|
2367 | - } |
|
2368 | - |
|
2369 | - return $string; |
|
2370 | - } |
|
2371 | - |
|
2372 | - /** |
|
2373 | - * Outputs the content of the widget |
|
2374 | - * |
|
2375 | - * @param array $args |
|
2376 | - * @param array $instance |
|
2377 | - */ |
|
2378 | - public function widget( $args, $instance ) { |
|
2379 | - |
|
2380 | - // get the filtered values |
|
2381 | - $argument_values = $this->argument_values( $instance ); |
|
2382 | - $argument_values = $this->string_to_bool( $argument_values ); |
|
2383 | - $output = $this->output( $argument_values, $args ); |
|
2384 | - |
|
2385 | - $no_wrap = false; |
|
2386 | - if ( isset( $argument_values['no_wrap'] ) && $argument_values['no_wrap'] ) { |
|
2387 | - $no_wrap = true; |
|
2388 | - } |
|
2389 | - |
|
2390 | - ob_start(); |
|
2391 | - if ( $output && ! $no_wrap ) { |
|
2392 | - |
|
2393 | - $class_original = $this->options['widget_ops']['classname']; |
|
2394 | - $class = $this->options['widget_ops']['classname']." sdel-".$this->get_instance_hash(); |
|
2395 | - |
|
2396 | - // Before widget |
|
2397 | - $before_widget = $args['before_widget']; |
|
2398 | - $before_widget = str_replace($class_original,$class,$before_widget); |
|
2399 | - $before_widget = apply_filters( 'wp_super_duper_before_widget', $before_widget, $args, $instance, $this ); |
|
2400 | - $before_widget = apply_filters( 'wp_super_duper_before_widget_' . $this->base_id, $before_widget, $args, $instance, $this ); |
|
2401 | - |
|
2402 | - // After widget |
|
2403 | - $after_widget = $args['after_widget']; |
|
2404 | - $after_widget = apply_filters( 'wp_super_duper_after_widget', $after_widget, $args, $instance, $this ); |
|
2405 | - $after_widget = apply_filters( 'wp_super_duper_after_widget_' . $this->base_id, $after_widget, $args, $instance, $this ); |
|
2406 | - |
|
2407 | - echo $before_widget; |
|
2408 | - // elementor strips the widget wrapping div so we check for and add it back if needed |
|
2409 | - if ( $this->is_elementor_widget_output() ) { |
|
2410 | - echo ! empty( $this->options['widget_ops']['classname'] ) ? "<span class='" . esc_attr( $class ) . "'>" : ''; |
|
2411 | - } |
|
2412 | - echo $this->output_title( $args, $instance ); |
|
2413 | - echo $output; |
|
2414 | - if ( $this->is_elementor_widget_output() ) { |
|
2415 | - echo ! empty( $this->options['widget_ops']['classname'] ) ? "</span>" : ''; |
|
2416 | - } |
|
2417 | - echo $after_widget; |
|
2418 | - } elseif ( $this->is_preview() && $output == '' ) {// if preview show a placeholder if empty |
|
2419 | - $output = $this->preview_placeholder_text( "{{" . $this->base_id . "}}" ); |
|
2420 | - echo $output; |
|
2421 | - } elseif ( $output && $no_wrap ) { |
|
2422 | - echo $output; |
|
2423 | - } |
|
2424 | - $output = ob_get_clean(); |
|
2425 | - |
|
2426 | - $output = apply_filters( 'wp_super_duper_widget_output', $output, $instance, $args, $this ); |
|
2427 | - |
|
2428 | - echo $output; |
|
2429 | - } |
|
2430 | - |
|
2431 | - /** |
|
2432 | - * Tests if the current output is inside a elementor container. |
|
2433 | - * |
|
2434 | - * @since 1.0.4 |
|
2435 | - * @return bool |
|
2436 | - */ |
|
2437 | - public function is_elementor_widget_output() { |
|
2438 | - $result = false; |
|
2439 | - if ( defined( 'ELEMENTOR_VERSION' ) && isset( $this->number ) && $this->number == 'REPLACE_TO_ID' ) { |
|
2440 | - $result = true; |
|
2441 | - } |
|
2442 | - |
|
2443 | - return $result; |
|
2444 | - } |
|
2445 | - |
|
2446 | - /** |
|
2447 | - * Tests if the current output is inside a elementor preview. |
|
2448 | - * |
|
2449 | - * @since 1.0.4 |
|
2450 | - * @return bool |
|
2451 | - */ |
|
2452 | - public function is_elementor_preview() { |
|
2453 | - $result = false; |
|
2454 | - if ( isset( $_REQUEST['elementor-preview'] ) || ( is_admin() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor' ) || ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor_ajax' ) ) { |
|
2455 | - $result = true; |
|
2456 | - } |
|
2457 | - |
|
2458 | - return $result; |
|
2459 | - } |
|
2460 | - |
|
2461 | - /** |
|
2462 | - * Tests if the current output is inside a Divi preview. |
|
2463 | - * |
|
2464 | - * @since 1.0.6 |
|
2465 | - * @return bool |
|
2466 | - */ |
|
2467 | - public function is_divi_preview() { |
|
2468 | - $result = false; |
|
2469 | - if ( isset( $_REQUEST['et_fb'] ) || isset( $_REQUEST['et_pb_preview'] ) || ( is_admin() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor' ) ) { |
|
2470 | - $result = true; |
|
2471 | - } |
|
2472 | - |
|
2473 | - return $result; |
|
2474 | - } |
|
2475 | - |
|
2476 | - /** |
|
2477 | - * Tests if the current output is inside a Beaver builder preview. |
|
2478 | - * |
|
2479 | - * @since 1.0.6 |
|
2480 | - * @return bool |
|
2481 | - */ |
|
2482 | - public function is_beaver_preview() { |
|
2483 | - $result = false; |
|
2484 | - if ( isset( $_REQUEST['fl_builder'] ) ) { |
|
2485 | - $result = true; |
|
2486 | - } |
|
2487 | - |
|
2488 | - return $result; |
|
2489 | - } |
|
2490 | - |
|
2491 | - /** |
|
2492 | - * Tests if the current output is inside a siteorigin builder preview. |
|
2493 | - * |
|
2494 | - * @since 1.0.6 |
|
2495 | - * @return bool |
|
2496 | - */ |
|
2497 | - public function is_siteorigin_preview() { |
|
2498 | - $result = false; |
|
2499 | - if ( ! empty( $_REQUEST['siteorigin_panels_live_editor'] ) ) { |
|
2500 | - $result = true; |
|
2501 | - } |
|
2502 | - |
|
2503 | - return $result; |
|
2504 | - } |
|
2505 | - |
|
2506 | - /** |
|
2507 | - * Tests if the current output is inside a cornerstone builder preview. |
|
2508 | - * |
|
2509 | - * @since 1.0.8 |
|
2510 | - * @return bool |
|
2511 | - */ |
|
2512 | - public function is_cornerstone_preview() { |
|
2513 | - $result = false; |
|
2514 | - if ( ! empty( $_REQUEST['cornerstone_preview'] ) || basename( $_SERVER['REQUEST_URI'] ) == 'cornerstone-endpoint' ) { |
|
2515 | - $result = true; |
|
2516 | - } |
|
2517 | - |
|
2518 | - return $result; |
|
2519 | - } |
|
2520 | - |
|
2521 | - /** |
|
2522 | - * Tests if the current output is inside a fusion builder preview. |
|
2523 | - * |
|
2524 | - * @since 1.1.0 |
|
2525 | - * @return bool |
|
2526 | - */ |
|
2527 | - public function is_fusion_preview() { |
|
2528 | - $result = false; |
|
2529 | - if ( ! empty( $_REQUEST['fb-edit'] ) || ! empty( $_REQUEST['fusion_load_nonce'] ) ) { |
|
2530 | - $result = true; |
|
2531 | - } |
|
2532 | - |
|
2533 | - return $result; |
|
2534 | - } |
|
2535 | - |
|
2536 | - /** |
|
2537 | - * Tests if the current output is inside a Oxygen builder preview. |
|
2538 | - * |
|
2539 | - * @since 1.0.18 |
|
2540 | - * @return bool |
|
2541 | - */ |
|
2542 | - public function is_oxygen_preview() { |
|
2543 | - $result = false; |
|
2544 | - if ( ! empty( $_REQUEST['ct_builder'] ) || ( ! empty( $_REQUEST['action'] ) && ( substr( $_REQUEST['action'], 0, 11 ) === "oxy_render_" || substr( $_REQUEST['action'], 0, 10 ) === "ct_render_" ) ) ) { |
|
2545 | - $result = true; |
|
2546 | - } |
|
2547 | - |
|
2548 | - return $result; |
|
2549 | - } |
|
2550 | - |
|
2551 | - /** |
|
2552 | - * General function to check if we are in a preview situation. |
|
2553 | - * |
|
2554 | - * @since 1.0.6 |
|
2555 | - * @return bool |
|
2556 | - */ |
|
2557 | - public function is_preview() { |
|
2558 | - $preview = false; |
|
2559 | - if ( $this->is_divi_preview() ) { |
|
2560 | - $preview = true; |
|
2561 | - } elseif ( $this->is_elementor_preview() ) { |
|
2562 | - $preview = true; |
|
2563 | - } elseif ( $this->is_beaver_preview() ) { |
|
2564 | - $preview = true; |
|
2565 | - } elseif ( $this->is_siteorigin_preview() ) { |
|
2566 | - $preview = true; |
|
2567 | - } elseif ( $this->is_cornerstone_preview() ) { |
|
2568 | - $preview = true; |
|
2569 | - } elseif ( $this->is_fusion_preview() ) { |
|
2570 | - $preview = true; |
|
2571 | - } elseif ( $this->is_oxygen_preview() ) { |
|
2572 | - $preview = true; |
|
2573 | - } elseif( $this->is_block_content_call() ) { |
|
2574 | - $preview = true; |
|
2575 | - } |
|
2576 | - |
|
2577 | - return $preview; |
|
2578 | - } |
|
2579 | - |
|
2580 | - /** |
|
2581 | - * Output the super title. |
|
2582 | - * |
|
2583 | - * @param $args |
|
2584 | - * @param array $instance |
|
2585 | - * |
|
2586 | - * @return string |
|
2587 | - */ |
|
2588 | - public function output_title( $args, $instance = array() ) { |
|
2589 | - $output = ''; |
|
2590 | - if ( ! empty( $instance['title'] ) ) { |
|
2591 | - /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ |
|
2592 | - $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ); |
|
2593 | - $output = $args['before_title'] . $title . $args['after_title']; |
|
2594 | - } |
|
2595 | - |
|
2596 | - return $output; |
|
2597 | - } |
|
2598 | - |
|
2599 | - /** |
|
2600 | - * Outputs the options form inputs for the widget. |
|
2601 | - * |
|
2602 | - * @param array $instance The widget options. |
|
2603 | - */ |
|
2604 | - public function form( $instance ) { |
|
2605 | - |
|
2606 | - // set widget instance |
|
2607 | - $this->instance = $instance; |
|
2608 | - |
|
2609 | - // set it as a SD widget |
|
2610 | - echo $this->widget_advanced_toggle(); |
|
2611 | - |
|
2612 | - echo "<p>" . esc_attr( $this->options['widget_ops']['description'] ) . "</p>"; |
|
2613 | - $arguments_raw = $this->get_arguments(); |
|
2614 | - |
|
2615 | - if ( is_array( $arguments_raw ) ) { |
|
2616 | - |
|
2617 | - $arguments = $this->group_arguments( $arguments_raw ); |
|
2618 | - |
|
2619 | - // Do we have sections? |
|
2620 | - $has_sections = $arguments == $arguments_raw ? false : true; |
|
2621 | - |
|
2622 | - |
|
2623 | - if ( $has_sections ) { |
|
2624 | - $panel_count = 0; |
|
2625 | - foreach ( $arguments as $key => $args ) { |
|
2626 | - |
|
2627 | - ?> |
|
2231 | + } |
|
2232 | + |
|
2233 | + /** |
|
2234 | + * Convert an array of attributes to block string. |
|
2235 | + * |
|
2236 | + * @todo there is prob a faster way to do this, also we could add some validation here. |
|
2237 | + * |
|
2238 | + * @param $custom_attributes |
|
2239 | + * |
|
2240 | + * @return string |
|
2241 | + */ |
|
2242 | + public function array_to_attributes( $custom_attributes, $html = false ) { |
|
2243 | + $attributes = ''; |
|
2244 | + if ( ! empty( $custom_attributes ) ) { |
|
2245 | + |
|
2246 | + if ( $html ) { |
|
2247 | + foreach ( $custom_attributes as $key => $val ) { |
|
2248 | + $attributes .= " $key='$val' "; |
|
2249 | + } |
|
2250 | + } else { |
|
2251 | + foreach ( $custom_attributes as $key => $val ) { |
|
2252 | + $attributes .= "'$key': '$val',"; |
|
2253 | + } |
|
2254 | + } |
|
2255 | + } |
|
2256 | + |
|
2257 | + return $attributes; |
|
2258 | + } |
|
2259 | + |
|
2260 | + /** |
|
2261 | + * A self looping function to create the output for JS block elements. |
|
2262 | + * |
|
2263 | + * This is what is output in the WP Editor visual view. |
|
2264 | + * |
|
2265 | + * @param $args |
|
2266 | + */ |
|
2267 | + public function block_element( $args ) { |
|
2268 | + |
|
2269 | + |
|
2270 | + if ( ! empty( $args ) ) { |
|
2271 | + foreach ( $args as $element => $new_args ) { |
|
2272 | + |
|
2273 | + if ( is_array( $new_args ) ) { // its an element |
|
2274 | + |
|
2275 | + |
|
2276 | + if ( isset( $new_args['element'] ) ) { |
|
2277 | + |
|
2278 | + if ( isset( $new_args['element_require'] ) ) { |
|
2279 | + echo str_replace( array( |
|
2280 | + "'+", |
|
2281 | + "+'" |
|
2282 | + ), '', $this->block_props_replace( $new_args['element_require'] ) ) . " && "; |
|
2283 | + unset( $new_args['element_require'] ); |
|
2284 | + } |
|
2285 | + |
|
2286 | + echo "\n el( '" . $new_args['element'] . "', {"; |
|
2287 | + |
|
2288 | + // get the attributes |
|
2289 | + foreach ( $new_args as $new_key => $new_value ) { |
|
2290 | + |
|
2291 | + |
|
2292 | + if ( $new_key == 'element' || $new_key == 'content' || $new_key == 'element_require' || $new_key == 'element_repeat' || is_array( $new_value ) ) { |
|
2293 | + // do nothing |
|
2294 | + } else { |
|
2295 | + echo $this->block_element( array( $new_key => $new_value ) ); |
|
2296 | + } |
|
2297 | + } |
|
2298 | + |
|
2299 | + echo "},";// end attributes |
|
2300 | + |
|
2301 | + // get the content |
|
2302 | + $first_item = 0; |
|
2303 | + foreach ( $new_args as $new_key => $new_value ) { |
|
2304 | + if ( $new_key === 'content' || is_array( $new_value ) ) { |
|
2305 | + |
|
2306 | + if ( $new_key === 'content' ) { |
|
2307 | + echo "'" . $this->block_props_replace( wp_slash( $new_value ) ) . "'"; |
|
2308 | + } |
|
2309 | + |
|
2310 | + if ( is_array( $new_value ) ) { |
|
2311 | + |
|
2312 | + if ( isset( $new_value['element_require'] ) ) { |
|
2313 | + echo str_replace( array( |
|
2314 | + "'+", |
|
2315 | + "+'" |
|
2316 | + ), '', $this->block_props_replace( $new_value['element_require'] ) ) . " && "; |
|
2317 | + unset( $new_value['element_require'] ); |
|
2318 | + } |
|
2319 | + |
|
2320 | + if ( isset( $new_value['element_repeat'] ) ) { |
|
2321 | + $x = 1; |
|
2322 | + while ( $x <= absint( $new_value['element_repeat'] ) ) { |
|
2323 | + $this->block_element( array( '' => $new_value ) ); |
|
2324 | + $x ++; |
|
2325 | + } |
|
2326 | + } else { |
|
2327 | + $this->block_element( array( '' => $new_value ) ); |
|
2328 | + } |
|
2329 | + } |
|
2330 | + $first_item ++; |
|
2331 | + } |
|
2332 | + } |
|
2333 | + |
|
2334 | + echo ")";// end content |
|
2335 | + |
|
2336 | + echo ", \n"; |
|
2337 | + |
|
2338 | + } |
|
2339 | + } else { |
|
2340 | + |
|
2341 | + if ( substr( $element, 0, 3 ) === "if_" ) { |
|
2342 | + echo str_replace( "if_", "", $element ) . ": " . $this->block_props_replace( $new_args, true ) . ","; |
|
2343 | + } elseif ( $element == 'style' ) { |
|
2344 | + echo $element . ": " . $this->block_props_replace( $new_args ) . ","; |
|
2345 | + } else { |
|
2346 | + echo $element . ": '" . $this->block_props_replace( $new_args ) . "',"; |
|
2347 | + } |
|
2348 | + |
|
2349 | + } |
|
2350 | + } |
|
2351 | + } |
|
2352 | + } |
|
2353 | + |
|
2354 | + /** |
|
2355 | + * Replace block attributes placeholders with the proper naming. |
|
2356 | + * |
|
2357 | + * @param $string |
|
2358 | + * |
|
2359 | + * @return mixed |
|
2360 | + */ |
|
2361 | + public function block_props_replace( $string, $no_wrap = false ) { |
|
2362 | + |
|
2363 | + if ( $no_wrap ) { |
|
2364 | + $string = str_replace( array( "[%", "%]" ), array( "props.attributes.", "" ), $string ); |
|
2365 | + } else { |
|
2366 | + $string = str_replace( array( "[%", "%]" ), array( "'+props.attributes.", "+'" ), $string ); |
|
2367 | + } |
|
2368 | + |
|
2369 | + return $string; |
|
2370 | + } |
|
2371 | + |
|
2372 | + /** |
|
2373 | + * Outputs the content of the widget |
|
2374 | + * |
|
2375 | + * @param array $args |
|
2376 | + * @param array $instance |
|
2377 | + */ |
|
2378 | + public function widget( $args, $instance ) { |
|
2379 | + |
|
2380 | + // get the filtered values |
|
2381 | + $argument_values = $this->argument_values( $instance ); |
|
2382 | + $argument_values = $this->string_to_bool( $argument_values ); |
|
2383 | + $output = $this->output( $argument_values, $args ); |
|
2384 | + |
|
2385 | + $no_wrap = false; |
|
2386 | + if ( isset( $argument_values['no_wrap'] ) && $argument_values['no_wrap'] ) { |
|
2387 | + $no_wrap = true; |
|
2388 | + } |
|
2389 | + |
|
2390 | + ob_start(); |
|
2391 | + if ( $output && ! $no_wrap ) { |
|
2392 | + |
|
2393 | + $class_original = $this->options['widget_ops']['classname']; |
|
2394 | + $class = $this->options['widget_ops']['classname']." sdel-".$this->get_instance_hash(); |
|
2395 | + |
|
2396 | + // Before widget |
|
2397 | + $before_widget = $args['before_widget']; |
|
2398 | + $before_widget = str_replace($class_original,$class,$before_widget); |
|
2399 | + $before_widget = apply_filters( 'wp_super_duper_before_widget', $before_widget, $args, $instance, $this ); |
|
2400 | + $before_widget = apply_filters( 'wp_super_duper_before_widget_' . $this->base_id, $before_widget, $args, $instance, $this ); |
|
2401 | + |
|
2402 | + // After widget |
|
2403 | + $after_widget = $args['after_widget']; |
|
2404 | + $after_widget = apply_filters( 'wp_super_duper_after_widget', $after_widget, $args, $instance, $this ); |
|
2405 | + $after_widget = apply_filters( 'wp_super_duper_after_widget_' . $this->base_id, $after_widget, $args, $instance, $this ); |
|
2406 | + |
|
2407 | + echo $before_widget; |
|
2408 | + // elementor strips the widget wrapping div so we check for and add it back if needed |
|
2409 | + if ( $this->is_elementor_widget_output() ) { |
|
2410 | + echo ! empty( $this->options['widget_ops']['classname'] ) ? "<span class='" . esc_attr( $class ) . "'>" : ''; |
|
2411 | + } |
|
2412 | + echo $this->output_title( $args, $instance ); |
|
2413 | + echo $output; |
|
2414 | + if ( $this->is_elementor_widget_output() ) { |
|
2415 | + echo ! empty( $this->options['widget_ops']['classname'] ) ? "</span>" : ''; |
|
2416 | + } |
|
2417 | + echo $after_widget; |
|
2418 | + } elseif ( $this->is_preview() && $output == '' ) {// if preview show a placeholder if empty |
|
2419 | + $output = $this->preview_placeholder_text( "{{" . $this->base_id . "}}" ); |
|
2420 | + echo $output; |
|
2421 | + } elseif ( $output && $no_wrap ) { |
|
2422 | + echo $output; |
|
2423 | + } |
|
2424 | + $output = ob_get_clean(); |
|
2425 | + |
|
2426 | + $output = apply_filters( 'wp_super_duper_widget_output', $output, $instance, $args, $this ); |
|
2427 | + |
|
2428 | + echo $output; |
|
2429 | + } |
|
2430 | + |
|
2431 | + /** |
|
2432 | + * Tests if the current output is inside a elementor container. |
|
2433 | + * |
|
2434 | + * @since 1.0.4 |
|
2435 | + * @return bool |
|
2436 | + */ |
|
2437 | + public function is_elementor_widget_output() { |
|
2438 | + $result = false; |
|
2439 | + if ( defined( 'ELEMENTOR_VERSION' ) && isset( $this->number ) && $this->number == 'REPLACE_TO_ID' ) { |
|
2440 | + $result = true; |
|
2441 | + } |
|
2442 | + |
|
2443 | + return $result; |
|
2444 | + } |
|
2445 | + |
|
2446 | + /** |
|
2447 | + * Tests if the current output is inside a elementor preview. |
|
2448 | + * |
|
2449 | + * @since 1.0.4 |
|
2450 | + * @return bool |
|
2451 | + */ |
|
2452 | + public function is_elementor_preview() { |
|
2453 | + $result = false; |
|
2454 | + if ( isset( $_REQUEST['elementor-preview'] ) || ( is_admin() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor' ) || ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor_ajax' ) ) { |
|
2455 | + $result = true; |
|
2456 | + } |
|
2457 | + |
|
2458 | + return $result; |
|
2459 | + } |
|
2460 | + |
|
2461 | + /** |
|
2462 | + * Tests if the current output is inside a Divi preview. |
|
2463 | + * |
|
2464 | + * @since 1.0.6 |
|
2465 | + * @return bool |
|
2466 | + */ |
|
2467 | + public function is_divi_preview() { |
|
2468 | + $result = false; |
|
2469 | + if ( isset( $_REQUEST['et_fb'] ) || isset( $_REQUEST['et_pb_preview'] ) || ( is_admin() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor' ) ) { |
|
2470 | + $result = true; |
|
2471 | + } |
|
2472 | + |
|
2473 | + return $result; |
|
2474 | + } |
|
2475 | + |
|
2476 | + /** |
|
2477 | + * Tests if the current output is inside a Beaver builder preview. |
|
2478 | + * |
|
2479 | + * @since 1.0.6 |
|
2480 | + * @return bool |
|
2481 | + */ |
|
2482 | + public function is_beaver_preview() { |
|
2483 | + $result = false; |
|
2484 | + if ( isset( $_REQUEST['fl_builder'] ) ) { |
|
2485 | + $result = true; |
|
2486 | + } |
|
2487 | + |
|
2488 | + return $result; |
|
2489 | + } |
|
2490 | + |
|
2491 | + /** |
|
2492 | + * Tests if the current output is inside a siteorigin builder preview. |
|
2493 | + * |
|
2494 | + * @since 1.0.6 |
|
2495 | + * @return bool |
|
2496 | + */ |
|
2497 | + public function is_siteorigin_preview() { |
|
2498 | + $result = false; |
|
2499 | + if ( ! empty( $_REQUEST['siteorigin_panels_live_editor'] ) ) { |
|
2500 | + $result = true; |
|
2501 | + } |
|
2502 | + |
|
2503 | + return $result; |
|
2504 | + } |
|
2505 | + |
|
2506 | + /** |
|
2507 | + * Tests if the current output is inside a cornerstone builder preview. |
|
2508 | + * |
|
2509 | + * @since 1.0.8 |
|
2510 | + * @return bool |
|
2511 | + */ |
|
2512 | + public function is_cornerstone_preview() { |
|
2513 | + $result = false; |
|
2514 | + if ( ! empty( $_REQUEST['cornerstone_preview'] ) || basename( $_SERVER['REQUEST_URI'] ) == 'cornerstone-endpoint' ) { |
|
2515 | + $result = true; |
|
2516 | + } |
|
2517 | + |
|
2518 | + return $result; |
|
2519 | + } |
|
2520 | + |
|
2521 | + /** |
|
2522 | + * Tests if the current output is inside a fusion builder preview. |
|
2523 | + * |
|
2524 | + * @since 1.1.0 |
|
2525 | + * @return bool |
|
2526 | + */ |
|
2527 | + public function is_fusion_preview() { |
|
2528 | + $result = false; |
|
2529 | + if ( ! empty( $_REQUEST['fb-edit'] ) || ! empty( $_REQUEST['fusion_load_nonce'] ) ) { |
|
2530 | + $result = true; |
|
2531 | + } |
|
2532 | + |
|
2533 | + return $result; |
|
2534 | + } |
|
2535 | + |
|
2536 | + /** |
|
2537 | + * Tests if the current output is inside a Oxygen builder preview. |
|
2538 | + * |
|
2539 | + * @since 1.0.18 |
|
2540 | + * @return bool |
|
2541 | + */ |
|
2542 | + public function is_oxygen_preview() { |
|
2543 | + $result = false; |
|
2544 | + if ( ! empty( $_REQUEST['ct_builder'] ) || ( ! empty( $_REQUEST['action'] ) && ( substr( $_REQUEST['action'], 0, 11 ) === "oxy_render_" || substr( $_REQUEST['action'], 0, 10 ) === "ct_render_" ) ) ) { |
|
2545 | + $result = true; |
|
2546 | + } |
|
2547 | + |
|
2548 | + return $result; |
|
2549 | + } |
|
2550 | + |
|
2551 | + /** |
|
2552 | + * General function to check if we are in a preview situation. |
|
2553 | + * |
|
2554 | + * @since 1.0.6 |
|
2555 | + * @return bool |
|
2556 | + */ |
|
2557 | + public function is_preview() { |
|
2558 | + $preview = false; |
|
2559 | + if ( $this->is_divi_preview() ) { |
|
2560 | + $preview = true; |
|
2561 | + } elseif ( $this->is_elementor_preview() ) { |
|
2562 | + $preview = true; |
|
2563 | + } elseif ( $this->is_beaver_preview() ) { |
|
2564 | + $preview = true; |
|
2565 | + } elseif ( $this->is_siteorigin_preview() ) { |
|
2566 | + $preview = true; |
|
2567 | + } elseif ( $this->is_cornerstone_preview() ) { |
|
2568 | + $preview = true; |
|
2569 | + } elseif ( $this->is_fusion_preview() ) { |
|
2570 | + $preview = true; |
|
2571 | + } elseif ( $this->is_oxygen_preview() ) { |
|
2572 | + $preview = true; |
|
2573 | + } elseif( $this->is_block_content_call() ) { |
|
2574 | + $preview = true; |
|
2575 | + } |
|
2576 | + |
|
2577 | + return $preview; |
|
2578 | + } |
|
2579 | + |
|
2580 | + /** |
|
2581 | + * Output the super title. |
|
2582 | + * |
|
2583 | + * @param $args |
|
2584 | + * @param array $instance |
|
2585 | + * |
|
2586 | + * @return string |
|
2587 | + */ |
|
2588 | + public function output_title( $args, $instance = array() ) { |
|
2589 | + $output = ''; |
|
2590 | + if ( ! empty( $instance['title'] ) ) { |
|
2591 | + /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ |
|
2592 | + $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ); |
|
2593 | + $output = $args['before_title'] . $title . $args['after_title']; |
|
2594 | + } |
|
2595 | + |
|
2596 | + return $output; |
|
2597 | + } |
|
2598 | + |
|
2599 | + /** |
|
2600 | + * Outputs the options form inputs for the widget. |
|
2601 | + * |
|
2602 | + * @param array $instance The widget options. |
|
2603 | + */ |
|
2604 | + public function form( $instance ) { |
|
2605 | + |
|
2606 | + // set widget instance |
|
2607 | + $this->instance = $instance; |
|
2608 | + |
|
2609 | + // set it as a SD widget |
|
2610 | + echo $this->widget_advanced_toggle(); |
|
2611 | + |
|
2612 | + echo "<p>" . esc_attr( $this->options['widget_ops']['description'] ) . "</p>"; |
|
2613 | + $arguments_raw = $this->get_arguments(); |
|
2614 | + |
|
2615 | + if ( is_array( $arguments_raw ) ) { |
|
2616 | + |
|
2617 | + $arguments = $this->group_arguments( $arguments_raw ); |
|
2618 | + |
|
2619 | + // Do we have sections? |
|
2620 | + $has_sections = $arguments == $arguments_raw ? false : true; |
|
2621 | + |
|
2622 | + |
|
2623 | + if ( $has_sections ) { |
|
2624 | + $panel_count = 0; |
|
2625 | + foreach ( $arguments as $key => $args ) { |
|
2626 | + |
|
2627 | + ?> |
|
2628 | 2628 | <script> |
2629 | 2629 | // jQuery(this).find("i").toggleClass("fas fa-chevron-up fas fa-chevron-down");jQuery(this).next().toggle(); |
2630 | 2630 | </script> |
2631 | 2631 | <?php |
2632 | 2632 | |
2633 | - $hide = $panel_count ? ' style="display:none;" ' : ''; |
|
2634 | - $icon_class = $panel_count ? 'fas fa-chevron-up' : 'fas fa-chevron-down'; |
|
2635 | - echo "<button onclick='jQuery(this).find(\"i\").toggleClass(\"fas fa-chevron-up fas fa-chevron-down\");jQuery(this).next().slideToggle();' type='button' class='sd-toggle-group-button sd-input-group-toggle" . sanitize_title_with_dashes( $key ) . "'>" . esc_attr( $key ) . " <i style='float:right;' class='" . $icon_class . "'></i></button>"; |
|
2636 | - echo "<div class='sd-toggle-group sd-input-group-" . sanitize_title_with_dashes( $key ) . "' $hide>"; |
|
2637 | - |
|
2638 | - foreach ( $args as $k => $a ) { |
|
2639 | - $this->widget_inputs( $a, $instance ); |
|
2640 | - } |
|
2641 | - |
|
2642 | - echo "</div>"; |
|
2643 | - |
|
2644 | - $panel_count ++; |
|
2645 | - |
|
2646 | - } |
|
2647 | - } else { |
|
2648 | - foreach ( $arguments as $key => $args ) { |
|
2649 | - $this->widget_inputs( $args, $instance ); |
|
2650 | - } |
|
2651 | - } |
|
2652 | - |
|
2653 | - } |
|
2654 | - } |
|
2655 | - |
|
2656 | - /** |
|
2657 | - * Get the hidden input that when added makes the advanced button show on widget settings. |
|
2658 | - * |
|
2659 | - * @return string |
|
2660 | - */ |
|
2661 | - public function widget_advanced_toggle() { |
|
2662 | - |
|
2663 | - $output = ''; |
|
2664 | - if ( $this->block_show_advanced() ) { |
|
2665 | - $val = 1; |
|
2666 | - } else { |
|
2667 | - $val = 0; |
|
2668 | - } |
|
2669 | - |
|
2670 | - $output .= "<input type='hidden' class='sd-show-advanced' value='$val' />"; |
|
2671 | - |
|
2672 | - return $output; |
|
2673 | - } |
|
2674 | - |
|
2675 | - /** |
|
2676 | - * Convert require element. |
|
2677 | - * |
|
2678 | - * @since 1.0.0 |
|
2679 | - * |
|
2680 | - * @param string $input Input element. |
|
2681 | - * |
|
2682 | - * @return string $output |
|
2683 | - */ |
|
2684 | - public function convert_element_require( $input ) { |
|
2685 | - |
|
2686 | - $input = str_replace( "'", '"', $input );// we only want double quotes |
|
2687 | - |
|
2688 | - $output = esc_attr( str_replace( array( "[%", "%]" ), array( |
|
2689 | - "jQuery(form).find('[data-argument=\"", |
|
2690 | - "\"]').find('input,select,textarea').val()" |
|
2691 | - ), $input ) ); |
|
2692 | - |
|
2693 | - return $output; |
|
2694 | - } |
|
2695 | - |
|
2696 | - /** |
|
2697 | - * Builds the inputs for the widget options. |
|
2698 | - * |
|
2699 | - * @param $args |
|
2700 | - * @param $instance |
|
2701 | - */ |
|
2702 | - public function widget_inputs( $args, $instance ) { |
|
2703 | - |
|
2704 | - $class = ""; |
|
2705 | - $element_require = ""; |
|
2706 | - $custom_attributes = ""; |
|
2707 | - |
|
2708 | - // get value |
|
2709 | - if ( isset( $instance[ $args['name'] ] ) ) { |
|
2710 | - $value = $instance[ $args['name'] ]; |
|
2711 | - } elseif ( ! isset( $instance[ $args['name'] ] ) && ! empty( $args['default'] ) ) { |
|
2712 | - $value = is_array( $args['default'] ) ? array_map( "esc_html", $args['default'] ) : esc_html( $args['default'] ); |
|
2713 | - } else { |
|
2714 | - $value = ''; |
|
2715 | - } |
|
2716 | - |
|
2717 | - // get placeholder |
|
2718 | - if ( ! empty( $args['placeholder'] ) ) { |
|
2719 | - $placeholder = "placeholder='" . esc_html( $args['placeholder'] ) . "'"; |
|
2720 | - } else { |
|
2721 | - $placeholder = ''; |
|
2722 | - } |
|
2723 | - |
|
2724 | - // get if advanced |
|
2725 | - if ( isset( $args['advanced'] ) && $args['advanced'] ) { |
|
2726 | - $class .= " sd-advanced-setting "; |
|
2727 | - } |
|
2728 | - |
|
2729 | - // element_require |
|
2730 | - if ( isset( $args['element_require'] ) && $args['element_require'] ) { |
|
2731 | - $element_require = $args['element_require']; |
|
2732 | - } |
|
2733 | - |
|
2734 | - // custom_attributes |
|
2735 | - if ( isset( $args['custom_attributes'] ) && $args['custom_attributes'] ) { |
|
2736 | - $custom_attributes = $this->array_to_attributes( $args['custom_attributes'], true ); |
|
2737 | - } |
|
2738 | - |
|
2739 | - // before wrapper |
|
2740 | - ?> |
|
2633 | + $hide = $panel_count ? ' style="display:none;" ' : ''; |
|
2634 | + $icon_class = $panel_count ? 'fas fa-chevron-up' : 'fas fa-chevron-down'; |
|
2635 | + echo "<button onclick='jQuery(this).find(\"i\").toggleClass(\"fas fa-chevron-up fas fa-chevron-down\");jQuery(this).next().slideToggle();' type='button' class='sd-toggle-group-button sd-input-group-toggle" . sanitize_title_with_dashes( $key ) . "'>" . esc_attr( $key ) . " <i style='float:right;' class='" . $icon_class . "'></i></button>"; |
|
2636 | + echo "<div class='sd-toggle-group sd-input-group-" . sanitize_title_with_dashes( $key ) . "' $hide>"; |
|
2637 | + |
|
2638 | + foreach ( $args as $k => $a ) { |
|
2639 | + $this->widget_inputs( $a, $instance ); |
|
2640 | + } |
|
2641 | + |
|
2642 | + echo "</div>"; |
|
2643 | + |
|
2644 | + $panel_count ++; |
|
2645 | + |
|
2646 | + } |
|
2647 | + } else { |
|
2648 | + foreach ( $arguments as $key => $args ) { |
|
2649 | + $this->widget_inputs( $args, $instance ); |
|
2650 | + } |
|
2651 | + } |
|
2652 | + |
|
2653 | + } |
|
2654 | + } |
|
2655 | + |
|
2656 | + /** |
|
2657 | + * Get the hidden input that when added makes the advanced button show on widget settings. |
|
2658 | + * |
|
2659 | + * @return string |
|
2660 | + */ |
|
2661 | + public function widget_advanced_toggle() { |
|
2662 | + |
|
2663 | + $output = ''; |
|
2664 | + if ( $this->block_show_advanced() ) { |
|
2665 | + $val = 1; |
|
2666 | + } else { |
|
2667 | + $val = 0; |
|
2668 | + } |
|
2669 | + |
|
2670 | + $output .= "<input type='hidden' class='sd-show-advanced' value='$val' />"; |
|
2671 | + |
|
2672 | + return $output; |
|
2673 | + } |
|
2674 | + |
|
2675 | + /** |
|
2676 | + * Convert require element. |
|
2677 | + * |
|
2678 | + * @since 1.0.0 |
|
2679 | + * |
|
2680 | + * @param string $input Input element. |
|
2681 | + * |
|
2682 | + * @return string $output |
|
2683 | + */ |
|
2684 | + public function convert_element_require( $input ) { |
|
2685 | + |
|
2686 | + $input = str_replace( "'", '"', $input );// we only want double quotes |
|
2687 | + |
|
2688 | + $output = esc_attr( str_replace( array( "[%", "%]" ), array( |
|
2689 | + "jQuery(form).find('[data-argument=\"", |
|
2690 | + "\"]').find('input,select,textarea').val()" |
|
2691 | + ), $input ) ); |
|
2692 | + |
|
2693 | + return $output; |
|
2694 | + } |
|
2695 | + |
|
2696 | + /** |
|
2697 | + * Builds the inputs for the widget options. |
|
2698 | + * |
|
2699 | + * @param $args |
|
2700 | + * @param $instance |
|
2701 | + */ |
|
2702 | + public function widget_inputs( $args, $instance ) { |
|
2703 | + |
|
2704 | + $class = ""; |
|
2705 | + $element_require = ""; |
|
2706 | + $custom_attributes = ""; |
|
2707 | + |
|
2708 | + // get value |
|
2709 | + if ( isset( $instance[ $args['name'] ] ) ) { |
|
2710 | + $value = $instance[ $args['name'] ]; |
|
2711 | + } elseif ( ! isset( $instance[ $args['name'] ] ) && ! empty( $args['default'] ) ) { |
|
2712 | + $value = is_array( $args['default'] ) ? array_map( "esc_html", $args['default'] ) : esc_html( $args['default'] ); |
|
2713 | + } else { |
|
2714 | + $value = ''; |
|
2715 | + } |
|
2716 | + |
|
2717 | + // get placeholder |
|
2718 | + if ( ! empty( $args['placeholder'] ) ) { |
|
2719 | + $placeholder = "placeholder='" . esc_html( $args['placeholder'] ) . "'"; |
|
2720 | + } else { |
|
2721 | + $placeholder = ''; |
|
2722 | + } |
|
2723 | + |
|
2724 | + // get if advanced |
|
2725 | + if ( isset( $args['advanced'] ) && $args['advanced'] ) { |
|
2726 | + $class .= " sd-advanced-setting "; |
|
2727 | + } |
|
2728 | + |
|
2729 | + // element_require |
|
2730 | + if ( isset( $args['element_require'] ) && $args['element_require'] ) { |
|
2731 | + $element_require = $args['element_require']; |
|
2732 | + } |
|
2733 | + |
|
2734 | + // custom_attributes |
|
2735 | + if ( isset( $args['custom_attributes'] ) && $args['custom_attributes'] ) { |
|
2736 | + $custom_attributes = $this->array_to_attributes( $args['custom_attributes'], true ); |
|
2737 | + } |
|
2738 | + |
|
2739 | + // before wrapper |
|
2740 | + ?> |
|
2741 | 2741 | <p class="sd-argument <?php echo esc_attr( $class ); ?>" |
2742 | 2742 | data-argument='<?php echo esc_attr( $args['name'] ); ?>' |
2743 | 2743 | data-element_require='<?php if ( $element_require ) { |
2744 | - echo $this->convert_element_require( $element_require ); |
|
2745 | - } ?>' |
|
2744 | + echo $this->convert_element_require( $element_require ); |
|
2745 | + } ?>' |
|
2746 | 2746 | > |
2747 | 2747 | <?php |
2748 | 2748 | |
2749 | - switch ( $args['type'] ) { |
|
2750 | - //array('text','password','number','email','tel','url','color') |
|
2751 | - case "text": |
|
2752 | - case "password": |
|
2753 | - case "number": |
|
2754 | - case "email": |
|
2755 | - case "tel": |
|
2756 | - case "url": |
|
2757 | - case "color": |
|
2758 | - ?> |
|
2749 | + switch ( $args['type'] ) { |
|
2750 | + //array('text','password','number','email','tel','url','color') |
|
2751 | + case "text": |
|
2752 | + case "password": |
|
2753 | + case "number": |
|
2754 | + case "email": |
|
2755 | + case "tel": |
|
2756 | + case "url": |
|
2757 | + case "color": |
|
2758 | + ?> |
|
2759 | 2759 | <label |
2760 | 2760 | for="<?php echo esc_attr( $this->get_field_id( $args['name'] ) ); ?>"><?php echo esc_attr( $args['title'] ); ?><?php echo $this->widget_field_desc( $args ); ?></label> |
2761 | 2761 | <input <?php echo $placeholder; ?> class="widefat" |
@@ -2766,47 +2766,47 @@ discard block |
||
2766 | 2766 | value="<?php echo esc_attr( $value ); ?>"> |
2767 | 2767 | <?php |
2768 | 2768 | |
2769 | - break; |
|
2770 | - case "select": |
|
2771 | - $multiple = isset( $args['multiple'] ) && $args['multiple'] ? true : false; |
|
2772 | - if ( $multiple ) { |
|
2773 | - if ( empty( $value ) ) { |
|
2774 | - $value = array(); |
|
2775 | - } |
|
2776 | - } |
|
2777 | - ?> |
|
2769 | + break; |
|
2770 | + case "select": |
|
2771 | + $multiple = isset( $args['multiple'] ) && $args['multiple'] ? true : false; |
|
2772 | + if ( $multiple ) { |
|
2773 | + if ( empty( $value ) ) { |
|
2774 | + $value = array(); |
|
2775 | + } |
|
2776 | + } |
|
2777 | + ?> |
|
2778 | 2778 | <label |
2779 | 2779 | for="<?php echo esc_attr( $this->get_field_id( $args['name'] ) ); ?>"><?php echo esc_attr( $args['title'] ); ?><?php echo $this->widget_field_desc( $args ); ?></label> |
2780 | 2780 | <select <?php echo $placeholder; ?> class="widefat" |
2781 | 2781 | <?php echo $custom_attributes; ?> |
2782 | 2782 | id="<?php echo esc_attr( $this->get_field_id( $args['name'] ) ); ?>" |
2783 | 2783 | name="<?php echo esc_attr( $this->get_field_name( $args['name'] ) ); |
2784 | - if ( $multiple ) { |
|
2785 | - echo "[]"; |
|
2786 | - } ?>" |
|
2784 | + if ( $multiple ) { |
|
2785 | + echo "[]"; |
|
2786 | + } ?>" |
|
2787 | 2787 | <?php if ( $multiple ) { |
2788 | - echo "multiple"; |
|
2789 | - } //@todo not implemented yet due to gutenberg not supporting it |
|
2790 | - ?> |
|
2788 | + echo "multiple"; |
|
2789 | + } //@todo not implemented yet due to gutenberg not supporting it |
|
2790 | + ?> |
|
2791 | 2791 | > |
2792 | 2792 | <?php |
2793 | 2793 | |
2794 | - if ( ! empty( $args['options'] ) ) { |
|
2795 | - foreach ( $args['options'] as $val => $label ) { |
|
2796 | - if ( $multiple ) { |
|
2797 | - $selected = in_array( $val, $value ) ? 'selected="selected"' : ''; |
|
2798 | - } else { |
|
2799 | - $selected = selected( $value, $val, false ); |
|
2800 | - } |
|
2801 | - echo "<option value='$val' " . $selected . ">$label</option>"; |
|
2802 | - } |
|
2803 | - } |
|
2804 | - ?> |
|
2794 | + if ( ! empty( $args['options'] ) ) { |
|
2795 | + foreach ( $args['options'] as $val => $label ) { |
|
2796 | + if ( $multiple ) { |
|
2797 | + $selected = in_array( $val, $value ) ? 'selected="selected"' : ''; |
|
2798 | + } else { |
|
2799 | + $selected = selected( $value, $val, false ); |
|
2800 | + } |
|
2801 | + echo "<option value='$val' " . $selected . ">$label</option>"; |
|
2802 | + } |
|
2803 | + } |
|
2804 | + ?> |
|
2805 | 2805 | </select> |
2806 | 2806 | <?php |
2807 | - break; |
|
2808 | - case "checkbox": |
|
2809 | - ?> |
|
2807 | + break; |
|
2808 | + case "checkbox": |
|
2809 | + ?> |
|
2810 | 2810 | <input <?php echo $placeholder; ?> |
2811 | 2811 | <?php checked( 1, $value, true ) ?> |
2812 | 2812 | <?php echo $custom_attributes; ?> |
@@ -2816,9 +2816,9 @@ discard block |
||
2816 | 2816 | <label |
2817 | 2817 | for="<?php echo esc_attr( $this->get_field_id( $args['name'] ) ); ?>"><?php echo esc_attr( $args['title'] ); ?><?php echo $this->widget_field_desc( $args ); ?></label> |
2818 | 2818 | <?php |
2819 | - break; |
|
2820 | - case "textarea": |
|
2821 | - ?> |
|
2819 | + break; |
|
2820 | + case "textarea": |
|
2821 | + ?> |
|
2822 | 2822 | <label |
2823 | 2823 | for="<?php echo esc_attr( $this->get_field_id( $args['name'] ) ); ?>"><?php echo esc_attr( $args['title'] ); ?><?php echo $this->widget_field_desc( $args ); ?></label> |
2824 | 2824 | <textarea <?php echo $placeholder; ?> class="widefat" |
@@ -2828,173 +2828,173 @@ discard block |
||
2828 | 2828 | ><?php echo esc_attr( $value ); ?></textarea> |
2829 | 2829 | <?php |
2830 | 2830 | |
2831 | - break; |
|
2832 | - case "hidden": |
|
2833 | - ?> |
|
2831 | + break; |
|
2832 | + case "hidden": |
|
2833 | + ?> |
|
2834 | 2834 | <input id="<?php echo esc_attr( $this->get_field_id( $args['name'] ) ); ?>" |
2835 | 2835 | name="<?php echo esc_attr( $this->get_field_name( $args['name'] ) ); ?>" type="hidden" |
2836 | 2836 | value="<?php echo esc_attr( $value ); ?>"> |
2837 | 2837 | <?php |
2838 | - break; |
|
2839 | - default: |
|
2840 | - echo "No input type found!"; // @todo we need to add more input types. |
|
2841 | - } |
|
2838 | + break; |
|
2839 | + default: |
|
2840 | + echo "No input type found!"; // @todo we need to add more input types. |
|
2841 | + } |
|
2842 | 2842 | |
2843 | - // after wrapper |
|
2844 | - ?> |
|
2843 | + // after wrapper |
|
2844 | + ?> |
|
2845 | 2845 | </p> |
2846 | 2846 | <?php |
2847 | 2847 | |
2848 | - } |
|
2849 | - |
|
2850 | - /** |
|
2851 | - * Get the widget input description html. |
|
2852 | - * |
|
2853 | - * @param $args |
|
2854 | - * |
|
2855 | - * @return string |
|
2856 | - * @todo, need to make its own tooltip script |
|
2857 | - */ |
|
2858 | - public function widget_field_desc( $args ) { |
|
2859 | - |
|
2860 | - $description = ''; |
|
2861 | - if ( isset( $args['desc'] ) && $args['desc'] ) { |
|
2862 | - if ( isset( $args['desc_tip'] ) && $args['desc_tip'] ) { |
|
2863 | - $description = $this->desc_tip( $args['desc'] ); |
|
2864 | - } else { |
|
2865 | - $description = '<span class="description">' . wp_kses_post( $args['desc'] ) . '</span>'; |
|
2866 | - } |
|
2867 | - } |
|
2868 | - |
|
2869 | - return $description; |
|
2870 | - } |
|
2871 | - |
|
2872 | - /** |
|
2873 | - * Get the tool tip html. |
|
2874 | - * |
|
2875 | - * @param $tip |
|
2876 | - * @param bool $allow_html |
|
2877 | - * |
|
2878 | - * @return string |
|
2879 | - */ |
|
2880 | - function desc_tip( $tip, $allow_html = false ) { |
|
2881 | - if ( $allow_html ) { |
|
2882 | - $tip = $this->sanitize_tooltip( $tip ); |
|
2883 | - } else { |
|
2884 | - $tip = esc_attr( $tip ); |
|
2885 | - } |
|
2886 | - |
|
2887 | - return '<span class="gd-help-tip dashicons dashicons-editor-help" title="' . $tip . '"></span>'; |
|
2888 | - } |
|
2889 | - |
|
2890 | - /** |
|
2891 | - * Sanitize a string destined to be a tooltip. |
|
2892 | - * |
|
2893 | - * @param string $var |
|
2894 | - * |
|
2895 | - * @return string |
|
2896 | - */ |
|
2897 | - public function sanitize_tooltip( $var ) { |
|
2898 | - return htmlspecialchars( wp_kses( html_entity_decode( $var ), array( |
|
2899 | - 'br' => array(), |
|
2900 | - 'em' => array(), |
|
2901 | - 'strong' => array(), |
|
2902 | - 'small' => array(), |
|
2903 | - 'span' => array(), |
|
2904 | - 'ul' => array(), |
|
2905 | - 'li' => array(), |
|
2906 | - 'ol' => array(), |
|
2907 | - 'p' => array(), |
|
2908 | - ) ) ); |
|
2909 | - } |
|
2910 | - |
|
2911 | - /** |
|
2912 | - * Processing widget options on save |
|
2913 | - * |
|
2914 | - * @param array $new_instance The new options |
|
2915 | - * @param array $old_instance The previous options |
|
2916 | - * |
|
2917 | - * @return array |
|
2918 | - * @todo we should add some sanitation here. |
|
2919 | - */ |
|
2920 | - public function update( $new_instance, $old_instance ) { |
|
2921 | - |
|
2922 | - //save the widget |
|
2923 | - $instance = array_merge( (array) $old_instance, (array) $new_instance ); |
|
2924 | - |
|
2925 | - // set widget instance |
|
2926 | - $this->instance = $instance; |
|
2927 | - |
|
2928 | - if ( empty( $this->arguments ) ) { |
|
2929 | - $this->get_arguments(); |
|
2930 | - } |
|
2931 | - |
|
2932 | - // check for checkboxes |
|
2933 | - if ( ! empty( $this->arguments ) ) { |
|
2934 | - foreach ( $this->arguments as $argument ) { |
|
2935 | - if ( isset( $argument['type'] ) && $argument['type'] == 'checkbox' && ! isset( $new_instance[ $argument['name'] ] ) ) { |
|
2936 | - $instance[ $argument['name'] ] = '0'; |
|
2937 | - } |
|
2938 | - } |
|
2939 | - } |
|
2940 | - |
|
2941 | - return $instance; |
|
2942 | - } |
|
2943 | - |
|
2944 | - /** |
|
2945 | - * Checks if the current call is a ajax call to get the block content. |
|
2946 | - * |
|
2947 | - * This can be used in your widget to return different content as the block content. |
|
2948 | - * |
|
2949 | - * @since 1.0.3 |
|
2950 | - * @return bool |
|
2951 | - */ |
|
2952 | - public function is_block_content_call() { |
|
2953 | - $result = false; |
|
2954 | - if ( wp_doing_ajax() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'super_duper_output_shortcode' ) { |
|
2955 | - $result = true; |
|
2956 | - } |
|
2957 | - |
|
2958 | - return $result; |
|
2959 | - } |
|
2960 | - |
|
2961 | - /** |
|
2962 | - * Get an instance hash that will be unique to the type and settings. |
|
2963 | - * |
|
2964 | - * @since 1.0.20 |
|
2965 | - * @return string |
|
2966 | - */ |
|
2967 | - public function get_instance_hash(){ |
|
2968 | - $instance_string = $this->base_id.serialize($this->instance); |
|
2969 | - return hash('crc32b',$instance_string); |
|
2970 | - } |
|
2971 | - |
|
2972 | - /** |
|
2973 | - * Generate and return inline styles from CSS rules that will match the unique class of the instance. |
|
2974 | - * |
|
2975 | - * @param array $rules |
|
2976 | - * |
|
2977 | - * @since 1.0.20 |
|
2978 | - * @return string |
|
2979 | - */ |
|
2980 | - public function get_instance_style($rules = array()){ |
|
2981 | - $css = ''; |
|
2982 | - |
|
2983 | - if(!empty($rules)){ |
|
2984 | - $rules = array_unique($rules); |
|
2985 | - $instance_hash = $this->get_instance_hash(); |
|
2986 | - $css .= "<style>"; |
|
2987 | - foreach($rules as $rule){ |
|
2988 | - $css .= ".sdel-$instance_hash $rule"; |
|
2989 | - } |
|
2990 | - $css .= "</style>"; |
|
2991 | - } |
|
2992 | - |
|
2993 | - |
|
2994 | - return $css; |
|
2995 | - |
|
2996 | - } |
|
2997 | - |
|
2998 | - } |
|
2848 | + } |
|
2849 | + |
|
2850 | + /** |
|
2851 | + * Get the widget input description html. |
|
2852 | + * |
|
2853 | + * @param $args |
|
2854 | + * |
|
2855 | + * @return string |
|
2856 | + * @todo, need to make its own tooltip script |
|
2857 | + */ |
|
2858 | + public function widget_field_desc( $args ) { |
|
2859 | + |
|
2860 | + $description = ''; |
|
2861 | + if ( isset( $args['desc'] ) && $args['desc'] ) { |
|
2862 | + if ( isset( $args['desc_tip'] ) && $args['desc_tip'] ) { |
|
2863 | + $description = $this->desc_tip( $args['desc'] ); |
|
2864 | + } else { |
|
2865 | + $description = '<span class="description">' . wp_kses_post( $args['desc'] ) . '</span>'; |
|
2866 | + } |
|
2867 | + } |
|
2868 | + |
|
2869 | + return $description; |
|
2870 | + } |
|
2871 | + |
|
2872 | + /** |
|
2873 | + * Get the tool tip html. |
|
2874 | + * |
|
2875 | + * @param $tip |
|
2876 | + * @param bool $allow_html |
|
2877 | + * |
|
2878 | + * @return string |
|
2879 | + */ |
|
2880 | + function desc_tip( $tip, $allow_html = false ) { |
|
2881 | + if ( $allow_html ) { |
|
2882 | + $tip = $this->sanitize_tooltip( $tip ); |
|
2883 | + } else { |
|
2884 | + $tip = esc_attr( $tip ); |
|
2885 | + } |
|
2886 | + |
|
2887 | + return '<span class="gd-help-tip dashicons dashicons-editor-help" title="' . $tip . '"></span>'; |
|
2888 | + } |
|
2889 | + |
|
2890 | + /** |
|
2891 | + * Sanitize a string destined to be a tooltip. |
|
2892 | + * |
|
2893 | + * @param string $var |
|
2894 | + * |
|
2895 | + * @return string |
|
2896 | + */ |
|
2897 | + public function sanitize_tooltip( $var ) { |
|
2898 | + return htmlspecialchars( wp_kses( html_entity_decode( $var ), array( |
|
2899 | + 'br' => array(), |
|
2900 | + 'em' => array(), |
|
2901 | + 'strong' => array(), |
|
2902 | + 'small' => array(), |
|
2903 | + 'span' => array(), |
|
2904 | + 'ul' => array(), |
|
2905 | + 'li' => array(), |
|
2906 | + 'ol' => array(), |
|
2907 | + 'p' => array(), |
|
2908 | + ) ) ); |
|
2909 | + } |
|
2910 | + |
|
2911 | + /** |
|
2912 | + * Processing widget options on save |
|
2913 | + * |
|
2914 | + * @param array $new_instance The new options |
|
2915 | + * @param array $old_instance The previous options |
|
2916 | + * |
|
2917 | + * @return array |
|
2918 | + * @todo we should add some sanitation here. |
|
2919 | + */ |
|
2920 | + public function update( $new_instance, $old_instance ) { |
|
2921 | + |
|
2922 | + //save the widget |
|
2923 | + $instance = array_merge( (array) $old_instance, (array) $new_instance ); |
|
2924 | + |
|
2925 | + // set widget instance |
|
2926 | + $this->instance = $instance; |
|
2927 | + |
|
2928 | + if ( empty( $this->arguments ) ) { |
|
2929 | + $this->get_arguments(); |
|
2930 | + } |
|
2931 | + |
|
2932 | + // check for checkboxes |
|
2933 | + if ( ! empty( $this->arguments ) ) { |
|
2934 | + foreach ( $this->arguments as $argument ) { |
|
2935 | + if ( isset( $argument['type'] ) && $argument['type'] == 'checkbox' && ! isset( $new_instance[ $argument['name'] ] ) ) { |
|
2936 | + $instance[ $argument['name'] ] = '0'; |
|
2937 | + } |
|
2938 | + } |
|
2939 | + } |
|
2940 | + |
|
2941 | + return $instance; |
|
2942 | + } |
|
2943 | + |
|
2944 | + /** |
|
2945 | + * Checks if the current call is a ajax call to get the block content. |
|
2946 | + * |
|
2947 | + * This can be used in your widget to return different content as the block content. |
|
2948 | + * |
|
2949 | + * @since 1.0.3 |
|
2950 | + * @return bool |
|
2951 | + */ |
|
2952 | + public function is_block_content_call() { |
|
2953 | + $result = false; |
|
2954 | + if ( wp_doing_ajax() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'super_duper_output_shortcode' ) { |
|
2955 | + $result = true; |
|
2956 | + } |
|
2957 | + |
|
2958 | + return $result; |
|
2959 | + } |
|
2960 | + |
|
2961 | + /** |
|
2962 | + * Get an instance hash that will be unique to the type and settings. |
|
2963 | + * |
|
2964 | + * @since 1.0.20 |
|
2965 | + * @return string |
|
2966 | + */ |
|
2967 | + public function get_instance_hash(){ |
|
2968 | + $instance_string = $this->base_id.serialize($this->instance); |
|
2969 | + return hash('crc32b',$instance_string); |
|
2970 | + } |
|
2971 | + |
|
2972 | + /** |
|
2973 | + * Generate and return inline styles from CSS rules that will match the unique class of the instance. |
|
2974 | + * |
|
2975 | + * @param array $rules |
|
2976 | + * |
|
2977 | + * @since 1.0.20 |
|
2978 | + * @return string |
|
2979 | + */ |
|
2980 | + public function get_instance_style($rules = array()){ |
|
2981 | + $css = ''; |
|
2982 | + |
|
2983 | + if(!empty($rules)){ |
|
2984 | + $rules = array_unique($rules); |
|
2985 | + $instance_hash = $this->get_instance_hash(); |
|
2986 | + $css .= "<style>"; |
|
2987 | + foreach($rules as $rule){ |
|
2988 | + $css .= ".sdel-$instance_hash $rule"; |
|
2989 | + } |
|
2990 | + $css .= "</style>"; |
|
2991 | + } |
|
2992 | + |
|
2993 | + |
|
2994 | + return $css; |
|
2995 | + |
|
2996 | + } |
|
2997 | + |
|
2998 | + } |
|
2999 | 2999 | |
3000 | 3000 | } |
@@ -13,7 +13,7 @@ discard block |
||
13 | 13 | * Bail if we are not in WP. |
14 | 14 | */ |
15 | 15 | if ( ! defined( 'ABSPATH' ) ) { |
16 | - exit; |
|
16 | + exit; |
|
17 | 17 | } |
18 | 18 | |
19 | 19 | /** |
@@ -21,294 +21,294 @@ discard block |
||
21 | 21 | */ |
22 | 22 | if ( ! class_exists( 'WP_Font_Awesome_Settings' ) ) { |
23 | 23 | |
24 | - /** |
|
25 | - * A Class to be able to change settings for Font Awesome. |
|
26 | - * |
|
27 | - * Class WP_Font_Awesome_Settings |
|
28 | - * @since 1.0.10 Now able to pass wp.org theme check. |
|
29 | - * @since 1.0.11 Font Awesome Pro now supported. |
|
30 | - * @since 1.0.11 Font Awesome Kits now supported. |
|
31 | - * @ver 1.0.11 |
|
32 | - * @todo decide how to implement textdomain |
|
33 | - */ |
|
34 | - class WP_Font_Awesome_Settings { |
|
35 | - |
|
36 | - /** |
|
37 | - * Class version version. |
|
38 | - * |
|
39 | - * @var string |
|
40 | - */ |
|
41 | - public $version = '1.0.12'; |
|
42 | - |
|
43 | - /** |
|
44 | - * Class textdomain. |
|
45 | - * |
|
46 | - * @var string |
|
47 | - */ |
|
48 | - public $textdomain = 'font-awesome-settings'; |
|
49 | - |
|
50 | - /** |
|
51 | - * Latest version of Font Awesome at time of publish published. |
|
52 | - * |
|
53 | - * @var string |
|
54 | - */ |
|
55 | - public $latest = "5.8.2"; |
|
56 | - |
|
57 | - /** |
|
58 | - * The title. |
|
59 | - * |
|
60 | - * @var string |
|
61 | - */ |
|
62 | - public $name = 'Font Awesome'; |
|
63 | - |
|
64 | - /** |
|
65 | - * Holds the settings values. |
|
66 | - * |
|
67 | - * @var array |
|
68 | - */ |
|
69 | - private $settings; |
|
70 | - |
|
71 | - /** |
|
72 | - * WP_Font_Awesome_Settings instance. |
|
73 | - * |
|
74 | - * @access private |
|
75 | - * @since 1.0.0 |
|
76 | - * @var WP_Font_Awesome_Settings There can be only one! |
|
77 | - */ |
|
78 | - private static $instance = null; |
|
79 | - |
|
80 | - /** |
|
81 | - * Main WP_Font_Awesome_Settings Instance. |
|
82 | - * |
|
83 | - * Ensures only one instance of WP_Font_Awesome_Settings is loaded or can be loaded. |
|
84 | - * |
|
85 | - * @since 1.0.0 |
|
86 | - * @static |
|
87 | - * @return WP_Font_Awesome_Settings - Main instance. |
|
88 | - */ |
|
89 | - public static function instance() { |
|
90 | - if ( ! isset( self::$instance ) && ! ( self::$instance instanceof WP_Font_Awesome_Settings ) ) { |
|
91 | - self::$instance = new WP_Font_Awesome_Settings; |
|
92 | - |
|
93 | - add_action( 'init', array( self::$instance, 'init' ) ); // set settings |
|
94 | - |
|
95 | - if ( is_admin() ) { |
|
96 | - add_action( 'admin_menu', array( self::$instance, 'menu_item' ) ); |
|
97 | - add_action( 'admin_init', array( self::$instance, 'register_settings' ) ); |
|
98 | - } |
|
99 | - |
|
100 | - do_action( 'wp_font_awesome_settings_loaded' ); |
|
101 | - } |
|
102 | - |
|
103 | - return self::$instance; |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Initiate the settings and add the required action hooks. |
|
108 | - * |
|
109 | - * @since 1.0.8 Settings name wrong - FIXED |
|
110 | - */ |
|
111 | - public function init() { |
|
112 | - $this->settings = $this->get_settings(); |
|
113 | - |
|
114 | - if ( $this->settings['type'] == 'CSS' ) { |
|
115 | - |
|
116 | - if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'frontend' ) { |
|
117 | - add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ), 5000 ); |
|
118 | - } |
|
119 | - |
|
120 | - if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'backend' ) { |
|
121 | - add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_style' ), 5000 ); |
|
122 | - } |
|
123 | - |
|
124 | - } else { |
|
125 | - |
|
126 | - if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'frontend' ) { |
|
127 | - add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 5000 ); |
|
128 | - } |
|
129 | - |
|
130 | - if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'backend' ) { |
|
131 | - add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 5000 ); |
|
132 | - } |
|
133 | - } |
|
134 | - |
|
135 | - // remove font awesome if set to do so |
|
136 | - if ( $this->settings['dequeue'] == '1' ) { |
|
137 | - add_action( 'clean_url', array( $this, 'remove_font_awesome' ), 5000, 3 ); |
|
138 | - } |
|
139 | - |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Adds the Font Awesome styles. |
|
144 | - */ |
|
145 | - public function enqueue_style() { |
|
146 | - // build url |
|
147 | - $url = $this->get_url(); |
|
148 | - |
|
149 | - wp_deregister_style( 'font-awesome' ); // deregister in case its already there |
|
150 | - wp_register_style( 'font-awesome', $url, array(), null ); |
|
151 | - wp_enqueue_style( 'font-awesome' ); |
|
152 | - |
|
153 | - if ( $this->settings['shims'] ) { |
|
154 | - $url = $this->get_url( true ); |
|
155 | - wp_deregister_style( 'font-awesome-shims' ); // deregister in case its already there |
|
156 | - wp_register_style( 'font-awesome-shims', $url, array(), null ); |
|
157 | - wp_enqueue_style( 'font-awesome-shims' ); |
|
158 | - } |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * Adds the Font Awesome JS. |
|
163 | - */ |
|
164 | - public function enqueue_scripts() { |
|
165 | - // build url |
|
166 | - $url = $this->get_url(); |
|
167 | - |
|
168 | - $deregister_function = 'wp' . '_' . 'deregister' . '_' . 'script'; |
|
169 | - call_user_func( $deregister_function, 'font-awesome' ); // deregister in case its already there |
|
170 | - wp_register_script( 'font-awesome', $url, array(), null ); |
|
171 | - wp_enqueue_script( 'font-awesome' ); |
|
172 | - |
|
173 | - if ( $this->settings['shims'] ) { |
|
174 | - $url = $this->get_url( true ); |
|
175 | - call_user_func( $deregister_function, 'font-awesome-shims' ); // deregister in case its already there |
|
176 | - wp_register_script( 'font-awesome-shims', $url, array(), null ); |
|
177 | - wp_enqueue_script( 'font-awesome-shims' ); |
|
178 | - } |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Get the url of the Font Awesome files. |
|
183 | - * |
|
184 | - * @param bool $shims If this is a shim file or not. |
|
185 | - * |
|
186 | - * @return string The url to the file. |
|
187 | - */ |
|
188 | - public function get_url( $shims = false ) { |
|
189 | - $script = $shims ? 'v4-shims' : 'all'; |
|
190 | - $sub = $this->settings['pro'] ? 'pro' : 'use'; |
|
191 | - $type = $this->settings['type']; |
|
192 | - $version = $this->settings['version']; |
|
193 | - $kit_url = $this->settings['kit-url'] ? esc_url( $this->settings['kit-url'] ) : ''; |
|
194 | - $url = ''; |
|
195 | - |
|
196 | - if ( $type == 'KIT' && $kit_url ) { |
|
197 | - if ( $shims ) { |
|
198 | - // if its a kit then we don't add shims here |
|
199 | - return ''; |
|
200 | - } |
|
201 | - $url .= $kit_url; // CDN |
|
202 | - $url .= "?wpfas=true"; // set our var so our version is not removed |
|
203 | - } else { |
|
204 | - $url .= "https://$sub.fontawesome.com/releases/"; // CDN |
|
205 | - $url .= ! empty( $version ) ? "v" . $version . '/' : "v" . $this->get_latest_version() . '/'; // version |
|
206 | - $url .= $type == 'CSS' ? 'css/' : 'js/'; // type |
|
207 | - $url .= $type == 'CSS' ? $script . '.css' : $script . '.js'; // type |
|
208 | - $url .= "?wpfas=true"; // set our var so our version is not removed |
|
209 | - } |
|
210 | - |
|
211 | - return $url; |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * Try and remove any other versions of Font Awesome added by other plugins/themes. |
|
216 | - * |
|
217 | - * Uses the clean_url filter to try and remove any other Font Awesome files added, it can also add pseudo-elements flag for the JS version. |
|
218 | - * |
|
219 | - * @param $url |
|
220 | - * @param $original_url |
|
221 | - * @param $_context |
|
222 | - * |
|
223 | - * @return string The filtered url. |
|
224 | - */ |
|
225 | - public function remove_font_awesome( $url, $original_url, $_context ) { |
|
226 | - |
|
227 | - if ( $_context == 'display' |
|
228 | - && ( strstr( $url, "fontawesome" ) !== false || strstr( $url, "font-awesome" ) !== false ) |
|
229 | - && ( strstr( $url, ".js" ) !== false || strstr( $url, ".css" ) !== false ) |
|
230 | - ) {// it's a font-awesome-url (probably) |
|
231 | - |
|
232 | - if ( strstr( $url, "wpfas=true" ) !== false ) { |
|
233 | - if ( $this->settings['type'] == 'JS' ) { |
|
234 | - if ( $this->settings['js-pseudo'] ) { |
|
235 | - $url .= "' data-search-pseudo-elements defer='defer"; |
|
236 | - } else { |
|
237 | - $url .= "' defer='defer"; |
|
238 | - } |
|
239 | - } |
|
240 | - } else { |
|
241 | - $url = ''; // removing the url removes the file |
|
242 | - } |
|
243 | - |
|
244 | - } |
|
245 | - |
|
246 | - return $url; |
|
247 | - } |
|
248 | - |
|
249 | - /** |
|
250 | - * Register the database settings with WordPress. |
|
251 | - */ |
|
252 | - public function register_settings() { |
|
253 | - register_setting( 'wp-font-awesome-settings', 'wp-font-awesome-settings' ); |
|
254 | - } |
|
255 | - |
|
256 | - /** |
|
257 | - * Add the WordPress settings menu item. |
|
258 | - * @since 1.0.10 Calling function name direct will fail theme check so we don't. |
|
259 | - */ |
|
260 | - public function menu_item() { |
|
261 | - $menu_function = 'add' . '_' . 'options' . '_' . 'page'; // won't pass theme check if function name present in theme |
|
262 | - call_user_func( $menu_function, $this->name, $this->name, 'manage_options', 'wp-font-awesome-settings', array( |
|
263 | - $this, |
|
264 | - 'settings_page' |
|
265 | - ) ); |
|
266 | - } |
|
267 | - |
|
268 | - /** |
|
269 | - * Get the current Font Awesome output settings. |
|
270 | - * |
|
271 | - * @return array The array of settings. |
|
272 | - */ |
|
273 | - public function get_settings() { |
|
274 | - |
|
275 | - $db_settings = get_option( 'wp-font-awesome-settings' ); |
|
276 | - |
|
277 | - $defaults = array( |
|
278 | - 'type' => 'CSS', // type to use, CSS or JS or KIT |
|
279 | - 'version' => '', // latest |
|
280 | - 'enqueue' => '', // front and backend |
|
281 | - 'shims' => '0', // default OFF now in 2020 |
|
282 | - 'js-pseudo' => '0', // if the pseudo elements flag should be set (CPU intensive) |
|
283 | - 'dequeue' => '0', // if we should try to remove other versions added by other plugins/themes |
|
284 | - 'pro' => '0', // if pro CDN url should be used |
|
285 | - 'kit-url' => '', // the kit url |
|
286 | - ); |
|
287 | - |
|
288 | - $settings = wp_parse_args( $db_settings, $defaults ); |
|
289 | - |
|
290 | - /** |
|
291 | - * Filter the Font Awesome settings. |
|
292 | - * |
|
293 | - * @todo if we add this filer people might use it and then it defeates the purpose of this class :/ |
|
294 | - */ |
|
295 | - return $this->settings = apply_filters( 'wp-font-awesome-settings', $settings, $db_settings, $defaults ); |
|
296 | - } |
|
297 | - |
|
298 | - |
|
299 | - /** |
|
300 | - * The settings page html output. |
|
301 | - */ |
|
302 | - public function settings_page() { |
|
303 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
304 | - wp_die( __( 'You do not have sufficient permissions to access this page.', 'font-awesome-settings' ) ); |
|
305 | - } |
|
306 | - |
|
307 | - // a hidden way to force the update of the verison number vai api instead of waiting the 48 hours |
|
308 | - if ( isset( $_REQUEST['force-version-check'] ) ) { |
|
309 | - $this->get_latest_version( $force_api = true ); |
|
310 | - } |
|
311 | - ?> |
|
24 | + /** |
|
25 | + * A Class to be able to change settings for Font Awesome. |
|
26 | + * |
|
27 | + * Class WP_Font_Awesome_Settings |
|
28 | + * @since 1.0.10 Now able to pass wp.org theme check. |
|
29 | + * @since 1.0.11 Font Awesome Pro now supported. |
|
30 | + * @since 1.0.11 Font Awesome Kits now supported. |
|
31 | + * @ver 1.0.11 |
|
32 | + * @todo decide how to implement textdomain |
|
33 | + */ |
|
34 | + class WP_Font_Awesome_Settings { |
|
35 | + |
|
36 | + /** |
|
37 | + * Class version version. |
|
38 | + * |
|
39 | + * @var string |
|
40 | + */ |
|
41 | + public $version = '1.0.12'; |
|
42 | + |
|
43 | + /** |
|
44 | + * Class textdomain. |
|
45 | + * |
|
46 | + * @var string |
|
47 | + */ |
|
48 | + public $textdomain = 'font-awesome-settings'; |
|
49 | + |
|
50 | + /** |
|
51 | + * Latest version of Font Awesome at time of publish published. |
|
52 | + * |
|
53 | + * @var string |
|
54 | + */ |
|
55 | + public $latest = "5.8.2"; |
|
56 | + |
|
57 | + /** |
|
58 | + * The title. |
|
59 | + * |
|
60 | + * @var string |
|
61 | + */ |
|
62 | + public $name = 'Font Awesome'; |
|
63 | + |
|
64 | + /** |
|
65 | + * Holds the settings values. |
|
66 | + * |
|
67 | + * @var array |
|
68 | + */ |
|
69 | + private $settings; |
|
70 | + |
|
71 | + /** |
|
72 | + * WP_Font_Awesome_Settings instance. |
|
73 | + * |
|
74 | + * @access private |
|
75 | + * @since 1.0.0 |
|
76 | + * @var WP_Font_Awesome_Settings There can be only one! |
|
77 | + */ |
|
78 | + private static $instance = null; |
|
79 | + |
|
80 | + /** |
|
81 | + * Main WP_Font_Awesome_Settings Instance. |
|
82 | + * |
|
83 | + * Ensures only one instance of WP_Font_Awesome_Settings is loaded or can be loaded. |
|
84 | + * |
|
85 | + * @since 1.0.0 |
|
86 | + * @static |
|
87 | + * @return WP_Font_Awesome_Settings - Main instance. |
|
88 | + */ |
|
89 | + public static function instance() { |
|
90 | + if ( ! isset( self::$instance ) && ! ( self::$instance instanceof WP_Font_Awesome_Settings ) ) { |
|
91 | + self::$instance = new WP_Font_Awesome_Settings; |
|
92 | + |
|
93 | + add_action( 'init', array( self::$instance, 'init' ) ); // set settings |
|
94 | + |
|
95 | + if ( is_admin() ) { |
|
96 | + add_action( 'admin_menu', array( self::$instance, 'menu_item' ) ); |
|
97 | + add_action( 'admin_init', array( self::$instance, 'register_settings' ) ); |
|
98 | + } |
|
99 | + |
|
100 | + do_action( 'wp_font_awesome_settings_loaded' ); |
|
101 | + } |
|
102 | + |
|
103 | + return self::$instance; |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Initiate the settings and add the required action hooks. |
|
108 | + * |
|
109 | + * @since 1.0.8 Settings name wrong - FIXED |
|
110 | + */ |
|
111 | + public function init() { |
|
112 | + $this->settings = $this->get_settings(); |
|
113 | + |
|
114 | + if ( $this->settings['type'] == 'CSS' ) { |
|
115 | + |
|
116 | + if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'frontend' ) { |
|
117 | + add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ), 5000 ); |
|
118 | + } |
|
119 | + |
|
120 | + if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'backend' ) { |
|
121 | + add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_style' ), 5000 ); |
|
122 | + } |
|
123 | + |
|
124 | + } else { |
|
125 | + |
|
126 | + if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'frontend' ) { |
|
127 | + add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 5000 ); |
|
128 | + } |
|
129 | + |
|
130 | + if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'backend' ) { |
|
131 | + add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 5000 ); |
|
132 | + } |
|
133 | + } |
|
134 | + |
|
135 | + // remove font awesome if set to do so |
|
136 | + if ( $this->settings['dequeue'] == '1' ) { |
|
137 | + add_action( 'clean_url', array( $this, 'remove_font_awesome' ), 5000, 3 ); |
|
138 | + } |
|
139 | + |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Adds the Font Awesome styles. |
|
144 | + */ |
|
145 | + public function enqueue_style() { |
|
146 | + // build url |
|
147 | + $url = $this->get_url(); |
|
148 | + |
|
149 | + wp_deregister_style( 'font-awesome' ); // deregister in case its already there |
|
150 | + wp_register_style( 'font-awesome', $url, array(), null ); |
|
151 | + wp_enqueue_style( 'font-awesome' ); |
|
152 | + |
|
153 | + if ( $this->settings['shims'] ) { |
|
154 | + $url = $this->get_url( true ); |
|
155 | + wp_deregister_style( 'font-awesome-shims' ); // deregister in case its already there |
|
156 | + wp_register_style( 'font-awesome-shims', $url, array(), null ); |
|
157 | + wp_enqueue_style( 'font-awesome-shims' ); |
|
158 | + } |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * Adds the Font Awesome JS. |
|
163 | + */ |
|
164 | + public function enqueue_scripts() { |
|
165 | + // build url |
|
166 | + $url = $this->get_url(); |
|
167 | + |
|
168 | + $deregister_function = 'wp' . '_' . 'deregister' . '_' . 'script'; |
|
169 | + call_user_func( $deregister_function, 'font-awesome' ); // deregister in case its already there |
|
170 | + wp_register_script( 'font-awesome', $url, array(), null ); |
|
171 | + wp_enqueue_script( 'font-awesome' ); |
|
172 | + |
|
173 | + if ( $this->settings['shims'] ) { |
|
174 | + $url = $this->get_url( true ); |
|
175 | + call_user_func( $deregister_function, 'font-awesome-shims' ); // deregister in case its already there |
|
176 | + wp_register_script( 'font-awesome-shims', $url, array(), null ); |
|
177 | + wp_enqueue_script( 'font-awesome-shims' ); |
|
178 | + } |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Get the url of the Font Awesome files. |
|
183 | + * |
|
184 | + * @param bool $shims If this is a shim file or not. |
|
185 | + * |
|
186 | + * @return string The url to the file. |
|
187 | + */ |
|
188 | + public function get_url( $shims = false ) { |
|
189 | + $script = $shims ? 'v4-shims' : 'all'; |
|
190 | + $sub = $this->settings['pro'] ? 'pro' : 'use'; |
|
191 | + $type = $this->settings['type']; |
|
192 | + $version = $this->settings['version']; |
|
193 | + $kit_url = $this->settings['kit-url'] ? esc_url( $this->settings['kit-url'] ) : ''; |
|
194 | + $url = ''; |
|
195 | + |
|
196 | + if ( $type == 'KIT' && $kit_url ) { |
|
197 | + if ( $shims ) { |
|
198 | + // if its a kit then we don't add shims here |
|
199 | + return ''; |
|
200 | + } |
|
201 | + $url .= $kit_url; // CDN |
|
202 | + $url .= "?wpfas=true"; // set our var so our version is not removed |
|
203 | + } else { |
|
204 | + $url .= "https://$sub.fontawesome.com/releases/"; // CDN |
|
205 | + $url .= ! empty( $version ) ? "v" . $version . '/' : "v" . $this->get_latest_version() . '/'; // version |
|
206 | + $url .= $type == 'CSS' ? 'css/' : 'js/'; // type |
|
207 | + $url .= $type == 'CSS' ? $script . '.css' : $script . '.js'; // type |
|
208 | + $url .= "?wpfas=true"; // set our var so our version is not removed |
|
209 | + } |
|
210 | + |
|
211 | + return $url; |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * Try and remove any other versions of Font Awesome added by other plugins/themes. |
|
216 | + * |
|
217 | + * Uses the clean_url filter to try and remove any other Font Awesome files added, it can also add pseudo-elements flag for the JS version. |
|
218 | + * |
|
219 | + * @param $url |
|
220 | + * @param $original_url |
|
221 | + * @param $_context |
|
222 | + * |
|
223 | + * @return string The filtered url. |
|
224 | + */ |
|
225 | + public function remove_font_awesome( $url, $original_url, $_context ) { |
|
226 | + |
|
227 | + if ( $_context == 'display' |
|
228 | + && ( strstr( $url, "fontawesome" ) !== false || strstr( $url, "font-awesome" ) !== false ) |
|
229 | + && ( strstr( $url, ".js" ) !== false || strstr( $url, ".css" ) !== false ) |
|
230 | + ) {// it's a font-awesome-url (probably) |
|
231 | + |
|
232 | + if ( strstr( $url, "wpfas=true" ) !== false ) { |
|
233 | + if ( $this->settings['type'] == 'JS' ) { |
|
234 | + if ( $this->settings['js-pseudo'] ) { |
|
235 | + $url .= "' data-search-pseudo-elements defer='defer"; |
|
236 | + } else { |
|
237 | + $url .= "' defer='defer"; |
|
238 | + } |
|
239 | + } |
|
240 | + } else { |
|
241 | + $url = ''; // removing the url removes the file |
|
242 | + } |
|
243 | + |
|
244 | + } |
|
245 | + |
|
246 | + return $url; |
|
247 | + } |
|
248 | + |
|
249 | + /** |
|
250 | + * Register the database settings with WordPress. |
|
251 | + */ |
|
252 | + public function register_settings() { |
|
253 | + register_setting( 'wp-font-awesome-settings', 'wp-font-awesome-settings' ); |
|
254 | + } |
|
255 | + |
|
256 | + /** |
|
257 | + * Add the WordPress settings menu item. |
|
258 | + * @since 1.0.10 Calling function name direct will fail theme check so we don't. |
|
259 | + */ |
|
260 | + public function menu_item() { |
|
261 | + $menu_function = 'add' . '_' . 'options' . '_' . 'page'; // won't pass theme check if function name present in theme |
|
262 | + call_user_func( $menu_function, $this->name, $this->name, 'manage_options', 'wp-font-awesome-settings', array( |
|
263 | + $this, |
|
264 | + 'settings_page' |
|
265 | + ) ); |
|
266 | + } |
|
267 | + |
|
268 | + /** |
|
269 | + * Get the current Font Awesome output settings. |
|
270 | + * |
|
271 | + * @return array The array of settings. |
|
272 | + */ |
|
273 | + public function get_settings() { |
|
274 | + |
|
275 | + $db_settings = get_option( 'wp-font-awesome-settings' ); |
|
276 | + |
|
277 | + $defaults = array( |
|
278 | + 'type' => 'CSS', // type to use, CSS or JS or KIT |
|
279 | + 'version' => '', // latest |
|
280 | + 'enqueue' => '', // front and backend |
|
281 | + 'shims' => '0', // default OFF now in 2020 |
|
282 | + 'js-pseudo' => '0', // if the pseudo elements flag should be set (CPU intensive) |
|
283 | + 'dequeue' => '0', // if we should try to remove other versions added by other plugins/themes |
|
284 | + 'pro' => '0', // if pro CDN url should be used |
|
285 | + 'kit-url' => '', // the kit url |
|
286 | + ); |
|
287 | + |
|
288 | + $settings = wp_parse_args( $db_settings, $defaults ); |
|
289 | + |
|
290 | + /** |
|
291 | + * Filter the Font Awesome settings. |
|
292 | + * |
|
293 | + * @todo if we add this filer people might use it and then it defeates the purpose of this class :/ |
|
294 | + */ |
|
295 | + return $this->settings = apply_filters( 'wp-font-awesome-settings', $settings, $db_settings, $defaults ); |
|
296 | + } |
|
297 | + |
|
298 | + |
|
299 | + /** |
|
300 | + * The settings page html output. |
|
301 | + */ |
|
302 | + public function settings_page() { |
|
303 | + if ( ! current_user_can( 'manage_options' ) ) { |
|
304 | + wp_die( __( 'You do not have sufficient permissions to access this page.', 'font-awesome-settings' ) ); |
|
305 | + } |
|
306 | + |
|
307 | + // a hidden way to force the update of the verison number vai api instead of waiting the 48 hours |
|
308 | + if ( isset( $_REQUEST['force-version-check'] ) ) { |
|
309 | + $this->get_latest_version( $force_api = true ); |
|
310 | + } |
|
311 | + ?> |
|
312 | 312 | <style> |
313 | 313 | .wpfas-kit-show { |
314 | 314 | display: none; |
@@ -326,10 +326,10 @@ discard block |
||
326 | 326 | <h1><?php echo $this->name; ?></h1> |
327 | 327 | <form method="post" action="options.php"> |
328 | 328 | <?php |
329 | - settings_fields( 'wp-font-awesome-settings' ); |
|
330 | - do_settings_sections( 'wp-font-awesome-settings' ); |
|
331 | - $kit_set = $this->settings['type'] == 'KIT' ? 'wpfas-kit-set' : ''; |
|
332 | - ?> |
|
329 | + settings_fields( 'wp-font-awesome-settings' ); |
|
330 | + do_settings_sections( 'wp-font-awesome-settings' ); |
|
331 | + $kit_set = $this->settings['type'] == 'KIT' ? 'wpfas-kit-set' : ''; |
|
332 | + ?> |
|
333 | 333 | <table class="form-table wpfas-table-settings <?php echo esc_attr( $kit_set ); ?>"> |
334 | 334 | <tr valign="top"> |
335 | 335 | <th scope="row"><label |
@@ -355,12 +355,12 @@ discard block |
||
355 | 355 | value="<?php echo esc_attr( $this->settings['kit-url'] ); ?>" |
356 | 356 | placeholder="<?php echo 'https://kit.font';echo 'awesome.com/123abc.js'; // this won't pass theme check :(?>"/> |
357 | 357 | <span><?php |
358 | - echo sprintf( |
|
359 | - __( 'Requires a free account with Font Awesome. %sGet kit url%s', 'font-awesome-settings' ), |
|
360 | - '<a rel="noopener noreferrer" target="_blank" href="https://fontawesome.com/kits"><i class="fas fa-external-link-alt"></i>', |
|
361 | - '</a>' |
|
362 | - ); |
|
363 | - ?></span> |
|
358 | + echo sprintf( |
|
359 | + __( 'Requires a free account with Font Awesome. %sGet kit url%s', 'font-awesome-settings' ), |
|
360 | + '<a rel="noopener noreferrer" target="_blank" href="https://fontawesome.com/kits"><i class="fas fa-external-link-alt"></i>', |
|
361 | + '</a>' |
|
362 | + ); |
|
363 | + ?></span> |
|
364 | 364 | </td> |
365 | 365 | </tr> |
366 | 366 | |
@@ -420,14 +420,14 @@ discard block |
||
420 | 420 | <input type="checkbox" name="wp-font-awesome-settings[pro]" |
421 | 421 | value="1" <?php checked( $this->settings['pro'], '1' ); ?> id="wpfas-pro"/> |
422 | 422 | <span><?php |
423 | - echo sprintf( |
|
424 | - __( 'Requires a subscription. %sLearn more%s %sManage my allowed domains%s', 'font-awesome-settings' ), |
|
425 | - '<a rel="noopener noreferrer" target="_blank" href="https://fontawesome.com/pro"><i class="fas fa-external-link-alt"></i>', |
|
426 | - '</a>', |
|
427 | - '<a rel="noopener noreferrer" target="_blank" href="https://fontawesome.com/account/cdn"><i class="fas fa-external-link-alt"></i>', |
|
428 | - '</a>' |
|
429 | - ); |
|
430 | - ?></span> |
|
423 | + echo sprintf( |
|
424 | + __( 'Requires a subscription. %sLearn more%s %sManage my allowed domains%s', 'font-awesome-settings' ), |
|
425 | + '<a rel="noopener noreferrer" target="_blank" href="https://fontawesome.com/pro"><i class="fas fa-external-link-alt"></i>', |
|
426 | + '</a>', |
|
427 | + '<a rel="noopener noreferrer" target="_blank" href="https://fontawesome.com/account/cdn"><i class="fas fa-external-link-alt"></i>', |
|
428 | + '</a>' |
|
429 | + ); |
|
430 | + ?></span> |
|
431 | 431 | </td> |
432 | 432 | </tr> |
433 | 433 | |
@@ -470,88 +470,88 @@ discard block |
||
470 | 470 | |
471 | 471 | </table> |
472 | 472 | <?php |
473 | - submit_button(); |
|
474 | - ?> |
|
473 | + submit_button(); |
|
474 | + ?> |
|
475 | 475 | </form> |
476 | 476 | |
477 | 477 | <div id="wpfas-version"><?php echo $this->version; ?></div> |
478 | 478 | </div> |
479 | 479 | |
480 | 480 | <?php |
481 | - } |
|
482 | - |
|
483 | - /** |
|
484 | - * Check a version number is valid and if so return it or else return an empty string. |
|
485 | - * |
|
486 | - * @param $version string The version number to check. |
|
487 | - * |
|
488 | - * @since 1.0.6 |
|
489 | - * |
|
490 | - * @return string Either a valid version number or an empty string. |
|
491 | - */ |
|
492 | - public function validate_version_number( $version ) { |
|
493 | - |
|
494 | - if ( version_compare( $version, '0.0.1', '>=' ) >= 0 ) { |
|
495 | - // valid |
|
496 | - } else { |
|
497 | - $version = '';// not validated |
|
498 | - } |
|
499 | - |
|
500 | - return $version; |
|
501 | - } |
|
502 | - |
|
503 | - |
|
504 | - /** |
|
505 | - * Get the latest version of Font Awesome. |
|
506 | - * |
|
507 | - * We check for a cached bersion and if none we will check for a live version via API and then cache it for 48 hours. |
|
508 | - * |
|
509 | - * @since 1.0.7 |
|
510 | - * @return mixed|string The latest version number found. |
|
511 | - */ |
|
512 | - public function get_latest_version( $force_api = false ) { |
|
513 | - $latest_version = $this->latest; |
|
514 | - |
|
515 | - $cache = get_transient( 'wp-font-awesome-settings-version' ); |
|
516 | - |
|
517 | - if ( $cache === false || $force_api ) { // its not set |
|
518 | - $api_ver = $this->get_latest_version_from_api(); |
|
519 | - if ( version_compare( $api_ver, $this->latest, '>=' ) >= 0 ) { |
|
520 | - $latest_version = $api_ver; |
|
521 | - set_transient( 'wp-font-awesome-settings-version', $api_ver, 48 * HOUR_IN_SECONDS ); |
|
522 | - } |
|
523 | - } elseif ( $this->validate_version_number( $cache ) ) { |
|
524 | - if ( version_compare( $cache, $this->latest, '>=' ) >= 0 ) { |
|
525 | - $latest_version = $cache; |
|
526 | - } |
|
527 | - } |
|
528 | - |
|
529 | - return $latest_version; |
|
530 | - } |
|
531 | - |
|
532 | - /** |
|
533 | - * Get the latest Font Awesome version from the github API. |
|
534 | - * |
|
535 | - * @since 1.0.7 |
|
536 | - * @return string The latest version number or `0` on API fail. |
|
537 | - */ |
|
538 | - public function get_latest_version_from_api() { |
|
539 | - $version = "0"; |
|
540 | - $response = wp_remote_get( "https://api.github.com/repos/FortAwesome/Font-Awesome/releases/latest" ); |
|
541 | - if ( ! is_wp_error( $response ) && is_array( $response ) ) { |
|
542 | - $api_response = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
543 | - if ( isset( $api_response['tag_name'] ) && version_compare( $api_response['tag_name'], $this->latest, '>=' ) >= 0 && empty( $api_response['prerelease'] ) ) { |
|
544 | - $version = $api_response['tag_name']; |
|
545 | - } |
|
546 | - } |
|
547 | - |
|
548 | - return $version; |
|
549 | - } |
|
550 | - |
|
551 | - } |
|
552 | - |
|
553 | - /** |
|
554 | - * Run the class if found. |
|
555 | - */ |
|
556 | - WP_Font_Awesome_Settings::instance(); |
|
481 | + } |
|
482 | + |
|
483 | + /** |
|
484 | + * Check a version number is valid and if so return it or else return an empty string. |
|
485 | + * |
|
486 | + * @param $version string The version number to check. |
|
487 | + * |
|
488 | + * @since 1.0.6 |
|
489 | + * |
|
490 | + * @return string Either a valid version number or an empty string. |
|
491 | + */ |
|
492 | + public function validate_version_number( $version ) { |
|
493 | + |
|
494 | + if ( version_compare( $version, '0.0.1', '>=' ) >= 0 ) { |
|
495 | + // valid |
|
496 | + } else { |
|
497 | + $version = '';// not validated |
|
498 | + } |
|
499 | + |
|
500 | + return $version; |
|
501 | + } |
|
502 | + |
|
503 | + |
|
504 | + /** |
|
505 | + * Get the latest version of Font Awesome. |
|
506 | + * |
|
507 | + * We check for a cached bersion and if none we will check for a live version via API and then cache it for 48 hours. |
|
508 | + * |
|
509 | + * @since 1.0.7 |
|
510 | + * @return mixed|string The latest version number found. |
|
511 | + */ |
|
512 | + public function get_latest_version( $force_api = false ) { |
|
513 | + $latest_version = $this->latest; |
|
514 | + |
|
515 | + $cache = get_transient( 'wp-font-awesome-settings-version' ); |
|
516 | + |
|
517 | + if ( $cache === false || $force_api ) { // its not set |
|
518 | + $api_ver = $this->get_latest_version_from_api(); |
|
519 | + if ( version_compare( $api_ver, $this->latest, '>=' ) >= 0 ) { |
|
520 | + $latest_version = $api_ver; |
|
521 | + set_transient( 'wp-font-awesome-settings-version', $api_ver, 48 * HOUR_IN_SECONDS ); |
|
522 | + } |
|
523 | + } elseif ( $this->validate_version_number( $cache ) ) { |
|
524 | + if ( version_compare( $cache, $this->latest, '>=' ) >= 0 ) { |
|
525 | + $latest_version = $cache; |
|
526 | + } |
|
527 | + } |
|
528 | + |
|
529 | + return $latest_version; |
|
530 | + } |
|
531 | + |
|
532 | + /** |
|
533 | + * Get the latest Font Awesome version from the github API. |
|
534 | + * |
|
535 | + * @since 1.0.7 |
|
536 | + * @return string The latest version number or `0` on API fail. |
|
537 | + */ |
|
538 | + public function get_latest_version_from_api() { |
|
539 | + $version = "0"; |
|
540 | + $response = wp_remote_get( "https://api.github.com/repos/FortAwesome/Font-Awesome/releases/latest" ); |
|
541 | + if ( ! is_wp_error( $response ) && is_array( $response ) ) { |
|
542 | + $api_response = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
543 | + if ( isset( $api_response['tag_name'] ) && version_compare( $api_response['tag_name'], $this->latest, '>=' ) >= 0 && empty( $api_response['prerelease'] ) ) { |
|
544 | + $version = $api_response['tag_name']; |
|
545 | + } |
|
546 | + } |
|
547 | + |
|
548 | + return $version; |
|
549 | + } |
|
550 | + |
|
551 | + } |
|
552 | + |
|
553 | + /** |
|
554 | + * Run the class if found. |
|
555 | + */ |
|
556 | + WP_Font_Awesome_Settings::instance(); |
|
557 | 557 | } |
558 | 558 | \ No newline at end of file |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if ( ! defined( 'ABSPATH' ) ) { |
4 | - exit; // Exit if accessed directly |
|
4 | + exit; // Exit if accessed directly |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -11,77 +11,77 @@ discard block |
||
11 | 11 | */ |
12 | 12 | class AUI_Component_Pagination { |
13 | 13 | |
14 | - /** |
|
15 | - * Build the component. |
|
16 | - * |
|
17 | - * @param array $args |
|
18 | - * |
|
19 | - * @return string The rendered component. |
|
20 | - */ |
|
21 | - public static function get( $args = array() ) { |
|
22 | - global $wp_query; |
|
23 | - |
|
24 | - $defaults = array( |
|
25 | - 'class' => '', |
|
26 | - 'mid_size' => 2, |
|
27 | - 'prev_text' => '<i class="fas fa-chevron-left"></i>', |
|
28 | - 'next_text' => '<i class="fas fa-chevron-right"></i>', |
|
29 | - 'screen_reader_text' => __( 'Posts navigation','aui' ), |
|
30 | - 'before_paging' => '', |
|
31 | - 'after_paging' => '', |
|
32 | - 'type' => 'array', |
|
33 | - 'total' => isset( $wp_query->max_num_pages ) ? $wp_query->max_num_pages : 1, |
|
34 | - 'links' => array() // an array of links if using custom links, this includes the a tag. |
|
35 | - ); |
|
36 | - |
|
37 | - /** |
|
38 | - * Parse incoming $args into an array and merge it with $defaults |
|
39 | - */ |
|
40 | - $args = wp_parse_args( $args, $defaults ); |
|
41 | - |
|
42 | - $output = ''; |
|
43 | - |
|
44 | - // Don't print empty markup if there's only one page. |
|
45 | - if ( $args['total'] > 1 ) { |
|
46 | - |
|
47 | - // Set up paginated links. |
|
48 | - $links = !empty( $args['links'] ) ? $args['links'] : paginate_links( $args ); |
|
49 | - |
|
50 | - $class = !empty($args['class']) ? $args['class'] : ''; |
|
51 | - |
|
52 | - // make the output bootstrap ready |
|
53 | - $links_html = "<ul class='pagination m-0 p-0 $class'>"; |
|
54 | - if ( ! empty( $links ) ) { |
|
55 | - foreach ( $links as $link ) { |
|
56 | - $active = strpos( $link, 'current' ) !== false ? 'active' : ''; |
|
57 | - $links_html .= "<li class='page-item $active'>"; |
|
58 | - $links_html .= str_replace( "page-numbers", "page-link", $link ); |
|
59 | - $links_html .= "</li>"; |
|
60 | - } |
|
61 | - } |
|
62 | - $links_html .= "</ul>"; |
|
63 | - |
|
64 | - if ( $links ) { |
|
65 | - $output .= '<section class="px-0 py-2 w-100">'; |
|
66 | - $output .= _navigation_markup( $links_html, 'aui-pagination', $args['screen_reader_text'] ); |
|
67 | - $output .= '</section>'; |
|
68 | - } |
|
69 | - |
|
70 | - $output = str_replace( "screen-reader-text", "screen-reader-text sr-only", $output ); |
|
71 | - $output = str_replace( "nav-links", "aui-nav-links", $output ); |
|
72 | - } |
|
73 | - |
|
74 | - if ( $output ) { |
|
75 | - if ( ! empty( $args['before_paging'] ) ) { |
|
76 | - $output = $args['before_paging'] . $output; |
|
77 | - } |
|
78 | - |
|
79 | - if ( ! empty( $args['after_paging'] ) ) { |
|
80 | - $output = $output . $args['after_paging']; |
|
81 | - } |
|
82 | - } |
|
83 | - |
|
84 | - return $output; |
|
85 | - } |
|
14 | + /** |
|
15 | + * Build the component. |
|
16 | + * |
|
17 | + * @param array $args |
|
18 | + * |
|
19 | + * @return string The rendered component. |
|
20 | + */ |
|
21 | + public static function get( $args = array() ) { |
|
22 | + global $wp_query; |
|
23 | + |
|
24 | + $defaults = array( |
|
25 | + 'class' => '', |
|
26 | + 'mid_size' => 2, |
|
27 | + 'prev_text' => '<i class="fas fa-chevron-left"></i>', |
|
28 | + 'next_text' => '<i class="fas fa-chevron-right"></i>', |
|
29 | + 'screen_reader_text' => __( 'Posts navigation','aui' ), |
|
30 | + 'before_paging' => '', |
|
31 | + 'after_paging' => '', |
|
32 | + 'type' => 'array', |
|
33 | + 'total' => isset( $wp_query->max_num_pages ) ? $wp_query->max_num_pages : 1, |
|
34 | + 'links' => array() // an array of links if using custom links, this includes the a tag. |
|
35 | + ); |
|
36 | + |
|
37 | + /** |
|
38 | + * Parse incoming $args into an array and merge it with $defaults |
|
39 | + */ |
|
40 | + $args = wp_parse_args( $args, $defaults ); |
|
41 | + |
|
42 | + $output = ''; |
|
43 | + |
|
44 | + // Don't print empty markup if there's only one page. |
|
45 | + if ( $args['total'] > 1 ) { |
|
46 | + |
|
47 | + // Set up paginated links. |
|
48 | + $links = !empty( $args['links'] ) ? $args['links'] : paginate_links( $args ); |
|
49 | + |
|
50 | + $class = !empty($args['class']) ? $args['class'] : ''; |
|
51 | + |
|
52 | + // make the output bootstrap ready |
|
53 | + $links_html = "<ul class='pagination m-0 p-0 $class'>"; |
|
54 | + if ( ! empty( $links ) ) { |
|
55 | + foreach ( $links as $link ) { |
|
56 | + $active = strpos( $link, 'current' ) !== false ? 'active' : ''; |
|
57 | + $links_html .= "<li class='page-item $active'>"; |
|
58 | + $links_html .= str_replace( "page-numbers", "page-link", $link ); |
|
59 | + $links_html .= "</li>"; |
|
60 | + } |
|
61 | + } |
|
62 | + $links_html .= "</ul>"; |
|
63 | + |
|
64 | + if ( $links ) { |
|
65 | + $output .= '<section class="px-0 py-2 w-100">'; |
|
66 | + $output .= _navigation_markup( $links_html, 'aui-pagination', $args['screen_reader_text'] ); |
|
67 | + $output .= '</section>'; |
|
68 | + } |
|
69 | + |
|
70 | + $output = str_replace( "screen-reader-text", "screen-reader-text sr-only", $output ); |
|
71 | + $output = str_replace( "nav-links", "aui-nav-links", $output ); |
|
72 | + } |
|
73 | + |
|
74 | + if ( $output ) { |
|
75 | + if ( ! empty( $args['before_paging'] ) ) { |
|
76 | + $output = $args['before_paging'] . $output; |
|
77 | + } |
|
78 | + |
|
79 | + if ( ! empty( $args['after_paging'] ) ) { |
|
80 | + $output = $output . $args['after_paging']; |
|
81 | + } |
|
82 | + } |
|
83 | + |
|
84 | + return $output; |
|
85 | + } |
|
86 | 86 | |
87 | 87 | } |
88 | 88 | \ No newline at end of file |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | if ( ! defined( 'ABSPATH' ) ) { |
3 | - exit; |
|
3 | + exit; |
|
4 | 4 | } |
5 | 5 | |
6 | 6 | /** |
@@ -10,60 +10,60 @@ discard block |
||
10 | 10 | class GetPaid_Form_Item extends WPInv_Item { |
11 | 11 | |
12 | 12 | /** |
13 | - * Stores a custom description for the item. |
|
14 | - * |
|
15 | - * @var string |
|
16 | - */ |
|
17 | - protected $custom_description = null; |
|
18 | - |
|
19 | - /** |
|
20 | - * Stores the item quantity. |
|
21 | - * |
|
22 | - * @var int |
|
23 | - */ |
|
24 | - protected $quantity = 1; |
|
25 | - |
|
26 | - /** |
|
27 | - * Stores the item meta. |
|
28 | - * |
|
29 | - * @var array |
|
30 | - */ |
|
31 | - protected $meta = array(); |
|
32 | - |
|
33 | - /** |
|
34 | - * Is this item required? |
|
35 | - * |
|
36 | - * @var int |
|
37 | - */ |
|
38 | - protected $is_required = true; |
|
39 | - |
|
40 | - /** |
|
41 | - * Are quantities allowed? |
|
42 | - * |
|
43 | - * @var int |
|
44 | - */ |
|
45 | - protected $allow_quantities = false; |
|
46 | - |
|
47 | - /** |
|
48 | - * Associated invoice. |
|
49 | - * |
|
50 | - * @var int |
|
51 | - */ |
|
52 | - public $invoice_id = 0; |
|
53 | - |
|
54 | - /** |
|
55 | - * Item discount. |
|
56 | - * |
|
57 | - * @var float |
|
58 | - */ |
|
59 | - public $item_discount = 0; |
|
60 | - |
|
61 | - /** |
|
62 | - * Item tax. |
|
63 | - * |
|
64 | - * @var float |
|
65 | - */ |
|
66 | - public $item_tax = 0; |
|
13 | + * Stores a custom description for the item. |
|
14 | + * |
|
15 | + * @var string |
|
16 | + */ |
|
17 | + protected $custom_description = null; |
|
18 | + |
|
19 | + /** |
|
20 | + * Stores the item quantity. |
|
21 | + * |
|
22 | + * @var int |
|
23 | + */ |
|
24 | + protected $quantity = 1; |
|
25 | + |
|
26 | + /** |
|
27 | + * Stores the item meta. |
|
28 | + * |
|
29 | + * @var array |
|
30 | + */ |
|
31 | + protected $meta = array(); |
|
32 | + |
|
33 | + /** |
|
34 | + * Is this item required? |
|
35 | + * |
|
36 | + * @var int |
|
37 | + */ |
|
38 | + protected $is_required = true; |
|
39 | + |
|
40 | + /** |
|
41 | + * Are quantities allowed? |
|
42 | + * |
|
43 | + * @var int |
|
44 | + */ |
|
45 | + protected $allow_quantities = false; |
|
46 | + |
|
47 | + /** |
|
48 | + * Associated invoice. |
|
49 | + * |
|
50 | + * @var int |
|
51 | + */ |
|
52 | + public $invoice_id = 0; |
|
53 | + |
|
54 | + /** |
|
55 | + * Item discount. |
|
56 | + * |
|
57 | + * @var float |
|
58 | + */ |
|
59 | + public $item_discount = 0; |
|
60 | + |
|
61 | + /** |
|
62 | + * Item tax. |
|
63 | + * |
|
64 | + * @var float |
|
65 | + */ |
|
66 | + public $item_tax = 0; |
|
67 | 67 | |
68 | 68 | /* |
69 | 69 | |-------------------------------------------------------------------------- |
@@ -81,232 +81,232 @@ discard block |
||
81 | 81 | */ |
82 | 82 | |
83 | 83 | /** |
84 | - * Get the item name. |
|
85 | - * |
|
86 | - * @since 1.0.19 |
|
87 | - * @param string $context View or edit context. |
|
88 | - * @return string |
|
89 | - */ |
|
90 | - public function get_name( $context = 'view' ) { |
|
91 | - $name = parent::get_name( $context ); |
|
92 | - return $name . wpinv_get_item_suffix( $this ); |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * Get the item name without a suffix. |
|
97 | - * |
|
98 | - * @since 1.0.19 |
|
99 | - * @param string $context View or edit context. |
|
100 | - * @return string |
|
101 | - */ |
|
102 | - public function get_raw_name( $context = 'view' ) { |
|
103 | - return parent::get_name( $context ); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Get the item description. |
|
108 | - * |
|
109 | - * @since 1.0.19 |
|
110 | - * @param string $context View or edit context. |
|
111 | - * @return string |
|
112 | - */ |
|
113 | - public function get_description( $context = 'view' ) { |
|
114 | - |
|
115 | - if ( isset( $this->custom_description ) ) { |
|
116 | - return $this->custom_description; |
|
117 | - } |
|
118 | - |
|
119 | - return parent::get_description( $context ); |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Returns the sub total. |
|
124 | - * |
|
125 | - * @since 1.0.19 |
|
126 | - * @param string $context View or edit context. |
|
127 | - * @return float |
|
128 | - */ |
|
129 | - public function get_sub_total( $context = 'view' ) { |
|
130 | - return $this->get_quantity( $context ) * $this->get_initial_price( $context ); |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Returns the recurring sub total. |
|
135 | - * |
|
136 | - * @since 1.0.19 |
|
137 | - * @param string $context View or edit context. |
|
138 | - * @return float |
|
139 | - */ |
|
140 | - public function get_recurring_sub_total( $context = 'view' ) { |
|
141 | - |
|
142 | - if ( $this->is_recurring() ) { |
|
143 | - return $this->get_quantity( $context ) * $this->get_price( $context ); |
|
144 | - } |
|
145 | - |
|
146 | - return 0; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * @deprecated |
|
151 | - */ |
|
152 | - public function get_qantity( $context = 'view' ) { |
|
153 | - return $this->get_quantity( $context ); |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Get the item quantity. |
|
158 | - * |
|
159 | - * @since 1.0.19 |
|
160 | - * @param string $context View or edit context. |
|
161 | - * @return int |
|
162 | - */ |
|
163 | - public function get_quantity( $context = 'view' ) { |
|
164 | - $quantity = (int) $this->quantity; |
|
165 | - |
|
166 | - if ( empty( $quantity ) || 1 > $quantity ) { |
|
167 | - $quantity = 1; |
|
168 | - } |
|
169 | - |
|
170 | - if ( 'view' == $context ) { |
|
171 | - return apply_filters( 'getpaid_payment_form_item_quantity', $quantity, $this ); |
|
172 | - } |
|
173 | - |
|
174 | - return $quantity; |
|
175 | - |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * Get the item meta data. |
|
180 | - * |
|
181 | - * @since 1.0.19 |
|
182 | - * @param string $context View or edit context. |
|
183 | - * @return meta |
|
184 | - */ |
|
185 | - public function get_item_meta( $context = 'view' ) { |
|
186 | - $meta = $this->meta; |
|
187 | - |
|
188 | - if ( 'view' == $context ) { |
|
189 | - return apply_filters( 'getpaid_payment_form_item_meta', $meta, $this ); |
|
190 | - } |
|
191 | - |
|
192 | - return $meta; |
|
193 | - |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * Returns whether or not customers can update the item quantity. |
|
198 | - * |
|
199 | - * @since 1.0.19 |
|
200 | - * @param string $context View or edit context. |
|
201 | - * @return bool |
|
202 | - */ |
|
203 | - public function get_allow_quantities( $context = 'view' ) { |
|
204 | - $allow_quantities = (bool) $this->allow_quantities; |
|
205 | - |
|
206 | - if ( 'view' == $context ) { |
|
207 | - return apply_filters( 'getpaid_payment_form_item_allow_quantities', $allow_quantities, $this ); |
|
208 | - } |
|
209 | - |
|
210 | - return $allow_quantities; |
|
211 | - |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * Returns whether or not the item is required. |
|
216 | - * |
|
217 | - * @since 1.0.19 |
|
218 | - * @param string $context View or edit context. |
|
219 | - * @return bool |
|
220 | - */ |
|
221 | - public function get_is_required( $context = 'view' ) { |
|
222 | - $is_required = (bool) $this->is_required; |
|
223 | - |
|
224 | - if ( 'view' == $context ) { |
|
225 | - return apply_filters( 'getpaid_payment_form_item_is_required', $is_required, $this ); |
|
226 | - } |
|
227 | - |
|
228 | - return $is_required; |
|
229 | - |
|
230 | - } |
|
231 | - |
|
232 | - /** |
|
233 | - * Prepares form data for use. |
|
234 | - * |
|
235 | - * @since 1.0.19 |
|
236 | - * @return array |
|
237 | - */ |
|
238 | - public function prepare_data_for_use( $required = null ) { |
|
239 | - |
|
240 | - $required = is_null( $required ) ? $this->is_required() : $required; |
|
241 | - return array( |
|
242 | - 'title' => strip_tags( $this->get_name() ), |
|
243 | - 'id' => $this->get_id(), |
|
244 | - 'price' => $this->get_price(), |
|
245 | - 'recurring' => $this->is_recurring(), |
|
246 | - 'description' => $this->get_description(), |
|
247 | - 'allow_quantities' => $this->allows_quantities(), |
|
248 | - 'required' => $required, |
|
249 | - ); |
|
250 | - |
|
251 | - } |
|
252 | - |
|
253 | - /** |
|
254 | - * Prepares form data for ajax use. |
|
255 | - * |
|
256 | - * @since 1.0.19 |
|
257 | - * @return array |
|
258 | - */ |
|
259 | - public function prepare_data_for_invoice_edit_ajax( $currency = '' ) { |
|
260 | - |
|
261 | - $description = getpaid_item_recurring_price_help_text( $this, $currency ); |
|
262 | - |
|
263 | - if ( $description ) { |
|
264 | - $description = "<div class='getpaid-subscription-help-text'>$description</div>"; |
|
265 | - } |
|
266 | - |
|
267 | - return array( |
|
268 | - 'id' => $this->get_id(), |
|
269 | - 'texts' => array( |
|
270 | - 'item-name' => sanitize_text_field( $this->get_name() ), |
|
271 | - 'item-description' => wp_kses_post( $this->get_description() ) . $description, |
|
272 | - 'item-quantity' => absint( $this->get_quantity() ), |
|
273 | - 'item-price' => wpinv_price( wpinv_format_amount ( $this->get_price() ), $currency ), |
|
274 | - 'item-total' => wpinv_price( wpinv_format_amount( $this->get_sub_total() ), $currency ), |
|
275 | - ), |
|
276 | - 'inputs' => array( |
|
277 | - 'item-id' => $this->get_id(), |
|
278 | - 'item-name' => sanitize_text_field( $this->get_name() ), |
|
279 | - 'item-description' => wp_kses_post( $this->get_description() ), |
|
280 | - 'item-quantity' => absint( $this->get_quantity() ), |
|
281 | - 'item-price' => $this->get_price(), |
|
282 | - ) |
|
283 | - ); |
|
284 | - |
|
285 | - } |
|
286 | - |
|
287 | - /** |
|
288 | - * Prepares form data for saving (cart_details). |
|
289 | - * |
|
290 | - * @since 1.0.19 |
|
291 | - * @return array |
|
292 | - */ |
|
293 | - public function prepare_data_for_saving() { |
|
294 | - |
|
295 | - return array( |
|
296 | - 'post_id' => $this->invoice_id, |
|
297 | - 'item_id' => $this->get_id(), |
|
298 | - 'item_name' => sanitize_text_field( $this->get_raw_name() ), |
|
299 | - 'item_description' => $this->get_description(), |
|
300 | - 'tax' => $this->item_tax, |
|
301 | - 'item_price' => $this->get_price(), |
|
302 | - 'quantity' => (int) $this->get_quantity(), |
|
303 | - 'discount' => $this->item_discount, |
|
304 | - 'subtotal' => $this->get_sub_total(), |
|
305 | - 'price' => $this->get_sub_total() + $this->item_tax + $this->item_discount, |
|
306 | - 'meta' => $this->get_item_meta(), |
|
307 | - ); |
|
308 | - |
|
309 | - } |
|
84 | + * Get the item name. |
|
85 | + * |
|
86 | + * @since 1.0.19 |
|
87 | + * @param string $context View or edit context. |
|
88 | + * @return string |
|
89 | + */ |
|
90 | + public function get_name( $context = 'view' ) { |
|
91 | + $name = parent::get_name( $context ); |
|
92 | + return $name . wpinv_get_item_suffix( $this ); |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * Get the item name without a suffix. |
|
97 | + * |
|
98 | + * @since 1.0.19 |
|
99 | + * @param string $context View or edit context. |
|
100 | + * @return string |
|
101 | + */ |
|
102 | + public function get_raw_name( $context = 'view' ) { |
|
103 | + return parent::get_name( $context ); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Get the item description. |
|
108 | + * |
|
109 | + * @since 1.0.19 |
|
110 | + * @param string $context View or edit context. |
|
111 | + * @return string |
|
112 | + */ |
|
113 | + public function get_description( $context = 'view' ) { |
|
114 | + |
|
115 | + if ( isset( $this->custom_description ) ) { |
|
116 | + return $this->custom_description; |
|
117 | + } |
|
118 | + |
|
119 | + return parent::get_description( $context ); |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Returns the sub total. |
|
124 | + * |
|
125 | + * @since 1.0.19 |
|
126 | + * @param string $context View or edit context. |
|
127 | + * @return float |
|
128 | + */ |
|
129 | + public function get_sub_total( $context = 'view' ) { |
|
130 | + return $this->get_quantity( $context ) * $this->get_initial_price( $context ); |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Returns the recurring sub total. |
|
135 | + * |
|
136 | + * @since 1.0.19 |
|
137 | + * @param string $context View or edit context. |
|
138 | + * @return float |
|
139 | + */ |
|
140 | + public function get_recurring_sub_total( $context = 'view' ) { |
|
141 | + |
|
142 | + if ( $this->is_recurring() ) { |
|
143 | + return $this->get_quantity( $context ) * $this->get_price( $context ); |
|
144 | + } |
|
145 | + |
|
146 | + return 0; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * @deprecated |
|
151 | + */ |
|
152 | + public function get_qantity( $context = 'view' ) { |
|
153 | + return $this->get_quantity( $context ); |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Get the item quantity. |
|
158 | + * |
|
159 | + * @since 1.0.19 |
|
160 | + * @param string $context View or edit context. |
|
161 | + * @return int |
|
162 | + */ |
|
163 | + public function get_quantity( $context = 'view' ) { |
|
164 | + $quantity = (int) $this->quantity; |
|
165 | + |
|
166 | + if ( empty( $quantity ) || 1 > $quantity ) { |
|
167 | + $quantity = 1; |
|
168 | + } |
|
169 | + |
|
170 | + if ( 'view' == $context ) { |
|
171 | + return apply_filters( 'getpaid_payment_form_item_quantity', $quantity, $this ); |
|
172 | + } |
|
173 | + |
|
174 | + return $quantity; |
|
175 | + |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * Get the item meta data. |
|
180 | + * |
|
181 | + * @since 1.0.19 |
|
182 | + * @param string $context View or edit context. |
|
183 | + * @return meta |
|
184 | + */ |
|
185 | + public function get_item_meta( $context = 'view' ) { |
|
186 | + $meta = $this->meta; |
|
187 | + |
|
188 | + if ( 'view' == $context ) { |
|
189 | + return apply_filters( 'getpaid_payment_form_item_meta', $meta, $this ); |
|
190 | + } |
|
191 | + |
|
192 | + return $meta; |
|
193 | + |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * Returns whether or not customers can update the item quantity. |
|
198 | + * |
|
199 | + * @since 1.0.19 |
|
200 | + * @param string $context View or edit context. |
|
201 | + * @return bool |
|
202 | + */ |
|
203 | + public function get_allow_quantities( $context = 'view' ) { |
|
204 | + $allow_quantities = (bool) $this->allow_quantities; |
|
205 | + |
|
206 | + if ( 'view' == $context ) { |
|
207 | + return apply_filters( 'getpaid_payment_form_item_allow_quantities', $allow_quantities, $this ); |
|
208 | + } |
|
209 | + |
|
210 | + return $allow_quantities; |
|
211 | + |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * Returns whether or not the item is required. |
|
216 | + * |
|
217 | + * @since 1.0.19 |
|
218 | + * @param string $context View or edit context. |
|
219 | + * @return bool |
|
220 | + */ |
|
221 | + public function get_is_required( $context = 'view' ) { |
|
222 | + $is_required = (bool) $this->is_required; |
|
223 | + |
|
224 | + if ( 'view' == $context ) { |
|
225 | + return apply_filters( 'getpaid_payment_form_item_is_required', $is_required, $this ); |
|
226 | + } |
|
227 | + |
|
228 | + return $is_required; |
|
229 | + |
|
230 | + } |
|
231 | + |
|
232 | + /** |
|
233 | + * Prepares form data for use. |
|
234 | + * |
|
235 | + * @since 1.0.19 |
|
236 | + * @return array |
|
237 | + */ |
|
238 | + public function prepare_data_for_use( $required = null ) { |
|
239 | + |
|
240 | + $required = is_null( $required ) ? $this->is_required() : $required; |
|
241 | + return array( |
|
242 | + 'title' => strip_tags( $this->get_name() ), |
|
243 | + 'id' => $this->get_id(), |
|
244 | + 'price' => $this->get_price(), |
|
245 | + 'recurring' => $this->is_recurring(), |
|
246 | + 'description' => $this->get_description(), |
|
247 | + 'allow_quantities' => $this->allows_quantities(), |
|
248 | + 'required' => $required, |
|
249 | + ); |
|
250 | + |
|
251 | + } |
|
252 | + |
|
253 | + /** |
|
254 | + * Prepares form data for ajax use. |
|
255 | + * |
|
256 | + * @since 1.0.19 |
|
257 | + * @return array |
|
258 | + */ |
|
259 | + public function prepare_data_for_invoice_edit_ajax( $currency = '' ) { |
|
260 | + |
|
261 | + $description = getpaid_item_recurring_price_help_text( $this, $currency ); |
|
262 | + |
|
263 | + if ( $description ) { |
|
264 | + $description = "<div class='getpaid-subscription-help-text'>$description</div>"; |
|
265 | + } |
|
266 | + |
|
267 | + return array( |
|
268 | + 'id' => $this->get_id(), |
|
269 | + 'texts' => array( |
|
270 | + 'item-name' => sanitize_text_field( $this->get_name() ), |
|
271 | + 'item-description' => wp_kses_post( $this->get_description() ) . $description, |
|
272 | + 'item-quantity' => absint( $this->get_quantity() ), |
|
273 | + 'item-price' => wpinv_price( wpinv_format_amount ( $this->get_price() ), $currency ), |
|
274 | + 'item-total' => wpinv_price( wpinv_format_amount( $this->get_sub_total() ), $currency ), |
|
275 | + ), |
|
276 | + 'inputs' => array( |
|
277 | + 'item-id' => $this->get_id(), |
|
278 | + 'item-name' => sanitize_text_field( $this->get_name() ), |
|
279 | + 'item-description' => wp_kses_post( $this->get_description() ), |
|
280 | + 'item-quantity' => absint( $this->get_quantity() ), |
|
281 | + 'item-price' => $this->get_price(), |
|
282 | + ) |
|
283 | + ); |
|
284 | + |
|
285 | + } |
|
286 | + |
|
287 | + /** |
|
288 | + * Prepares form data for saving (cart_details). |
|
289 | + * |
|
290 | + * @since 1.0.19 |
|
291 | + * @return array |
|
292 | + */ |
|
293 | + public function prepare_data_for_saving() { |
|
294 | + |
|
295 | + return array( |
|
296 | + 'post_id' => $this->invoice_id, |
|
297 | + 'item_id' => $this->get_id(), |
|
298 | + 'item_name' => sanitize_text_field( $this->get_raw_name() ), |
|
299 | + 'item_description' => $this->get_description(), |
|
300 | + 'tax' => $this->item_tax, |
|
301 | + 'item_price' => $this->get_price(), |
|
302 | + 'quantity' => (int) $this->get_quantity(), |
|
303 | + 'discount' => $this->item_discount, |
|
304 | + 'subtotal' => $this->get_sub_total(), |
|
305 | + 'price' => $this->get_sub_total() + $this->item_tax + $this->item_discount, |
|
306 | + 'meta' => $this->get_item_meta(), |
|
307 | + ); |
|
308 | + |
|
309 | + } |
|
310 | 310 | |
311 | 311 | /* |
312 | 312 | |-------------------------------------------------------------------------- |
@@ -318,70 +318,70 @@ discard block |
||
318 | 318 | | object. |
319 | 319 | */ |
320 | 320 | |
321 | - /** |
|
322 | - * Set the item qantity. |
|
323 | - * |
|
324 | - * @since 1.0.19 |
|
325 | - * @param int $quantity The item quantity. |
|
326 | - */ |
|
327 | - public function set_quantity( $quantity ) { |
|
328 | - |
|
329 | - if ( empty( $quantity ) || ! is_numeric( $quantity ) ) { |
|
330 | - $quantity = 1; |
|
331 | - } |
|
332 | - |
|
333 | - $this->quantity = (int) $quantity; |
|
334 | - |
|
335 | - } |
|
336 | - |
|
337 | - /** |
|
338 | - * Set the item meta data. |
|
339 | - * |
|
340 | - * @since 1.0.19 |
|
341 | - * @param array $meta The item meta data. |
|
342 | - */ |
|
343 | - public function set_item_meta( $meta ) { |
|
344 | - $this->meta = maybe_unserialize( $meta ); |
|
345 | - } |
|
346 | - |
|
347 | - /** |
|
348 | - * Set whether or not the quantities are allowed. |
|
349 | - * |
|
350 | - * @since 1.0.19 |
|
351 | - * @param bool $allow_quantities |
|
352 | - */ |
|
353 | - public function set_allow_quantities( $allow_quantities ) { |
|
354 | - $this->allow_quantities = (bool) $allow_quantities; |
|
355 | - } |
|
356 | - |
|
357 | - /** |
|
358 | - * Set whether or not the item is required. |
|
359 | - * |
|
360 | - * @since 1.0.19 |
|
361 | - * @param bool $is_required |
|
362 | - */ |
|
363 | - public function set_is_required( $is_required ) { |
|
364 | - $this->is_required = (bool) $is_required; |
|
365 | - } |
|
366 | - |
|
367 | - /** |
|
368 | - * Sets the custom item description. |
|
369 | - * |
|
370 | - * @since 1.0.19 |
|
371 | - * @param string $description |
|
372 | - */ |
|
373 | - public function set_custom_description( $description ) { |
|
374 | - $this->custom_description = $description; |
|
375 | - } |
|
321 | + /** |
|
322 | + * Set the item qantity. |
|
323 | + * |
|
324 | + * @since 1.0.19 |
|
325 | + * @param int $quantity The item quantity. |
|
326 | + */ |
|
327 | + public function set_quantity( $quantity ) { |
|
328 | + |
|
329 | + if ( empty( $quantity ) || ! is_numeric( $quantity ) ) { |
|
330 | + $quantity = 1; |
|
331 | + } |
|
332 | + |
|
333 | + $this->quantity = (int) $quantity; |
|
334 | + |
|
335 | + } |
|
336 | + |
|
337 | + /** |
|
338 | + * Set the item meta data. |
|
339 | + * |
|
340 | + * @since 1.0.19 |
|
341 | + * @param array $meta The item meta data. |
|
342 | + */ |
|
343 | + public function set_item_meta( $meta ) { |
|
344 | + $this->meta = maybe_unserialize( $meta ); |
|
345 | + } |
|
346 | + |
|
347 | + /** |
|
348 | + * Set whether or not the quantities are allowed. |
|
349 | + * |
|
350 | + * @since 1.0.19 |
|
351 | + * @param bool $allow_quantities |
|
352 | + */ |
|
353 | + public function set_allow_quantities( $allow_quantities ) { |
|
354 | + $this->allow_quantities = (bool) $allow_quantities; |
|
355 | + } |
|
356 | + |
|
357 | + /** |
|
358 | + * Set whether or not the item is required. |
|
359 | + * |
|
360 | + * @since 1.0.19 |
|
361 | + * @param bool $is_required |
|
362 | + */ |
|
363 | + public function set_is_required( $is_required ) { |
|
364 | + $this->is_required = (bool) $is_required; |
|
365 | + } |
|
366 | + |
|
367 | + /** |
|
368 | + * Sets the custom item description. |
|
369 | + * |
|
370 | + * @since 1.0.19 |
|
371 | + * @param string $description |
|
372 | + */ |
|
373 | + public function set_custom_description( $description ) { |
|
374 | + $this->custom_description = $description; |
|
375 | + } |
|
376 | 376 | |
377 | 377 | /** |
378 | 378 | * We do not want to save items to the database. |
379 | 379 | * |
380 | - * @return int item id |
|
380 | + * @return int item id |
|
381 | 381 | */ |
382 | 382 | public function save( $data = array() ) { |
383 | 383 | return $this->get_id(); |
384 | - } |
|
384 | + } |
|
385 | 385 | |
386 | 386 | /* |
387 | 387 | |-------------------------------------------------------------------------- |
@@ -393,23 +393,23 @@ discard block |
||
393 | 393 | */ |
394 | 394 | |
395 | 395 | /** |
396 | - * Checks whether the item has enabled dynamic pricing. |
|
397 | - * |
|
398 | - * @since 1.0.19 |
|
399 | - * @return bool |
|
400 | - */ |
|
401 | - public function is_required() { |
|
396 | + * Checks whether the item has enabled dynamic pricing. |
|
397 | + * |
|
398 | + * @since 1.0.19 |
|
399 | + * @return bool |
|
400 | + */ |
|
401 | + public function is_required() { |
|
402 | 402 | return (bool) $this->get_is_required(); |
403 | - } |
|
404 | - |
|
405 | - /** |
|
406 | - * Checks whether users can edit the quantities. |
|
407 | - * |
|
408 | - * @since 1.0.19 |
|
409 | - * @return bool |
|
410 | - */ |
|
411 | - public function allows_quantities() { |
|
403 | + } |
|
404 | + |
|
405 | + /** |
|
406 | + * Checks whether users can edit the quantities. |
|
407 | + * |
|
408 | + * @since 1.0.19 |
|
409 | + * @return bool |
|
410 | + */ |
|
411 | + public function allows_quantities() { |
|
412 | 412 | return (bool) $this->get_allow_quantities(); |
413 | - } |
|
413 | + } |
|
414 | 414 | |
415 | 415 | } |
@@ -232,26 +232,26 @@ discard block |
||
232 | 232 | } |
233 | 233 | |
234 | 234 | function wpinv_get_chosen_gateway( $invoice_id = 0 ) { |
235 | - $gateways = array_keys( wpinv_get_enabled_payment_gateways() ); |
|
235 | + $gateways = array_keys( wpinv_get_enabled_payment_gateways() ); |
|
236 | 236 | |
237 | 237 | $chosen = false; |
238 | 238 | if ( $invoice_id > 0 && $invoice = wpinv_get_invoice( $invoice_id ) ) { |
239 | 239 | $chosen = $invoice->get_gateway(); |
240 | 240 | } |
241 | 241 | |
242 | - $chosen = isset( $_REQUEST['payment-mode'] ) ? sanitize_text_field( $_REQUEST['payment-mode'] ) : $chosen; |
|
242 | + $chosen = isset( $_REQUEST['payment-mode'] ) ? sanitize_text_field( $_REQUEST['payment-mode'] ) : $chosen; |
|
243 | 243 | |
244 | - if ( false !== $chosen ) { |
|
245 | - $chosen = preg_replace('/[^a-zA-Z0-9-_]+/', '', $chosen ); |
|
246 | - } |
|
244 | + if ( false !== $chosen ) { |
|
245 | + $chosen = preg_replace('/[^a-zA-Z0-9-_]+/', '', $chosen ); |
|
246 | + } |
|
247 | 247 | |
248 | - if ( ! empty ( $chosen ) ) { |
|
249 | - $enabled_gateway = urldecode( $chosen ); |
|
250 | - } else if ( !empty( $invoice ) && (float)$invoice->get_subtotal() <= 0 ) { |
|
251 | - $enabled_gateway = 'manual'; |
|
252 | - } else { |
|
253 | - $enabled_gateway = wpinv_get_default_gateway(); |
|
254 | - } |
|
248 | + if ( ! empty ( $chosen ) ) { |
|
249 | + $enabled_gateway = urldecode( $chosen ); |
|
250 | + } else if ( !empty( $invoice ) && (float)$invoice->get_subtotal() <= 0 ) { |
|
251 | + $enabled_gateway = 'manual'; |
|
252 | + } else { |
|
253 | + $enabled_gateway = wpinv_get_default_gateway(); |
|
254 | + } |
|
255 | 255 | |
256 | 256 | if ( !wpinv_is_gateway_active( $enabled_gateway ) && !empty( $gateways ) ) { |
257 | 257 | if(wpinv_is_gateway_active( wpinv_get_default_gateway()) ){ |
@@ -262,7 +262,7 @@ discard block |
||
262 | 262 | |
263 | 263 | } |
264 | 264 | |
265 | - return apply_filters( 'wpinv_chosen_gateway', $enabled_gateway ); |
|
265 | + return apply_filters( 'wpinv_chosen_gateway', $enabled_gateway ); |
|
266 | 266 | } |
267 | 267 | |
268 | 268 | function wpinv_record_gateway_error( $title = '', $message = '' ) { |
@@ -270,21 +270,21 @@ discard block |
||
270 | 270 | } |
271 | 271 | |
272 | 272 | function wpinv_count_sales_by_gateway( $gateway_id = 'paypal', $status = 'publish' ) { |
273 | - $ret = 0; |
|
274 | - $args = array( |
|
275 | - 'meta_key' => '_wpinv_gateway', |
|
276 | - 'meta_value' => $gateway_id, |
|
277 | - 'nopaging' => true, |
|
278 | - 'post_type' => 'wpi_invoice', |
|
279 | - 'post_status' => $status, |
|
280 | - 'fields' => 'ids' |
|
281 | - ); |
|
282 | - |
|
283 | - $payments = new WP_Query( $args ); |
|
284 | - |
|
285 | - if( $payments ) |
|
286 | - $ret = $payments->post_count; |
|
287 | - return $ret; |
|
273 | + $ret = 0; |
|
274 | + $args = array( |
|
275 | + 'meta_key' => '_wpinv_gateway', |
|
276 | + 'meta_value' => $gateway_id, |
|
277 | + 'nopaging' => true, |
|
278 | + 'post_type' => 'wpi_invoice', |
|
279 | + 'post_status' => $status, |
|
280 | + 'fields' => 'ids' |
|
281 | + ); |
|
282 | + |
|
283 | + $payments = new WP_Query( $args ); |
|
284 | + |
|
285 | + if( $payments ) |
|
286 | + $ret = $payments->post_count; |
|
287 | + return $ret; |
|
288 | 288 | } |
289 | 289 | |
290 | 290 | function wpinv_settings_update_gateways( $input ) { |
@@ -13,94 +13,94 @@ discard block |
||
13 | 13 | class GetPaid_Paypal_Gateway extends GetPaid_Payment_Gateway { |
14 | 14 | |
15 | 15 | /** |
16 | - * Payment method id. |
|
17 | - * |
|
18 | - * @var string |
|
19 | - */ |
|
16 | + * Payment method id. |
|
17 | + * |
|
18 | + * @var string |
|
19 | + */ |
|
20 | 20 | public $id = 'paypal'; |
21 | 21 | |
22 | 22 | /** |
23 | - * An array of features that this gateway supports. |
|
24 | - * |
|
25 | - * @var array |
|
26 | - */ |
|
23 | + * An array of features that this gateway supports. |
|
24 | + * |
|
25 | + * @var array |
|
26 | + */ |
|
27 | 27 | protected $supports = array( 'subscription', 'sandbox' ); |
28 | 28 | |
29 | 29 | /** |
30 | - * Payment method order. |
|
31 | - * |
|
32 | - * @var int |
|
33 | - */ |
|
30 | + * Payment method order. |
|
31 | + * |
|
32 | + * @var int |
|
33 | + */ |
|
34 | 34 | public $order = 1; |
35 | 35 | |
36 | 36 | /** |
37 | - * Stores line items to send to PayPal. |
|
38 | - * |
|
39 | - * @var array |
|
40 | - */ |
|
37 | + * Stores line items to send to PayPal. |
|
38 | + * |
|
39 | + * @var array |
|
40 | + */ |
|
41 | 41 | protected $line_items = array(); |
42 | 42 | |
43 | 43 | /** |
44 | - * Endpoint for requests from PayPal. |
|
45 | - * |
|
46 | - * @var string |
|
47 | - */ |
|
48 | - protected $notify_url; |
|
49 | - |
|
50 | - /** |
|
51 | - * Endpoint for requests to PayPal. |
|
52 | - * |
|
53 | - * @var string |
|
54 | - */ |
|
44 | + * Endpoint for requests from PayPal. |
|
45 | + * |
|
46 | + * @var string |
|
47 | + */ |
|
48 | + protected $notify_url; |
|
49 | + |
|
50 | + /** |
|
51 | + * Endpoint for requests to PayPal. |
|
52 | + * |
|
53 | + * @var string |
|
54 | + */ |
|
55 | 55 | protected $endpoint; |
56 | 56 | |
57 | 57 | /** |
58 | - * Currencies this gateway is allowed for. |
|
59 | - * |
|
60 | - * @var array |
|
61 | - */ |
|
62 | - public $currencies = array( 'AUD', 'BRL', 'CAD', 'MXN', 'NZD', 'HKD', 'SGD', 'USD', 'EUR', 'JPY', 'TRY', 'NOK', 'CZK', 'DKK', 'HUF', 'ILS', 'MYR', 'PHP', 'PLN', 'SEK', 'CHF', 'TWD', 'THB', 'GBP', 'RMB', 'RUB', 'INR' ); |
|
58 | + * Currencies this gateway is allowed for. |
|
59 | + * |
|
60 | + * @var array |
|
61 | + */ |
|
62 | + public $currencies = array( 'AUD', 'BRL', 'CAD', 'MXN', 'NZD', 'HKD', 'SGD', 'USD', 'EUR', 'JPY', 'TRY', 'NOK', 'CZK', 'DKK', 'HUF', 'ILS', 'MYR', 'PHP', 'PLN', 'SEK', 'CHF', 'TWD', 'THB', 'GBP', 'RMB', 'RUB', 'INR' ); |
|
63 | 63 | |
64 | 64 | /** |
65 | - * URL to view a transaction. |
|
66 | - * |
|
67 | - * @var string |
|
68 | - */ |
|
65 | + * URL to view a transaction. |
|
66 | + * |
|
67 | + * @var string |
|
68 | + */ |
|
69 | 69 | public $view_transaction_url = 'https://www.{sandbox}paypal.com/activity/payment/%s'; |
70 | 70 | |
71 | 71 | /** |
72 | - * URL to view a subscription. |
|
73 | - * |
|
74 | - * @var string |
|
75 | - */ |
|
76 | - public $view_subscription_url = 'https://www.{sandbox}paypal.com/cgi-bin/webscr?cmd=_profile-recurring-payments&encrypted_profile_id=%s'; |
|
72 | + * URL to view a subscription. |
|
73 | + * |
|
74 | + * @var string |
|
75 | + */ |
|
76 | + public $view_subscription_url = 'https://www.{sandbox}paypal.com/cgi-bin/webscr?cmd=_profile-recurring-payments&encrypted_profile_id=%s'; |
|
77 | 77 | |
78 | 78 | /** |
79 | - * Class constructor. |
|
80 | - */ |
|
81 | - public function __construct() { |
|
79 | + * Class constructor. |
|
80 | + */ |
|
81 | + public function __construct() { |
|
82 | 82 | |
83 | 83 | $this->title = __( 'PayPal Standard', 'invoicing' ); |
84 | 84 | $this->method_title = __( 'PayPal Standard', 'invoicing' ); |
85 | 85 | $this->checkout_button_text = __( 'Proceed to PayPal', 'invoicing' ); |
86 | 86 | $this->notify_url = wpinv_get_ipn_url( $this->id ); |
87 | 87 | |
88 | - add_filter( 'getpaid_paypal_args', array( $this, 'process_subscription' ), 10, 2 ); |
|
88 | + add_filter( 'getpaid_paypal_args', array( $this, 'process_subscription' ), 10, 2 ); |
|
89 | 89 | add_filter( 'getpaid_paypal_sandbox_notice', array( $this, 'sandbox_notice' ) ); |
90 | 90 | |
91 | 91 | parent::__construct(); |
92 | 92 | } |
93 | 93 | |
94 | 94 | /** |
95 | - * Process Payment. |
|
96 | - * |
|
97 | - * |
|
98 | - * @param WPInv_Invoice $invoice Invoice. |
|
99 | - * @param array $submission_data Posted checkout fields. |
|
100 | - * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
101 | - * @return array |
|
102 | - */ |
|
103 | - public function process_payment( $invoice, $submission_data, $submission ) { |
|
95 | + * Process Payment. |
|
96 | + * |
|
97 | + * |
|
98 | + * @param WPInv_Invoice $invoice Invoice. |
|
99 | + * @param array $submission_data Posted checkout fields. |
|
100 | + * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
101 | + * @return array |
|
102 | + */ |
|
103 | + public function process_payment( $invoice, $submission_data, $submission ) { |
|
104 | 104 | |
105 | 105 | // Get redirect url. |
106 | 106 | $paypal_redirect = $this->get_request_url( $invoice ); |
@@ -123,15 +123,15 @@ discard block |
||
123 | 123 | } |
124 | 124 | |
125 | 125 | /** |
126 | - * Get the PayPal request URL for an invoice. |
|
127 | - * |
|
128 | - * @param WPInv_Invoice $invoice Invoice object. |
|
129 | - * @return string |
|
130 | - */ |
|
131 | - public function get_request_url( $invoice ) { |
|
126 | + * Get the PayPal request URL for an invoice. |
|
127 | + * |
|
128 | + * @param WPInv_Invoice $invoice Invoice object. |
|
129 | + * @return string |
|
130 | + */ |
|
131 | + public function get_request_url( $invoice ) { |
|
132 | 132 | |
133 | 133 | // Endpoint for this request |
134 | - $this->endpoint = $this->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr?test_ipn=1&' : 'https://www.paypal.com/cgi-bin/webscr?'; |
|
134 | + $this->endpoint = $this->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr?test_ipn=1&' : 'https://www.paypal.com/cgi-bin/webscr?'; |
|
135 | 135 | |
136 | 136 | // Retrieve paypal args. |
137 | 137 | $paypal_args = map_deep( $this->get_paypal_args( $invoice ), 'urlencode' ); |
@@ -144,44 +144,44 @@ discard block |
||
144 | 144 | |
145 | 145 | return add_query_arg( $paypal_args, $this->endpoint ); |
146 | 146 | |
147 | - } |
|
147 | + } |
|
148 | 148 | |
149 | 149 | /** |
150 | - * Get PayPal Args for passing to PP. |
|
151 | - * |
|
152 | - * @param WPInv_Invoice $invoice Invoice object. |
|
153 | - * @return array |
|
154 | - */ |
|
155 | - protected function get_paypal_args( $invoice ) { |
|
150 | + * Get PayPal Args for passing to PP. |
|
151 | + * |
|
152 | + * @param WPInv_Invoice $invoice Invoice object. |
|
153 | + * @return array |
|
154 | + */ |
|
155 | + protected function get_paypal_args( $invoice ) { |
|
156 | 156 | |
157 | 157 | // Whether or not to send the line items as one item. |
158 | - $force_one_line_item = apply_filters( 'getpaid_paypal_force_one_line_item', false, $invoice ); |
|
159 | - |
|
160 | - if ( $invoice->is_recurring() || ( wpinv_use_taxes() && wpinv_prices_include_tax() ) ) { |
|
161 | - $force_one_line_item = true; |
|
162 | - } |
|
163 | - |
|
164 | - $paypal_args = apply_filters( |
|
165 | - 'getpaid_paypal_args', |
|
166 | - array_merge( |
|
167 | - $this->get_transaction_args( $invoice ), |
|
168 | - $this->get_line_item_args( $invoice, $force_one_line_item ) |
|
169 | - ), |
|
170 | - $invoice |
|
171 | - ); |
|
172 | - |
|
173 | - return $this->fix_request_length( $invoice, $paypal_args ); |
|
158 | + $force_one_line_item = apply_filters( 'getpaid_paypal_force_one_line_item', false, $invoice ); |
|
159 | + |
|
160 | + if ( $invoice->is_recurring() || ( wpinv_use_taxes() && wpinv_prices_include_tax() ) ) { |
|
161 | + $force_one_line_item = true; |
|
162 | + } |
|
163 | + |
|
164 | + $paypal_args = apply_filters( |
|
165 | + 'getpaid_paypal_args', |
|
166 | + array_merge( |
|
167 | + $this->get_transaction_args( $invoice ), |
|
168 | + $this->get_line_item_args( $invoice, $force_one_line_item ) |
|
169 | + ), |
|
170 | + $invoice |
|
171 | + ); |
|
172 | + |
|
173 | + return $this->fix_request_length( $invoice, $paypal_args ); |
|
174 | 174 | } |
175 | 175 | |
176 | 176 | /** |
177 | - * Get transaction args for paypal request. |
|
178 | - * |
|
179 | - * @param WPInv_Invoice $invoice Invoice object. |
|
180 | - * @return array |
|
181 | - */ |
|
182 | - protected function get_transaction_args( $invoice ) { |
|
183 | - |
|
184 | - return array( |
|
177 | + * Get transaction args for paypal request. |
|
178 | + * |
|
179 | + * @param WPInv_Invoice $invoice Invoice object. |
|
180 | + * @return array |
|
181 | + */ |
|
182 | + protected function get_transaction_args( $invoice ) { |
|
183 | + |
|
184 | + return array( |
|
185 | 185 | 'cmd' => '_cart', |
186 | 186 | 'business' => wpinv_get_option( 'paypal_email', false ), |
187 | 187 | 'no_shipping' => '1', |
@@ -206,16 +206,16 @@ discard block |
||
206 | 206 | } |
207 | 207 | |
208 | 208 | /** |
209 | - * Get line item args for paypal request. |
|
210 | - * |
|
211 | - * @param WPInv_Invoice $invoice Invoice object. |
|
212 | - * @param bool $force_one_line_item Create only one item for this invoice. |
|
213 | - * @return array |
|
214 | - */ |
|
215 | - protected function get_line_item_args( $invoice, $force_one_line_item = false ) { |
|
209 | + * Get line item args for paypal request. |
|
210 | + * |
|
211 | + * @param WPInv_Invoice $invoice Invoice object. |
|
212 | + * @param bool $force_one_line_item Create only one item for this invoice. |
|
213 | + * @return array |
|
214 | + */ |
|
215 | + protected function get_line_item_args( $invoice, $force_one_line_item = false ) { |
|
216 | 216 | |
217 | 217 | // Maybe send invoice as a single item. |
218 | - if ( $force_one_line_item ) { |
|
218 | + if ( $force_one_line_item ) { |
|
219 | 219 | return $this->get_line_item_args_single_item( $invoice ); |
220 | 220 | } |
221 | 221 | |
@@ -235,129 +235,129 @@ discard block |
||
235 | 235 | $line_item_args['discount_amount_cart'] = wpinv_sanitize_amount( (float) $invoice->get_total_discount(), 2 ); |
236 | 236 | } |
237 | 237 | |
238 | - return array_merge( $line_item_args, $this->get_line_items() ); |
|
238 | + return array_merge( $line_item_args, $this->get_line_items() ); |
|
239 | 239 | |
240 | 240 | } |
241 | 241 | |
242 | 242 | /** |
243 | - * Get line item args for paypal request as a single line item. |
|
244 | - * |
|
245 | - * @param WPInv_Invoice $invoice Invoice object. |
|
246 | - * @return array |
|
247 | - */ |
|
248 | - protected function get_line_item_args_single_item( $invoice ) { |
|
249 | - $this->delete_line_items(); |
|
243 | + * Get line item args for paypal request as a single line item. |
|
244 | + * |
|
245 | + * @param WPInv_Invoice $invoice Invoice object. |
|
246 | + * @return array |
|
247 | + */ |
|
248 | + protected function get_line_item_args_single_item( $invoice ) { |
|
249 | + $this->delete_line_items(); |
|
250 | 250 | |
251 | 251 | $item_name = sprintf( __( 'Invoice #%s', 'invoicing' ), $invoice->get_number() ); |
252 | - $this->add_line_item( $item_name, 1, wpinv_sanitize_amount( (float) $invoice->get_total(), 2 ), $invoice->get_id() ); |
|
252 | + $this->add_line_item( $item_name, 1, wpinv_sanitize_amount( (float) $invoice->get_total(), 2 ), $invoice->get_id() ); |
|
253 | 253 | |
254 | - return $this->get_line_items(); |
|
254 | + return $this->get_line_items(); |
|
255 | 255 | } |
256 | 256 | |
257 | 257 | /** |
258 | - * Return all line items. |
|
259 | - */ |
|
260 | - protected function get_line_items() { |
|
261 | - return $this->line_items; |
|
262 | - } |
|
258 | + * Return all line items. |
|
259 | + */ |
|
260 | + protected function get_line_items() { |
|
261 | + return $this->line_items; |
|
262 | + } |
|
263 | 263 | |
264 | 264 | /** |
265 | - * Remove all line items. |
|
266 | - */ |
|
267 | - protected function delete_line_items() { |
|
268 | - $this->line_items = array(); |
|
265 | + * Remove all line items. |
|
266 | + */ |
|
267 | + protected function delete_line_items() { |
|
268 | + $this->line_items = array(); |
|
269 | 269 | } |
270 | 270 | |
271 | 271 | /** |
272 | - * Prepare line items to send to paypal. |
|
273 | - * |
|
274 | - * @param WPInv_Invoice $invoice Invoice object. |
|
275 | - */ |
|
276 | - protected function prepare_line_items( $invoice ) { |
|
277 | - $this->delete_line_items(); |
|
278 | - |
|
279 | - // Items. |
|
280 | - foreach ( $invoice->get_items() as $item ) { |
|
281 | - $amount = $invoice->get_template() == 'amount' ? $item->get_price() : $item->get_sub_total(); |
|
282 | - $quantity = $invoice->get_template() == 'amount' ? 1 : $item->get_quantity(); |
|
283 | - $this->add_line_item( $item->get_raw_name(), $quantity, $amount, $item->get_id() ); |
|
272 | + * Prepare line items to send to paypal. |
|
273 | + * |
|
274 | + * @param WPInv_Invoice $invoice Invoice object. |
|
275 | + */ |
|
276 | + protected function prepare_line_items( $invoice ) { |
|
277 | + $this->delete_line_items(); |
|
278 | + |
|
279 | + // Items. |
|
280 | + foreach ( $invoice->get_items() as $item ) { |
|
281 | + $amount = $invoice->get_template() == 'amount' ? $item->get_price() : $item->get_sub_total(); |
|
282 | + $quantity = $invoice->get_template() == 'amount' ? 1 : $item->get_quantity(); |
|
283 | + $this->add_line_item( $item->get_raw_name(), $quantity, $amount, $item->get_id() ); |
|
284 | 284 | } |
285 | 285 | |
286 | 286 | // Fees. |
287 | - foreach ( $invoice->get_fees() as $fee => $data ) { |
|
287 | + foreach ( $invoice->get_fees() as $fee => $data ) { |
|
288 | 288 | $this->add_line_item( $fee, 1, wpinv_sanitize_amount( $data['initial_fee'] ) ); |
289 | 289 | } |
290 | 290 | |
291 | 291 | } |
292 | 292 | |
293 | 293 | /** |
294 | - * Add PayPal Line Item. |
|
295 | - * |
|
296 | - * @param string $item_name Item name. |
|
297 | - * @param int $quantity Item quantity. |
|
298 | - * @param float $amount Amount. |
|
299 | - * @param string $item_number Item number. |
|
300 | - */ |
|
301 | - protected function add_line_item( $item_name, $quantity = 1, $amount = 0.0, $item_number = '' ) { |
|
302 | - $index = ( count( $this->line_items ) / 4 ) + 1; |
|
303 | - |
|
304 | - $item = apply_filters( |
|
305 | - 'getpaid_paypal_line_item', |
|
306 | - array( |
|
307 | - 'item_name' => html_entity_decode( getpaid_limit_length( $item_name ? wp_strip_all_tags( $item_name ) : __( 'Item', 'invoicing' ), 127 ), ENT_NOQUOTES, 'UTF-8' ), |
|
308 | - 'quantity' => (int) $quantity, |
|
309 | - 'amount' => wpinv_sanitize_amount( (float) $amount, 2 ), |
|
310 | - 'item_number' => $item_number, |
|
311 | - ), |
|
312 | - $item_name, |
|
313 | - $quantity, |
|
314 | - $amount, |
|
315 | - $item_number |
|
316 | - ); |
|
317 | - |
|
318 | - $this->line_items[ 'item_name_' . $index ] = getpaid_limit_length( $item['item_name'], 127 ); |
|
294 | + * Add PayPal Line Item. |
|
295 | + * |
|
296 | + * @param string $item_name Item name. |
|
297 | + * @param int $quantity Item quantity. |
|
298 | + * @param float $amount Amount. |
|
299 | + * @param string $item_number Item number. |
|
300 | + */ |
|
301 | + protected function add_line_item( $item_name, $quantity = 1, $amount = 0.0, $item_number = '' ) { |
|
302 | + $index = ( count( $this->line_items ) / 4 ) + 1; |
|
303 | + |
|
304 | + $item = apply_filters( |
|
305 | + 'getpaid_paypal_line_item', |
|
306 | + array( |
|
307 | + 'item_name' => html_entity_decode( getpaid_limit_length( $item_name ? wp_strip_all_tags( $item_name ) : __( 'Item', 'invoicing' ), 127 ), ENT_NOQUOTES, 'UTF-8' ), |
|
308 | + 'quantity' => (int) $quantity, |
|
309 | + 'amount' => wpinv_sanitize_amount( (float) $amount, 2 ), |
|
310 | + 'item_number' => $item_number, |
|
311 | + ), |
|
312 | + $item_name, |
|
313 | + $quantity, |
|
314 | + $amount, |
|
315 | + $item_number |
|
316 | + ); |
|
317 | + |
|
318 | + $this->line_items[ 'item_name_' . $index ] = getpaid_limit_length( $item['item_name'], 127 ); |
|
319 | 319 | $this->line_items[ 'quantity_' . $index ] = $item['quantity']; |
320 | 320 | |
321 | 321 | // The price or amount of the product, service, or contribution, not including shipping, handling, or tax. |
322 | - $this->line_items[ 'amount_' . $index ] = $item['amount']; |
|
323 | - $this->line_items[ 'item_number_' . $index ] = getpaid_limit_length( $item['item_number'], 127 ); |
|
322 | + $this->line_items[ 'amount_' . $index ] = $item['amount']; |
|
323 | + $this->line_items[ 'item_number_' . $index ] = getpaid_limit_length( $item['item_number'], 127 ); |
|
324 | 324 | } |
325 | 325 | |
326 | 326 | /** |
327 | - * If the default request with line items is too long, generate a new one with only one line item. |
|
328 | - * |
|
329 | - * https://support.microsoft.com/en-us/help/208427/maximum-url-length-is-2-083-characters-in-internet-explorer. |
|
330 | - * |
|
331 | - * @param WPInv_Invoice $invoice Invoice to be sent to Paypal. |
|
332 | - * @param array $paypal_args Arguments sent to Paypal in the request. |
|
333 | - * @return array |
|
334 | - */ |
|
335 | - protected function fix_request_length( $invoice, $paypal_args ) { |
|
336 | - $max_paypal_length = 2083; |
|
337 | - $query_candidate = http_build_query( $paypal_args, '', '&' ); |
|
338 | - |
|
339 | - if ( strlen( $this->endpoint . $query_candidate ) <= $max_paypal_length ) { |
|
340 | - return $paypal_args; |
|
341 | - } |
|
342 | - |
|
343 | - return apply_filters( |
|
344 | - 'getpaid_paypal_args', |
|
345 | - array_merge( |
|
346 | - $this->get_transaction_args( $invoice ), |
|
347 | - $this->get_line_item_args( $invoice, true ) |
|
348 | - ), |
|
349 | - $invoice |
|
350 | - ); |
|
327 | + * If the default request with line items is too long, generate a new one with only one line item. |
|
328 | + * |
|
329 | + * https://support.microsoft.com/en-us/help/208427/maximum-url-length-is-2-083-characters-in-internet-explorer. |
|
330 | + * |
|
331 | + * @param WPInv_Invoice $invoice Invoice to be sent to Paypal. |
|
332 | + * @param array $paypal_args Arguments sent to Paypal in the request. |
|
333 | + * @return array |
|
334 | + */ |
|
335 | + protected function fix_request_length( $invoice, $paypal_args ) { |
|
336 | + $max_paypal_length = 2083; |
|
337 | + $query_candidate = http_build_query( $paypal_args, '', '&' ); |
|
338 | + |
|
339 | + if ( strlen( $this->endpoint . $query_candidate ) <= $max_paypal_length ) { |
|
340 | + return $paypal_args; |
|
341 | + } |
|
342 | + |
|
343 | + return apply_filters( |
|
344 | + 'getpaid_paypal_args', |
|
345 | + array_merge( |
|
346 | + $this->get_transaction_args( $invoice ), |
|
347 | + $this->get_line_item_args( $invoice, true ) |
|
348 | + ), |
|
349 | + $invoice |
|
350 | + ); |
|
351 | 351 | |
352 | 352 | } |
353 | 353 | |
354 | 354 | /** |
355 | - * Processes recurring invoices. |
|
356 | - * |
|
357 | - * @param array $paypal_args PayPal args. |
|
358 | - * @param WPInv_Invoice $invoice Invoice object. |
|
359 | - */ |
|
360 | - public function process_subscription( $paypal_args, $invoice ) { |
|
355 | + * Processes recurring invoices. |
|
356 | + * |
|
357 | + * @param array $paypal_args PayPal args. |
|
358 | + * @param WPInv_Invoice $invoice Invoice object. |
|
359 | + */ |
|
360 | + public function process_subscription( $paypal_args, $invoice ) { |
|
361 | 361 | |
362 | 362 | // Make sure this is a subscription. |
363 | 363 | if ( ! $invoice->is_recurring() || ! $subscription = wpinv_get_subscription( $invoice ) ) { |
@@ -382,11 +382,11 @@ discard block |
||
382 | 382 | |
383 | 383 | $paypal_args['a1'] = 0 == $initial_amount ? 0 : $initial_amount; |
384 | 384 | |
385 | - // Trial period length. |
|
386 | - $paypal_args['p1'] = $subscription_item->get_trial_interval(); |
|
385 | + // Trial period length. |
|
386 | + $paypal_args['p1'] = $subscription_item->get_trial_interval(); |
|
387 | 387 | |
388 | - // Trial period. |
|
389 | - $paypal_args['t1'] = $subscription_item->get_trial_period(); |
|
388 | + // Trial period. |
|
389 | + $paypal_args['t1'] = $subscription_item->get_trial_period(); |
|
390 | 390 | |
391 | 391 | } else if ( $initial_amount != $recurring_amount ) { |
392 | 392 | |
@@ -409,40 +409,40 @@ discard block |
||
409 | 409 | } |
410 | 410 | |
411 | 411 | // We have a recurring payment |
412 | - if ( ! isset( $param_number ) || 1 == $param_number ) { |
|
412 | + if ( ! isset( $param_number ) || 1 == $param_number ) { |
|
413 | 413 | |
414 | - // Subscription price |
|
415 | - $paypal_args['a3'] = $recurring_amount; |
|
414 | + // Subscription price |
|
415 | + $paypal_args['a3'] = $recurring_amount; |
|
416 | 416 | |
417 | - // Subscription duration |
|
418 | - $paypal_args['p3'] = $interval; |
|
417 | + // Subscription duration |
|
418 | + $paypal_args['p3'] = $interval; |
|
419 | 419 | |
420 | - // Subscription period |
|
421 | - $paypal_args['t3'] = $period; |
|
420 | + // Subscription period |
|
421 | + $paypal_args['t3'] = $period; |
|
422 | 422 | |
423 | 423 | } |
424 | 424 | |
425 | 425 | // Recurring payments |
426 | - if ( 1 == $bill_times || ( $initial_amount != $recurring_amount && ! $subscription_item->has_free_trial() && 2 == $bill_times ) ) { |
|
426 | + if ( 1 == $bill_times || ( $initial_amount != $recurring_amount && ! $subscription_item->has_free_trial() && 2 == $bill_times ) ) { |
|
427 | 427 | |
428 | - // Non-recurring payments |
|
429 | - $paypal_args['src'] = 0; |
|
428 | + // Non-recurring payments |
|
429 | + $paypal_args['src'] = 0; |
|
430 | 430 | |
431 | - } else { |
|
431 | + } else { |
|
432 | 432 | |
433 | - $paypal_args['src'] = 1; |
|
433 | + $paypal_args['src'] = 1; |
|
434 | 434 | |
435 | - if ( $bill_times > 0 ) { |
|
435 | + if ( $bill_times > 0 ) { |
|
436 | 436 | |
437 | - // An initial period is being used to charge a sign-up fee |
|
438 | - if ( $initial_amount != $recurring_amount && ! $subscription_item->has_free_trial() ) { |
|
439 | - $bill_times--; |
|
440 | - } |
|
437 | + // An initial period is being used to charge a sign-up fee |
|
438 | + if ( $initial_amount != $recurring_amount && ! $subscription_item->has_free_trial() ) { |
|
439 | + $bill_times--; |
|
440 | + } |
|
441 | 441 | |
442 | 442 | // Make sure it's not over the max of 52 |
443 | 443 | $paypal_args['srt'] = ( $bill_times <= 52 ? absint( $bill_times ) : 52 ); |
444 | 444 | |
445 | - } |
|
445 | + } |
|
446 | 446 | } |
447 | 447 | |
448 | 448 | // Force return URL so that order description & instructions display |
@@ -458,19 +458,19 @@ discard block |
||
458 | 458 | } |
459 | 459 | |
460 | 460 | return apply_filters( |
461 | - 'getpaid_paypal_subscription_args', |
|
462 | - $paypal_args, |
|
463 | - $invoice |
|
461 | + 'getpaid_paypal_subscription_args', |
|
462 | + $paypal_args, |
|
463 | + $invoice |
|
464 | 464 | ); |
465 | 465 | |
466 | 466 | } |
467 | 467 | |
468 | 468 | /** |
469 | - * Processes ipns and marks payments as complete. |
|
470 | - * |
|
471 | - * @return void |
|
472 | - */ |
|
473 | - public function verify_ipn() { |
|
469 | + * Processes ipns and marks payments as complete. |
|
470 | + * |
|
471 | + * @return void |
|
472 | + */ |
|
473 | + public function verify_ipn() { |
|
474 | 474 | new GetPaid_Paypal_Gateway_IPN_Handler( $this ); |
475 | 475 | } |
476 | 476 | |
@@ -480,10 +480,10 @@ discard block |
||
480 | 480 | public function sandbox_notice() { |
481 | 481 | |
482 | 482 | return sprintf( |
483 | - __( 'SANDBOX ENABLED. You can use sandbox testing accounts only. See the %sPayPal Sandbox Testing Guide%s for more details.', 'invoicing' ), |
|
484 | - '<a href="https://developer.paypal.com/docs/classic/lifecycle/ug_sandbox/">', |
|
485 | - '</a>' |
|
486 | - ); |
|
483 | + __( 'SANDBOX ENABLED. You can use sandbox testing accounts only. See the %sPayPal Sandbox Testing Guide%s for more details.', 'invoicing' ), |
|
484 | + '<a href="https://developer.paypal.com/docs/classic/lifecycle/ug_sandbox/">', |
|
485 | + '</a>' |
|
486 | + ); |
|
487 | 487 | |
488 | 488 | } |
489 | 489 |
@@ -12,431 +12,431 @@ |
||
12 | 12 | */ |
13 | 13 | class GetPaid_Paypal_Gateway_IPN_Handler { |
14 | 14 | |
15 | - /** |
|
16 | - * Payment method id. |
|
17 | - * |
|
18 | - * @var string |
|
19 | - */ |
|
20 | - protected $id = 'paypal'; |
|
21 | - |
|
22 | - /** |
|
23 | - * Payment method object. |
|
24 | - * |
|
25 | - * @var GetPaid_Paypal_Gateway |
|
26 | - */ |
|
27 | - protected $gateway; |
|
28 | - |
|
29 | - /** |
|
30 | - * Class constructor. |
|
31 | - * |
|
32 | - * @param GetPaid_Paypal_Gateway $gateway |
|
33 | - */ |
|
34 | - public function __construct( $gateway ) { |
|
35 | - $this->gateway = $gateway; |
|
36 | - $this->verify_ipn(); |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * Processes ipns and marks payments as complete. |
|
41 | - * |
|
42 | - * @return void |
|
43 | - */ |
|
44 | - public function verify_ipn() { |
|
45 | - |
|
46 | - wpinv_error_log( 'GetPaid PayPal IPN Handler' ); |
|
47 | - |
|
48 | - // Validate the IPN. |
|
49 | - if ( empty( $_POST ) || ! $this->validate_ipn() ) { |
|
50 | - wp_die( 'PayPal IPN Request Failure', 500 ); |
|
51 | - } |
|
52 | - |
|
53 | - // Process the IPN. |
|
54 | - $posted = wp_unslash( $_POST ); |
|
55 | - $invoice = $this->get_ipn_invoice( $posted ); |
|
56 | - |
|
57 | - // Abort if it was not paid by our gateway. |
|
58 | - if ( $this->id != $invoice->get_gateway() ) { |
|
59 | - wpinv_error_log( 'Aborting, Invoice was not paid via PayPal' ); |
|
60 | - wp_die( 'Invoice not paid via PayPal', 500 ); |
|
61 | - } |
|
62 | - |
|
63 | - $posted['payment_status'] = isset( $posted['payment_status'] ) ? sanitize_key( strtolower( $posted['payment_status'] ) ) : ''; |
|
64 | - $posted['txn_type'] = sanitize_key( strtolower( $posted['txn_type'] ) ); |
|
65 | - |
|
66 | - wpinv_error_log( 'Payment status:' . $posted['payment_status'] ); |
|
67 | - wpinv_error_log( 'IPN Type:' . $posted['txn_type'] ); |
|
68 | - |
|
69 | - if ( method_exists( $this, 'ipn_txn_' . $posted['txn_type'] ) ) { |
|
70 | - call_user_func( array( $this, 'ipn_txn_' . $posted['txn_type'] ), $invoice, $posted ); |
|
71 | - wpinv_error_log( 'Done processing IPN' ); |
|
72 | - wp_die( 'Processed', 200 ); |
|
73 | - } |
|
74 | - |
|
75 | - wpinv_error_log( 'Aborting, Unsupported IPN type:' . $posted['txn_type'] ); |
|
76 | - wp_die( 'Unsupported IPN type', 200 ); |
|
77 | - |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * Retrieves IPN Invoice. |
|
82 | - * |
|
83 | - * @param array $posted |
|
84 | - * @return WPInv_Invoice |
|
85 | - */ |
|
86 | - protected function get_ipn_invoice( $posted ) { |
|
87 | - |
|
88 | - wpinv_error_log( 'Retrieving PayPal IPN Response Invoice' ); |
|
89 | - |
|
90 | - if ( ! empty( $posted['custom'] ) ) { |
|
91 | - $invoice = new WPInv_Invoice( $posted['custom'] ); |
|
92 | - |
|
93 | - if ( $invoice->exists() ) { |
|
94 | - wpinv_error_log( 'Found invoice #' . $invoice->get_number() ); |
|
95 | - return $invoice; |
|
96 | - } |
|
97 | - |
|
98 | - } |
|
99 | - |
|
100 | - wpinv_error_log( 'Could not retrieve the associated invoice.' ); |
|
101 | - wp_die( 'Could not retrieve the associated invoice.', 500 ); |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Check PayPal IPN validity. |
|
106 | - */ |
|
107 | - protected function validate_ipn() { |
|
108 | - |
|
109 | - wpinv_error_log( 'Validating PayPal IPN response' ); |
|
110 | - |
|
111 | - // Retrieve the associated invoice. |
|
112 | - $posted = wp_unslash( $_POST ); |
|
113 | - $invoice = $this->get_ipn_invoice( $posted ); |
|
114 | - |
|
115 | - if ( $this->gateway->is_sandbox( $invoice ) ) { |
|
116 | - wpinv_error_log( $posted, 'Invoice was processed in sandbox hence logging the posted data' ); |
|
117 | - } |
|
118 | - |
|
119 | - // Validate the IPN. |
|
120 | - $posted['cmd'] = '_notify-validate'; |
|
121 | - |
|
122 | - // Send back post vars to paypal. |
|
123 | - $params = array( |
|
124 | - 'body' => $posted, |
|
125 | - 'timeout' => 60, |
|
126 | - 'httpversion' => '1.1', |
|
127 | - 'compress' => false, |
|
128 | - 'decompress' => false, |
|
129 | - 'user-agent' => 'GetPaid/' . WPINV_VERSION, |
|
130 | - ); |
|
131 | - |
|
132 | - // Post back to get a response. |
|
133 | - $response = wp_safe_remote_post( $this->gateway->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr', $params ); |
|
134 | - |
|
135 | - // Check to see if the request was valid. |
|
136 | - if ( ! is_wp_error( $response ) && $response['response']['code'] < 300 && strstr( $response['body'], 'VERIFIED' ) ) { |
|
137 | - wpinv_error_log( $response['body'], 'Received valid response from PayPal IPN' ); |
|
138 | - return true; |
|
139 | - } |
|
140 | - |
|
141 | - if ( is_wp_error( $response ) ) { |
|
142 | - wpinv_error_log( $response->get_error_message(), 'Received invalid response from PayPal IPN' ); |
|
143 | - return false; |
|
144 | - } |
|
15 | + /** |
|
16 | + * Payment method id. |
|
17 | + * |
|
18 | + * @var string |
|
19 | + */ |
|
20 | + protected $id = 'paypal'; |
|
21 | + |
|
22 | + /** |
|
23 | + * Payment method object. |
|
24 | + * |
|
25 | + * @var GetPaid_Paypal_Gateway |
|
26 | + */ |
|
27 | + protected $gateway; |
|
28 | + |
|
29 | + /** |
|
30 | + * Class constructor. |
|
31 | + * |
|
32 | + * @param GetPaid_Paypal_Gateway $gateway |
|
33 | + */ |
|
34 | + public function __construct( $gateway ) { |
|
35 | + $this->gateway = $gateway; |
|
36 | + $this->verify_ipn(); |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * Processes ipns and marks payments as complete. |
|
41 | + * |
|
42 | + * @return void |
|
43 | + */ |
|
44 | + public function verify_ipn() { |
|
45 | + |
|
46 | + wpinv_error_log( 'GetPaid PayPal IPN Handler' ); |
|
47 | + |
|
48 | + // Validate the IPN. |
|
49 | + if ( empty( $_POST ) || ! $this->validate_ipn() ) { |
|
50 | + wp_die( 'PayPal IPN Request Failure', 500 ); |
|
51 | + } |
|
52 | + |
|
53 | + // Process the IPN. |
|
54 | + $posted = wp_unslash( $_POST ); |
|
55 | + $invoice = $this->get_ipn_invoice( $posted ); |
|
56 | + |
|
57 | + // Abort if it was not paid by our gateway. |
|
58 | + if ( $this->id != $invoice->get_gateway() ) { |
|
59 | + wpinv_error_log( 'Aborting, Invoice was not paid via PayPal' ); |
|
60 | + wp_die( 'Invoice not paid via PayPal', 500 ); |
|
61 | + } |
|
62 | + |
|
63 | + $posted['payment_status'] = isset( $posted['payment_status'] ) ? sanitize_key( strtolower( $posted['payment_status'] ) ) : ''; |
|
64 | + $posted['txn_type'] = sanitize_key( strtolower( $posted['txn_type'] ) ); |
|
65 | + |
|
66 | + wpinv_error_log( 'Payment status:' . $posted['payment_status'] ); |
|
67 | + wpinv_error_log( 'IPN Type:' . $posted['txn_type'] ); |
|
68 | + |
|
69 | + if ( method_exists( $this, 'ipn_txn_' . $posted['txn_type'] ) ) { |
|
70 | + call_user_func( array( $this, 'ipn_txn_' . $posted['txn_type'] ), $invoice, $posted ); |
|
71 | + wpinv_error_log( 'Done processing IPN' ); |
|
72 | + wp_die( 'Processed', 200 ); |
|
73 | + } |
|
74 | + |
|
75 | + wpinv_error_log( 'Aborting, Unsupported IPN type:' . $posted['txn_type'] ); |
|
76 | + wp_die( 'Unsupported IPN type', 200 ); |
|
77 | + |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * Retrieves IPN Invoice. |
|
82 | + * |
|
83 | + * @param array $posted |
|
84 | + * @return WPInv_Invoice |
|
85 | + */ |
|
86 | + protected function get_ipn_invoice( $posted ) { |
|
87 | + |
|
88 | + wpinv_error_log( 'Retrieving PayPal IPN Response Invoice' ); |
|
89 | + |
|
90 | + if ( ! empty( $posted['custom'] ) ) { |
|
91 | + $invoice = new WPInv_Invoice( $posted['custom'] ); |
|
92 | + |
|
93 | + if ( $invoice->exists() ) { |
|
94 | + wpinv_error_log( 'Found invoice #' . $invoice->get_number() ); |
|
95 | + return $invoice; |
|
96 | + } |
|
97 | + |
|
98 | + } |
|
99 | + |
|
100 | + wpinv_error_log( 'Could not retrieve the associated invoice.' ); |
|
101 | + wp_die( 'Could not retrieve the associated invoice.', 500 ); |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Check PayPal IPN validity. |
|
106 | + */ |
|
107 | + protected function validate_ipn() { |
|
108 | + |
|
109 | + wpinv_error_log( 'Validating PayPal IPN response' ); |
|
110 | + |
|
111 | + // Retrieve the associated invoice. |
|
112 | + $posted = wp_unslash( $_POST ); |
|
113 | + $invoice = $this->get_ipn_invoice( $posted ); |
|
114 | + |
|
115 | + if ( $this->gateway->is_sandbox( $invoice ) ) { |
|
116 | + wpinv_error_log( $posted, 'Invoice was processed in sandbox hence logging the posted data' ); |
|
117 | + } |
|
118 | + |
|
119 | + // Validate the IPN. |
|
120 | + $posted['cmd'] = '_notify-validate'; |
|
121 | + |
|
122 | + // Send back post vars to paypal. |
|
123 | + $params = array( |
|
124 | + 'body' => $posted, |
|
125 | + 'timeout' => 60, |
|
126 | + 'httpversion' => '1.1', |
|
127 | + 'compress' => false, |
|
128 | + 'decompress' => false, |
|
129 | + 'user-agent' => 'GetPaid/' . WPINV_VERSION, |
|
130 | + ); |
|
131 | + |
|
132 | + // Post back to get a response. |
|
133 | + $response = wp_safe_remote_post( $this->gateway->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr', $params ); |
|
134 | + |
|
135 | + // Check to see if the request was valid. |
|
136 | + if ( ! is_wp_error( $response ) && $response['response']['code'] < 300 && strstr( $response['body'], 'VERIFIED' ) ) { |
|
137 | + wpinv_error_log( $response['body'], 'Received valid response from PayPal IPN' ); |
|
138 | + return true; |
|
139 | + } |
|
140 | + |
|
141 | + if ( is_wp_error( $response ) ) { |
|
142 | + wpinv_error_log( $response->get_error_message(), 'Received invalid response from PayPal IPN' ); |
|
143 | + return false; |
|
144 | + } |
|
145 | 145 | |
146 | - wpinv_error_log( $response['body'], 'Received invalid response from PayPal IPN' ); |
|
147 | - return false; |
|
148 | - |
|
149 | - } |
|
146 | + wpinv_error_log( $response['body'], 'Received invalid response from PayPal IPN' ); |
|
147 | + return false; |
|
148 | + |
|
149 | + } |
|
150 | 150 | |
151 | - /** |
|
152 | - * Check currency from IPN matches the invoice. |
|
153 | - * |
|
154 | - * @param WPInv_Invoice $invoice Invoice object. |
|
155 | - * @param string $currency currency to validate. |
|
156 | - */ |
|
157 | - protected function validate_ipn_currency( $invoice, $currency ) { |
|
151 | + /** |
|
152 | + * Check currency from IPN matches the invoice. |
|
153 | + * |
|
154 | + * @param WPInv_Invoice $invoice Invoice object. |
|
155 | + * @param string $currency currency to validate. |
|
156 | + */ |
|
157 | + protected function validate_ipn_currency( $invoice, $currency ) { |
|
158 | 158 | |
159 | - if ( strtolower( $invoice->get_currency() ) !== strtolower( $currency ) ) { |
|
159 | + if ( strtolower( $invoice->get_currency() ) !== strtolower( $currency ) ) { |
|
160 | 160 | |
161 | - /* translators: %s: currency code. */ |
|
162 | - $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal currencies do not match (code %s).', 'invoicing' ), $currency ) ); |
|
161 | + /* translators: %s: currency code. */ |
|
162 | + $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal currencies do not match (code %s).', 'invoicing' ), $currency ) ); |
|
163 | 163 | |
164 | - wpinv_error_log( "Currencies do not match: {$currency} instead of {$invoice->get_currency()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
165 | - } |
|
164 | + wpinv_error_log( "Currencies do not match: {$currency} instead of {$invoice->get_currency()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
165 | + } |
|
166 | 166 | |
167 | - wpinv_error_log( $currency, 'Validated IPN Currency' ); |
|
168 | - } |
|
167 | + wpinv_error_log( $currency, 'Validated IPN Currency' ); |
|
168 | + } |
|
169 | 169 | |
170 | - /** |
|
171 | - * Check payment amount from IPN matches the invoice. |
|
172 | - * |
|
173 | - * @param WPInv_Invoice $invoice Invoice object. |
|
174 | - * @param float $amount amount to validate. |
|
175 | - */ |
|
176 | - protected function validate_ipn_amount( $invoice, $amount ) { |
|
177 | - if ( number_format( $invoice->get_total(), 2, '.', '' ) !== number_format( $amount, 2, '.', '' ) ) { |
|
170 | + /** |
|
171 | + * Check payment amount from IPN matches the invoice. |
|
172 | + * |
|
173 | + * @param WPInv_Invoice $invoice Invoice object. |
|
174 | + * @param float $amount amount to validate. |
|
175 | + */ |
|
176 | + protected function validate_ipn_amount( $invoice, $amount ) { |
|
177 | + if ( number_format( $invoice->get_total(), 2, '.', '' ) !== number_format( $amount, 2, '.', '' ) ) { |
|
178 | 178 | |
179 | - /* translators: %s: Amount. */ |
|
180 | - $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal amounts do not match (gross %s).', 'invoicing' ), $amount ) ); |
|
179 | + /* translators: %s: Amount. */ |
|
180 | + $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal amounts do not match (gross %s).', 'invoicing' ), $amount ) ); |
|
181 | 181 | |
182 | - wpinv_error_log( "Amounts do not match: {$amount} instead of {$invoice->get_total()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
183 | - } |
|
182 | + wpinv_error_log( "Amounts do not match: {$amount} instead of {$invoice->get_total()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
183 | + } |
|
184 | 184 | |
185 | - wpinv_error_log( $amount, 'Validated IPN Amount' ); |
|
186 | - } |
|
185 | + wpinv_error_log( $amount, 'Validated IPN Amount' ); |
|
186 | + } |
|
187 | 187 | |
188 | - /** |
|
189 | - * Verify receiver email from PayPal. |
|
190 | - * |
|
191 | - * @param WPInv_Invoice $invoice Invoice object. |
|
192 | - * @param string $receiver_email Email to validate. |
|
193 | - */ |
|
194 | - protected function validate_ipn_receiver_email( $invoice, $receiver_email ) { |
|
195 | - $paypal_email = wpinv_get_option( 'paypal_email' ); |
|
188 | + /** |
|
189 | + * Verify receiver email from PayPal. |
|
190 | + * |
|
191 | + * @param WPInv_Invoice $invoice Invoice object. |
|
192 | + * @param string $receiver_email Email to validate. |
|
193 | + */ |
|
194 | + protected function validate_ipn_receiver_email( $invoice, $receiver_email ) { |
|
195 | + $paypal_email = wpinv_get_option( 'paypal_email' ); |
|
196 | 196 | |
197 | - if ( strcasecmp( trim( $receiver_email ), trim( $paypal_email ) ) !== 0 ) { |
|
198 | - wpinv_record_gateway_error( 'IPN Error', "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}" ); |
|
197 | + if ( strcasecmp( trim( $receiver_email ), trim( $paypal_email ) ) !== 0 ) { |
|
198 | + wpinv_record_gateway_error( 'IPN Error', "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}" ); |
|
199 | 199 | |
200 | - /* translators: %s: email address . */ |
|
201 | - $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal IPN response from a different email address (%s).', 'invoicing' ), $receiver_email ) ); |
|
200 | + /* translators: %s: email address . */ |
|
201 | + $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal IPN response from a different email address (%s).', 'invoicing' ), $receiver_email ) ); |
|
202 | 202 | |
203 | - return wpinv_error_log( "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}", 'IPN Error', __FILE__, __LINE__, true ); |
|
204 | - } |
|
203 | + return wpinv_error_log( "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}", 'IPN Error', __FILE__, __LINE__, true ); |
|
204 | + } |
|
205 | 205 | |
206 | - wpinv_error_log( 'Validated PayPal Email' ); |
|
207 | - } |
|
206 | + wpinv_error_log( 'Validated PayPal Email' ); |
|
207 | + } |
|
208 | 208 | |
209 | - /** |
|
210 | - * Handles one time payments. |
|
211 | - * |
|
212 | - * @param WPInv_Invoice $invoice Invoice object. |
|
213 | - * @param array $posted Posted data. |
|
214 | - */ |
|
215 | - protected function ipn_txn_web_accept( $invoice, $posted ) { |
|
216 | - |
|
217 | - // Collect payment details |
|
218 | - $payment_status = strtolower( $posted['payment_status'] ); |
|
219 | - $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
220 | - |
|
221 | - $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
222 | - $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
223 | - |
|
224 | - // Update the transaction id. |
|
225 | - if ( ! empty( $posted['txn_id'] ) ) { |
|
226 | - $invoice->set_transaction_id( wpinv_clean( $posted['txn_id'] ) ); |
|
227 | - $invoice->save(); |
|
228 | - } |
|
209 | + /** |
|
210 | + * Handles one time payments. |
|
211 | + * |
|
212 | + * @param WPInv_Invoice $invoice Invoice object. |
|
213 | + * @param array $posted Posted data. |
|
214 | + */ |
|
215 | + protected function ipn_txn_web_accept( $invoice, $posted ) { |
|
216 | + |
|
217 | + // Collect payment details |
|
218 | + $payment_status = strtolower( $posted['payment_status'] ); |
|
219 | + $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
220 | + |
|
221 | + $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
222 | + $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
223 | + |
|
224 | + // Update the transaction id. |
|
225 | + if ( ! empty( $posted['txn_id'] ) ) { |
|
226 | + $invoice->set_transaction_id( wpinv_clean( $posted['txn_id'] ) ); |
|
227 | + $invoice->save(); |
|
228 | + } |
|
229 | 229 | |
230 | - // Process a refund. |
|
231 | - if ( $payment_status == 'refunded' || $payment_status == 'reversed' ) { |
|
230 | + // Process a refund. |
|
231 | + if ( $payment_status == 'refunded' || $payment_status == 'reversed' ) { |
|
232 | 232 | |
233 | - update_post_meta( $invoice->get_id(), 'refunded_remotely', 1 ); |
|
233 | + update_post_meta( $invoice->get_id(), 'refunded_remotely', 1 ); |
|
234 | 234 | |
235 | - if ( ! $invoice->is_refunded() ) { |
|
236 | - $invoice->update_status( 'wpi-refunded', $posted['reason_code'] ); |
|
237 | - } |
|
235 | + if ( ! $invoice->is_refunded() ) { |
|
236 | + $invoice->update_status( 'wpi-refunded', $posted['reason_code'] ); |
|
237 | + } |
|
238 | 238 | |
239 | - return wpinv_error_log( $posted['reason_code'] ); |
|
240 | - } |
|
239 | + return wpinv_error_log( $posted['reason_code'] ); |
|
240 | + } |
|
241 | 241 | |
242 | - // Process payments. |
|
243 | - if ( $payment_status == 'completed' ) { |
|
242 | + // Process payments. |
|
243 | + if ( $payment_status == 'completed' ) { |
|
244 | 244 | |
245 | - if ( $invoice->is_paid() && 'wpi_processing' != $invoice->get_status() ) { |
|
246 | - return wpinv_error_log( 'Aborting, Invoice #' . $invoice->get_number() . ' is already paid.' ); |
|
247 | - } |
|
245 | + if ( $invoice->is_paid() && 'wpi_processing' != $invoice->get_status() ) { |
|
246 | + return wpinv_error_log( 'Aborting, Invoice #' . $invoice->get_number() . ' is already paid.' ); |
|
247 | + } |
|
248 | 248 | |
249 | - $this->validate_ipn_amount( $invoice, $posted['mc_gross'] ); |
|
249 | + $this->validate_ipn_amount( $invoice, $posted['mc_gross'] ); |
|
250 | 250 | |
251 | - $note = ''; |
|
252 | - |
|
253 | - if ( ! empty( $posted['mc_fee'] ) ) { |
|
254 | - $note = sprintf( __( 'PayPal Transaction Fee %.', 'invoicing' ), sanitize_text_field( $posted['mc_fee'] ) ); |
|
255 | - } |
|
251 | + $note = ''; |
|
252 | + |
|
253 | + if ( ! empty( $posted['mc_fee'] ) ) { |
|
254 | + $note = sprintf( __( 'PayPal Transaction Fee %.', 'invoicing' ), sanitize_text_field( $posted['mc_fee'] ) ); |
|
255 | + } |
|
256 | 256 | |
257 | - if ( ! empty( $posted['payer_status'] ) ) { |
|
258 | - $note = ' ' . sprintf( __( 'Buyer status %.', 'invoicing' ), sanitize_text_field( $posted['payer_status'] ) ); |
|
259 | - } |
|
257 | + if ( ! empty( $posted['payer_status'] ) ) { |
|
258 | + $note = ' ' . sprintf( __( 'Buyer status %.', 'invoicing' ), sanitize_text_field( $posted['payer_status'] ) ); |
|
259 | + } |
|
260 | 260 | |
261 | - $invoice->mark_paid( ( ! empty( $posted['txn_id'] ) ? sanitize_text_field( $posted['txn_id'] ) : '' ), trim( $note ) ); |
|
262 | - return wpinv_error_log( 'Invoice marked as paid.' ); |
|
261 | + $invoice->mark_paid( ( ! empty( $posted['txn_id'] ) ? sanitize_text_field( $posted['txn_id'] ) : '' ), trim( $note ) ); |
|
262 | + return wpinv_error_log( 'Invoice marked as paid.' ); |
|
263 | 263 | |
264 | - } |
|
264 | + } |
|
265 | 265 | |
266 | - // Pending payments. |
|
267 | - if ( $payment_status == 'pending' ) { |
|
266 | + // Pending payments. |
|
267 | + if ( $payment_status == 'pending' ) { |
|
268 | 268 | |
269 | - /* translators: %s: pending reason. */ |
|
270 | - $invoice->update_status( 'wpi-onhold', sprintf( __( 'Payment pending (%s).', 'invoicing' ), $posted['pending_reason'] ) ); |
|
269 | + /* translators: %s: pending reason. */ |
|
270 | + $invoice->update_status( 'wpi-onhold', sprintf( __( 'Payment pending (%s).', 'invoicing' ), $posted['pending_reason'] ) ); |
|
271 | 271 | |
272 | - return wpinv_error_log( 'Invoice marked as "payment held".' ); |
|
273 | - } |
|
272 | + return wpinv_error_log( 'Invoice marked as "payment held".' ); |
|
273 | + } |
|
274 | 274 | |
275 | - /* translators: %s: payment status. */ |
|
276 | - $invoice->update_status( 'wpi-failed', sprintf( __( 'Payment %s via IPN.', 'invoicing' ), sanitize_text_field( $posted['payment_status'] ) ) ); |
|
275 | + /* translators: %s: payment status. */ |
|
276 | + $invoice->update_status( 'wpi-failed', sprintf( __( 'Payment %s via IPN.', 'invoicing' ), sanitize_text_field( $posted['payment_status'] ) ) ); |
|
277 | 277 | |
278 | - } |
|
278 | + } |
|
279 | 279 | |
280 | - /** |
|
281 | - * Handles one time payments. |
|
282 | - * |
|
283 | - * @param WPInv_Invoice $invoice Invoice object. |
|
284 | - * @param array $posted Posted data. |
|
285 | - */ |
|
286 | - protected function ipn_txn_cart( $invoice, $posted ) { |
|
287 | - $this->ipn_txn_web_accept( $invoice, $posted ); |
|
288 | - } |
|
280 | + /** |
|
281 | + * Handles one time payments. |
|
282 | + * |
|
283 | + * @param WPInv_Invoice $invoice Invoice object. |
|
284 | + * @param array $posted Posted data. |
|
285 | + */ |
|
286 | + protected function ipn_txn_cart( $invoice, $posted ) { |
|
287 | + $this->ipn_txn_web_accept( $invoice, $posted ); |
|
288 | + } |
|
289 | 289 | |
290 | - /** |
|
291 | - * Handles subscription sign ups. |
|
292 | - * |
|
293 | - * @param WPInv_Invoice $invoice Invoice object. |
|
294 | - * @param array $posted Posted data. |
|
295 | - */ |
|
296 | - protected function ipn_txn_subscr_signup( $invoice, $posted ) { |
|
290 | + /** |
|
291 | + * Handles subscription sign ups. |
|
292 | + * |
|
293 | + * @param WPInv_Invoice $invoice Invoice object. |
|
294 | + * @param array $posted Posted data. |
|
295 | + */ |
|
296 | + protected function ipn_txn_subscr_signup( $invoice, $posted ) { |
|
297 | 297 | |
298 | - wpinv_error_log( 'Processing subscription signup' ); |
|
298 | + wpinv_error_log( 'Processing subscription signup' ); |
|
299 | 299 | |
300 | - // Make sure the invoice has a subscription. |
|
301 | - $subscription = getpaid_get_invoice_subscription( $invoice ); |
|
300 | + // Make sure the invoice has a subscription. |
|
301 | + $subscription = getpaid_get_invoice_subscription( $invoice ); |
|
302 | 302 | |
303 | - if ( empty( $subscription ) ) { |
|
304 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found' ); |
|
305 | - } |
|
303 | + if ( empty( $subscription ) ) { |
|
304 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found' ); |
|
305 | + } |
|
306 | 306 | |
307 | - // Validate the IPN. |
|
308 | - $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
309 | - $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
310 | - $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
307 | + // Validate the IPN. |
|
308 | + $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
309 | + $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
310 | + $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
311 | 311 | |
312 | - // Activate the subscription. |
|
313 | - $duration = strtotime( $subscription->get_expiration() ) - strtotime( $subscription->get_date_created() ); |
|
314 | - $subscription->set_date_created( current_time( 'mysql' ) ); |
|
315 | - $subscription->set_expiration( date( 'Y-m-d H:i:s', ( current_time( 'timestamp' ) + $duration ) ) ); |
|
316 | - $subscription->set_profile_id( sanitize_text_field( $posted['subscr_id'] ) ); |
|
317 | - $subscription->activate(); |
|
312 | + // Activate the subscription. |
|
313 | + $duration = strtotime( $subscription->get_expiration() ) - strtotime( $subscription->get_date_created() ); |
|
314 | + $subscription->set_date_created( current_time( 'mysql' ) ); |
|
315 | + $subscription->set_expiration( date( 'Y-m-d H:i:s', ( current_time( 'timestamp' ) + $duration ) ) ); |
|
316 | + $subscription->set_profile_id( sanitize_text_field( $posted['subscr_id'] ) ); |
|
317 | + $subscription->activate(); |
|
318 | 318 | |
319 | - // Set the transaction id. |
|
320 | - if ( ! empty( $posted['txn_id'] ) ) { |
|
321 | - $invoice->set_transaction_id( $posted['txn_id'] ); |
|
322 | - } |
|
319 | + // Set the transaction id. |
|
320 | + if ( ! empty( $posted['txn_id'] ) ) { |
|
321 | + $invoice->set_transaction_id( $posted['txn_id'] ); |
|
322 | + } |
|
323 | 323 | |
324 | - // Update the payment status. |
|
325 | - $invoice->mark_paid(); |
|
324 | + // Update the payment status. |
|
325 | + $invoice->mark_paid(); |
|
326 | 326 | |
327 | - $invoice->add_note( sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true ); |
|
327 | + $invoice->add_note( sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true ); |
|
328 | 328 | |
329 | - wpinv_error_log( 'Subscription started.' ); |
|
330 | - } |
|
329 | + wpinv_error_log( 'Subscription started.' ); |
|
330 | + } |
|
331 | 331 | |
332 | - /** |
|
333 | - * Handles subscription renewals. |
|
334 | - * |
|
335 | - * @param WPInv_Invoice $invoice Invoice object. |
|
336 | - * @param array $posted Posted data. |
|
337 | - */ |
|
338 | - protected function ipn_txn_subscr_payment( $invoice, $posted ) { |
|
332 | + /** |
|
333 | + * Handles subscription renewals. |
|
334 | + * |
|
335 | + * @param WPInv_Invoice $invoice Invoice object. |
|
336 | + * @param array $posted Posted data. |
|
337 | + */ |
|
338 | + protected function ipn_txn_subscr_payment( $invoice, $posted ) { |
|
339 | 339 | |
340 | - // Make sure the invoice has a subscription. |
|
341 | - $subscription = wpinv_get_subscription( $invoice ); |
|
340 | + // Make sure the invoice has a subscription. |
|
341 | + $subscription = wpinv_get_subscription( $invoice ); |
|
342 | 342 | |
343 | - if ( empty( $subscription ) ) { |
|
344 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found' ); |
|
345 | - } |
|
343 | + if ( empty( $subscription ) ) { |
|
344 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found' ); |
|
345 | + } |
|
346 | 346 | |
347 | - // Abort if this is the first payment. |
|
348 | - if ( date( 'Ynd', $subscription->get_time_created() ) == date( 'Ynd', strtotime( $posted['payment_date'] ) ) ) { |
|
349 | - $invoice->set_transaction_id( sanitize_text_field( $posted['txn_id'] ) ); |
|
350 | - $invoice->save(); |
|
351 | - return; |
|
352 | - } |
|
353 | - |
|
354 | - wpinv_error_log( 'Processing subscription renewal payment for the invoice ' . $invoice->get_id() ); |
|
347 | + // Abort if this is the first payment. |
|
348 | + if ( date( 'Ynd', $subscription->get_time_created() ) == date( 'Ynd', strtotime( $posted['payment_date'] ) ) ) { |
|
349 | + $invoice->set_transaction_id( sanitize_text_field( $posted['txn_id'] ) ); |
|
350 | + $invoice->save(); |
|
351 | + return; |
|
352 | + } |
|
353 | + |
|
354 | + wpinv_error_log( 'Processing subscription renewal payment for the invoice ' . $invoice->get_id() ); |
|
355 | 355 | |
356 | - // Abort if the payment is already recorded. |
|
357 | - if ( wpinv_get_id_by_transaction_id( $posted['txn_id'] ) ) { |
|
358 | - return wpinv_error_log( 'Aborting, Transaction ' . $posted['txn_id'] .' has already been processed' ); |
|
359 | - } |
|
356 | + // Abort if the payment is already recorded. |
|
357 | + if ( wpinv_get_id_by_transaction_id( $posted['txn_id'] ) ) { |
|
358 | + return wpinv_error_log( 'Aborting, Transaction ' . $posted['txn_id'] .' has already been processed' ); |
|
359 | + } |
|
360 | 360 | |
361 | - $args = array( |
|
362 | - 'transaction_id' => $posted['txn_id'], |
|
363 | - 'gateway' => $this->id, |
|
364 | - ); |
|
365 | - |
|
366 | - $invoice = wpinv_get_invoice( $subscription->add_payment( $args ) ); |
|
367 | - |
|
368 | - if ( empty( $invoice ) ) { |
|
369 | - return; |
|
370 | - } |
|
371 | - |
|
372 | - $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $posted['txn_id'] ), false, false, true ); |
|
373 | - $invoice->add_note( wp_sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true ); |
|
374 | - |
|
375 | - $subscription->renew(); |
|
376 | - wpinv_error_log( 'Subscription renewed.' ); |
|
377 | - |
|
378 | - } |
|
379 | - |
|
380 | - /** |
|
381 | - * Handles subscription cancelations. |
|
382 | - * |
|
383 | - * @param WPInv_Invoice $invoice Invoice object. |
|
384 | - */ |
|
385 | - protected function ipn_txn_subscr_cancel( $invoice ) { |
|
386 | - |
|
387 | - // Make sure the invoice has a subscription. |
|
388 | - $subscription = wpinv_get_subscription( $invoice ); |
|
389 | - |
|
390 | - if ( empty( $subscription ) ) { |
|
391 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found' ); |
|
392 | - } |
|
393 | - |
|
394 | - wpinv_error_log( 'Processing subscription cancellation for the invoice ' . $invoice->get_id() ); |
|
395 | - $subscription->cancel(); |
|
396 | - wpinv_error_log( 'Subscription cancelled.' ); |
|
361 | + $args = array( |
|
362 | + 'transaction_id' => $posted['txn_id'], |
|
363 | + 'gateway' => $this->id, |
|
364 | + ); |
|
365 | + |
|
366 | + $invoice = wpinv_get_invoice( $subscription->add_payment( $args ) ); |
|
367 | + |
|
368 | + if ( empty( $invoice ) ) { |
|
369 | + return; |
|
370 | + } |
|
371 | + |
|
372 | + $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $posted['txn_id'] ), false, false, true ); |
|
373 | + $invoice->add_note( wp_sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true ); |
|
374 | + |
|
375 | + $subscription->renew(); |
|
376 | + wpinv_error_log( 'Subscription renewed.' ); |
|
377 | + |
|
378 | + } |
|
379 | + |
|
380 | + /** |
|
381 | + * Handles subscription cancelations. |
|
382 | + * |
|
383 | + * @param WPInv_Invoice $invoice Invoice object. |
|
384 | + */ |
|
385 | + protected function ipn_txn_subscr_cancel( $invoice ) { |
|
386 | + |
|
387 | + // Make sure the invoice has a subscription. |
|
388 | + $subscription = wpinv_get_subscription( $invoice ); |
|
389 | + |
|
390 | + if ( empty( $subscription ) ) { |
|
391 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found' ); |
|
392 | + } |
|
393 | + |
|
394 | + wpinv_error_log( 'Processing subscription cancellation for the invoice ' . $invoice->get_id() ); |
|
395 | + $subscription->cancel(); |
|
396 | + wpinv_error_log( 'Subscription cancelled.' ); |
|
397 | 397 | |
398 | - } |
|
398 | + } |
|
399 | 399 | |
400 | - /** |
|
401 | - * Handles subscription completions. |
|
402 | - * |
|
403 | - * @param WPInv_Invoice $invoice Invoice object. |
|
404 | - * @param array $posted Posted data. |
|
405 | - */ |
|
406 | - protected function ipn_txn_subscr_eot( $invoice ) { |
|
407 | - |
|
408 | - // Make sure the invoice has a subscription. |
|
409 | - $subscription = wpinv_get_subscription( $invoice ); |
|
400 | + /** |
|
401 | + * Handles subscription completions. |
|
402 | + * |
|
403 | + * @param WPInv_Invoice $invoice Invoice object. |
|
404 | + * @param array $posted Posted data. |
|
405 | + */ |
|
406 | + protected function ipn_txn_subscr_eot( $invoice ) { |
|
407 | + |
|
408 | + // Make sure the invoice has a subscription. |
|
409 | + $subscription = wpinv_get_subscription( $invoice ); |
|
410 | 410 | |
411 | - if ( empty( $subscription ) ) { |
|
412 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found' ); |
|
413 | - } |
|
411 | + if ( empty( $subscription ) ) { |
|
412 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found' ); |
|
413 | + } |
|
414 | 414 | |
415 | - wpinv_error_log( 'Processing subscription end of life for the invoice ' . $invoice->get_id() ); |
|
416 | - $subscription->complete(); |
|
417 | - wpinv_error_log( 'Subscription completed.' ); |
|
415 | + wpinv_error_log( 'Processing subscription end of life for the invoice ' . $invoice->get_id() ); |
|
416 | + $subscription->complete(); |
|
417 | + wpinv_error_log( 'Subscription completed.' ); |
|
418 | 418 | |
419 | - } |
|
419 | + } |
|
420 | 420 | |
421 | - /** |
|
422 | - * Handles subscription fails. |
|
423 | - * |
|
424 | - * @param WPInv_Invoice $invoice Invoice object. |
|
425 | - * @param array $posted Posted data. |
|
426 | - */ |
|
427 | - protected function ipn_txn_subscr_failed( $invoice ) { |
|
421 | + /** |
|
422 | + * Handles subscription fails. |
|
423 | + * |
|
424 | + * @param WPInv_Invoice $invoice Invoice object. |
|
425 | + * @param array $posted Posted data. |
|
426 | + */ |
|
427 | + protected function ipn_txn_subscr_failed( $invoice ) { |
|
428 | 428 | |
429 | - // Make sure the invoice has a subscription. |
|
430 | - $subscription = wpinv_get_subscription( $invoice ); |
|
429 | + // Make sure the invoice has a subscription. |
|
430 | + $subscription = wpinv_get_subscription( $invoice ); |
|
431 | 431 | |
432 | - if ( empty( $subscription ) ) { |
|
433 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found' ); |
|
434 | - } |
|
432 | + if ( empty( $subscription ) ) { |
|
433 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found' ); |
|
434 | + } |
|
435 | 435 | |
436 | - wpinv_error_log( 'Processing subscription payment failure for the invoice ' . $invoice->get_id() ); |
|
437 | - $subscription->failing(); |
|
438 | - wpinv_error_log( 'Subscription marked as failing.' ); |
|
436 | + wpinv_error_log( 'Processing subscription payment failure for the invoice ' . $invoice->get_id() ); |
|
437 | + $subscription->failing(); |
|
438 | + wpinv_error_log( 'Subscription marked as failing.' ); |
|
439 | 439 | |
440 | - } |
|
440 | + } |
|
441 | 441 | |
442 | 442 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | if ( ! defined( 'ABSPATH' ) ) { |
3 | - exit; |
|
3 | + exit; |
|
4 | 4 | } |
5 | 5 | |
6 | 6 | /** |
@@ -10,55 +10,55 @@ discard block |
||
10 | 10 | class GetPaid_Payment_Form extends GetPaid_Data { |
11 | 11 | |
12 | 12 | /** |
13 | - * Which data store to load. |
|
14 | - * |
|
15 | - * @var string |
|
16 | - */ |
|
13 | + * Which data store to load. |
|
14 | + * |
|
15 | + * @var string |
|
16 | + */ |
|
17 | 17 | protected $data_store_name = 'payment_form'; |
18 | 18 | |
19 | 19 | /** |
20 | - * This is the name of this object type. |
|
21 | - * |
|
22 | - * @var string |
|
23 | - */ |
|
24 | - protected $object_type = 'payment_form'; |
|
20 | + * This is the name of this object type. |
|
21 | + * |
|
22 | + * @var string |
|
23 | + */ |
|
24 | + protected $object_type = 'payment_form'; |
|
25 | 25 | |
26 | 26 | /** |
27 | - * Form Data array. This is the core form data exposed in APIs. |
|
28 | - * |
|
29 | - * @since 1.0.19 |
|
30 | - * @var array |
|
31 | - */ |
|
32 | - protected $data = array( |
|
33 | - 'status' => 'draft', |
|
34 | - 'version' => '', |
|
35 | - 'date_created' => null, |
|
27 | + * Form Data array. This is the core form data exposed in APIs. |
|
28 | + * |
|
29 | + * @since 1.0.19 |
|
30 | + * @var array |
|
31 | + */ |
|
32 | + protected $data = array( |
|
33 | + 'status' => 'draft', |
|
34 | + 'version' => '', |
|
35 | + 'date_created' => null, |
|
36 | 36 | 'date_modified' => null, |
37 | 37 | 'name' => '', |
38 | 38 | 'author' => 1, |
39 | 39 | 'elements' => null, |
40 | - 'items' => null, |
|
41 | - 'earned' => 0, |
|
42 | - 'refunded' => 0, |
|
43 | - 'cancelled' => 0, |
|
44 | - 'failed' => 0, |
|
45 | - ); |
|
46 | - |
|
47 | - /** |
|
48 | - * Stores meta in cache for future reads. |
|
49 | - * |
|
50 | - * A group must be set to to enable caching. |
|
51 | - * |
|
52 | - * @var string |
|
53 | - */ |
|
54 | - protected $cache_group = 'getpaid_forms'; |
|
55 | - |
|
56 | - /** |
|
57 | - * Stores a reference to the invoice if the form is for an invoice.. |
|
58 | - * |
|
59 | - * @var WPInv_Invoice |
|
60 | - */ |
|
61 | - public $invoice = 0; |
|
40 | + 'items' => null, |
|
41 | + 'earned' => 0, |
|
42 | + 'refunded' => 0, |
|
43 | + 'cancelled' => 0, |
|
44 | + 'failed' => 0, |
|
45 | + ); |
|
46 | + |
|
47 | + /** |
|
48 | + * Stores meta in cache for future reads. |
|
49 | + * |
|
50 | + * A group must be set to to enable caching. |
|
51 | + * |
|
52 | + * @var string |
|
53 | + */ |
|
54 | + protected $cache_group = 'getpaid_forms'; |
|
55 | + |
|
56 | + /** |
|
57 | + * Stores a reference to the invoice if the form is for an invoice.. |
|
58 | + * |
|
59 | + * @var WPInv_Invoice |
|
60 | + */ |
|
61 | + public $invoice = 0; |
|
62 | 62 | |
63 | 63 | /** |
64 | 64 | * Stores a reference to the original WP_Post object |
@@ -68,35 +68,35 @@ discard block |
||
68 | 68 | protected $post = null; |
69 | 69 | |
70 | 70 | /** |
71 | - * Get the form if ID is passed, otherwise the form is new and empty. |
|
72 | - * |
|
73 | - * @param int|object|GetPaid_Payment_Form|WP_Post $form Form to read. |
|
74 | - */ |
|
75 | - public function __construct( $form = 0 ) { |
|
76 | - parent::__construct( $form ); |
|
71 | + * Get the form if ID is passed, otherwise the form is new and empty. |
|
72 | + * |
|
73 | + * @param int|object|GetPaid_Payment_Form|WP_Post $form Form to read. |
|
74 | + */ |
|
75 | + public function __construct( $form = 0 ) { |
|
76 | + parent::__construct( $form ); |
|
77 | 77 | |
78 | - if ( is_numeric( $form ) && $form > 0 ) { |
|
79 | - $this->set_id( $form ); |
|
80 | - } elseif ( $form instanceof self ) { |
|
78 | + if ( is_numeric( $form ) && $form > 0 ) { |
|
79 | + $this->set_id( $form ); |
|
80 | + } elseif ( $form instanceof self ) { |
|
81 | 81 | |
82 | - $this->set_id( $form->get_id() ); |
|
83 | - $this->invoice = $form->invoice; |
|
82 | + $this->set_id( $form->get_id() ); |
|
83 | + $this->invoice = $form->invoice; |
|
84 | 84 | |
85 | - } elseif ( ! empty( $form->ID ) ) { |
|
86 | - $this->set_id( $form->ID ); |
|
87 | - } else { |
|
88 | - $this->set_object_read( true ); |
|
89 | - } |
|
85 | + } elseif ( ! empty( $form->ID ) ) { |
|
86 | + $this->set_id( $form->ID ); |
|
87 | + } else { |
|
88 | + $this->set_object_read( true ); |
|
89 | + } |
|
90 | 90 | |
91 | 91 | // Load the datastore. |
92 | - $this->data_store = GetPaid_Data_Store::load( $this->data_store_name ); |
|
92 | + $this->data_store = GetPaid_Data_Store::load( $this->data_store_name ); |
|
93 | 93 | |
94 | - if ( $this->get_id() > 0 ) { |
|
94 | + if ( $this->get_id() > 0 ) { |
|
95 | 95 | $this->post = get_post( $this->get_id() ); |
96 | - $this->data_store->read( $this ); |
|
96 | + $this->data_store->read( $this ); |
|
97 | 97 | } |
98 | 98 | |
99 | - } |
|
99 | + } |
|
100 | 100 | |
101 | 101 | /* |
102 | 102 | |-------------------------------------------------------------------------- |
@@ -114,358 +114,358 @@ discard block |
||
114 | 114 | */ |
115 | 115 | |
116 | 116 | /** |
117 | - * Get plugin version when the form was created. |
|
118 | - * |
|
119 | - * @since 1.0.19 |
|
120 | - * @param string $context View or edit context. |
|
121 | - * @return string |
|
122 | - */ |
|
123 | - public function get_version( $context = 'view' ) { |
|
124 | - return $this->get_prop( 'version', $context ); |
|
117 | + * Get plugin version when the form was created. |
|
118 | + * |
|
119 | + * @since 1.0.19 |
|
120 | + * @param string $context View or edit context. |
|
121 | + * @return string |
|
122 | + */ |
|
123 | + public function get_version( $context = 'view' ) { |
|
124 | + return $this->get_prop( 'version', $context ); |
|
125 | 125 | } |
126 | 126 | |
127 | 127 | /** |
128 | - * Get date when the form was created. |
|
129 | - * |
|
130 | - * @since 1.0.19 |
|
131 | - * @param string $context View or edit context. |
|
132 | - * @return string |
|
133 | - */ |
|
134 | - public function get_date_created( $context = 'view' ) { |
|
135 | - return $this->get_prop( 'date_created', $context ); |
|
128 | + * Get date when the form was created. |
|
129 | + * |
|
130 | + * @since 1.0.19 |
|
131 | + * @param string $context View or edit context. |
|
132 | + * @return string |
|
133 | + */ |
|
134 | + public function get_date_created( $context = 'view' ) { |
|
135 | + return $this->get_prop( 'date_created', $context ); |
|
136 | 136 | } |
137 | 137 | |
138 | 138 | /** |
139 | - * Get GMT date when the form was created. |
|
140 | - * |
|
141 | - * @since 1.0.19 |
|
142 | - * @param string $context View or edit context. |
|
143 | - * @return string |
|
144 | - */ |
|
145 | - public function get_date_created_gmt( $context = 'view' ) { |
|
139 | + * Get GMT date when the form was created. |
|
140 | + * |
|
141 | + * @since 1.0.19 |
|
142 | + * @param string $context View or edit context. |
|
143 | + * @return string |
|
144 | + */ |
|
145 | + public function get_date_created_gmt( $context = 'view' ) { |
|
146 | 146 | $date = $this->get_date_created( $context ); |
147 | 147 | |
148 | 148 | if ( $date ) { |
149 | 149 | $date = get_gmt_from_date( $date ); |
150 | 150 | } |
151 | - return $date; |
|
151 | + return $date; |
|
152 | 152 | } |
153 | 153 | |
154 | 154 | /** |
155 | - * Get date when the form was last modified. |
|
156 | - * |
|
157 | - * @since 1.0.19 |
|
158 | - * @param string $context View or edit context. |
|
159 | - * @return string |
|
160 | - */ |
|
161 | - public function get_date_modified( $context = 'view' ) { |
|
162 | - return $this->get_prop( 'date_modified', $context ); |
|
155 | + * Get date when the form was last modified. |
|
156 | + * |
|
157 | + * @since 1.0.19 |
|
158 | + * @param string $context View or edit context. |
|
159 | + * @return string |
|
160 | + */ |
|
161 | + public function get_date_modified( $context = 'view' ) { |
|
162 | + return $this->get_prop( 'date_modified', $context ); |
|
163 | 163 | } |
164 | 164 | |
165 | 165 | /** |
166 | - * Get GMT date when the form was last modified. |
|
167 | - * |
|
168 | - * @since 1.0.19 |
|
169 | - * @param string $context View or edit context. |
|
170 | - * @return string |
|
171 | - */ |
|
172 | - public function get_date_modified_gmt( $context = 'view' ) { |
|
166 | + * Get GMT date when the form was last modified. |
|
167 | + * |
|
168 | + * @since 1.0.19 |
|
169 | + * @param string $context View or edit context. |
|
170 | + * @return string |
|
171 | + */ |
|
172 | + public function get_date_modified_gmt( $context = 'view' ) { |
|
173 | 173 | $date = $this->get_date_modified( $context ); |
174 | 174 | |
175 | 175 | if ( $date ) { |
176 | 176 | $date = get_gmt_from_date( $date ); |
177 | 177 | } |
178 | - return $date; |
|
178 | + return $date; |
|
179 | 179 | } |
180 | 180 | |
181 | 181 | /** |
182 | - * Get the form name. |
|
183 | - * |
|
184 | - * @since 1.0.19 |
|
185 | - * @param string $context View or edit context. |
|
186 | - * @return string |
|
187 | - */ |
|
188 | - public function get_name( $context = 'view' ) { |
|
189 | - return $this->get_prop( 'name', $context ); |
|
182 | + * Get the form name. |
|
183 | + * |
|
184 | + * @since 1.0.19 |
|
185 | + * @param string $context View or edit context. |
|
186 | + * @return string |
|
187 | + */ |
|
188 | + public function get_name( $context = 'view' ) { |
|
189 | + return $this->get_prop( 'name', $context ); |
|
190 | 190 | } |
191 | 191 | |
192 | 192 | /** |
193 | - * Alias of self::get_name(). |
|
194 | - * |
|
195 | - * @since 1.0.19 |
|
196 | - * @param string $context View or edit context. |
|
197 | - * @return string |
|
198 | - */ |
|
199 | - public function get_title( $context = 'view' ) { |
|
200 | - return $this->get_name( $context ); |
|
201 | - } |
|
193 | + * Alias of self::get_name(). |
|
194 | + * |
|
195 | + * @since 1.0.19 |
|
196 | + * @param string $context View or edit context. |
|
197 | + * @return string |
|
198 | + */ |
|
199 | + public function get_title( $context = 'view' ) { |
|
200 | + return $this->get_name( $context ); |
|
201 | + } |
|
202 | 202 | |
203 | 203 | /** |
204 | - * Get the owner of the form. |
|
205 | - * |
|
206 | - * @since 1.0.19 |
|
207 | - * @param string $context View or edit context. |
|
208 | - * @return int |
|
209 | - */ |
|
210 | - public function get_author( $context = 'view' ) { |
|
211 | - return (int) $this->get_prop( 'author', $context ); |
|
204 | + * Get the owner of the form. |
|
205 | + * |
|
206 | + * @since 1.0.19 |
|
207 | + * @param string $context View or edit context. |
|
208 | + * @return int |
|
209 | + */ |
|
210 | + public function get_author( $context = 'view' ) { |
|
211 | + return (int) $this->get_prop( 'author', $context ); |
|
212 | 212 | } |
213 | 213 | |
214 | 214 | /** |
215 | - * Get the elements that make up the form. |
|
216 | - * |
|
217 | - * @since 1.0.19 |
|
218 | - * @param string $context View or edit context. |
|
219 | - * @return array |
|
220 | - */ |
|
221 | - public function get_elements( $context = 'view' ) { |
|
222 | - $elements = $this->get_prop( 'elements', $context ); |
|
215 | + * Get the elements that make up the form. |
|
216 | + * |
|
217 | + * @since 1.0.19 |
|
218 | + * @param string $context View or edit context. |
|
219 | + * @return array |
|
220 | + */ |
|
221 | + public function get_elements( $context = 'view' ) { |
|
222 | + $elements = $this->get_prop( 'elements', $context ); |
|
223 | 223 | |
224 | - if ( empty( $elements ) || ! is_array( $elements ) ) { |
|
224 | + if ( empty( $elements ) || ! is_array( $elements ) ) { |
|
225 | 225 | return wpinv_get_data( 'sample-payment-form' ); |
226 | - } |
|
226 | + } |
|
227 | 227 | |
228 | - // Ensure that all required elements exist. |
|
229 | - $_elements = array(); |
|
230 | - foreach ( $elements as $element ) { |
|
228 | + // Ensure that all required elements exist. |
|
229 | + $_elements = array(); |
|
230 | + foreach ( $elements as $element ) { |
|
231 | 231 | |
232 | - if ( $element['type'] == 'pay_button' && ! $this->has_element_type( 'gateway_select' ) ) { |
|
232 | + if ( $element['type'] == 'pay_button' && ! $this->has_element_type( 'gateway_select' ) ) { |
|
233 | 233 | |
234 | - $_elements[] = array( |
|
235 | - 'text' => __( 'Select Payment Method', 'invoicing' ), |
|
236 | - 'id' => 'gtscicd', |
|
237 | - 'name' => 'gtscicd', |
|
238 | - 'type' => 'gateway_select', |
|
239 | - 'premade' => true |
|
234 | + $_elements[] = array( |
|
235 | + 'text' => __( 'Select Payment Method', 'invoicing' ), |
|
236 | + 'id' => 'gtscicd', |
|
237 | + 'name' => 'gtscicd', |
|
238 | + 'type' => 'gateway_select', |
|
239 | + 'premade' => true |
|
240 | 240 | |
241 | - ); |
|
241 | + ); |
|
242 | 242 | |
243 | - } |
|
243 | + } |
|
244 | 244 | |
245 | - $_elements[] = $element; |
|
245 | + $_elements[] = $element; |
|
246 | 246 | |
247 | - } |
|
247 | + } |
|
248 | 248 | |
249 | 249 | return $_elements; |
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * Get the items sold via the form. |
|
254 | - * |
|
255 | - * @since 1.0.19 |
|
256 | - * @param string $context View or edit context. |
|
257 | - * @param string $return objects or arrays. |
|
258 | - * @return GetPaid_Form_Item[] |
|
259 | - */ |
|
260 | - public function get_items( $context = 'view', $return = 'objects' ) { |
|
261 | - $items = $this->get_prop( 'items', $context ); |
|
262 | - |
|
263 | - if ( empty( $items ) || ! is_array( $items ) ) { |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * Get the items sold via the form. |
|
254 | + * |
|
255 | + * @since 1.0.19 |
|
256 | + * @param string $context View or edit context. |
|
257 | + * @param string $return objects or arrays. |
|
258 | + * @return GetPaid_Form_Item[] |
|
259 | + */ |
|
260 | + public function get_items( $context = 'view', $return = 'objects' ) { |
|
261 | + $items = $this->get_prop( 'items', $context ); |
|
262 | + |
|
263 | + if ( empty( $items ) || ! is_array( $items ) ) { |
|
264 | 264 | $items = wpinv_get_data( 'sample-payment-form-items' ); |
265 | - } |
|
265 | + } |
|
266 | + |
|
267 | + // Convert the items. |
|
268 | + $prepared = array(); |
|
266 | 269 | |
267 | - // Convert the items. |
|
268 | - $prepared = array(); |
|
270 | + foreach ( $items as $key => $value ) { |
|
269 | 271 | |
270 | - foreach ( $items as $key => $value ) { |
|
272 | + // Form items. |
|
273 | + if ( $value instanceof GetPaid_Form_Item ) { |
|
271 | 274 | |
272 | - // Form items. |
|
273 | - if ( $value instanceof GetPaid_Form_Item ) { |
|
275 | + if ( $value->can_purchase() ) { |
|
276 | + $prepared[] = $value; |
|
277 | + } |
|
274 | 278 | |
275 | - if ( $value->can_purchase() ) { |
|
276 | - $prepared[] = $value; |
|
277 | - } |
|
279 | + continue; |
|
278 | 280 | |
279 | - continue; |
|
281 | + } |
|
280 | 282 | |
281 | - } |
|
283 | + // $item_id => $quantity (buy buttons) |
|
284 | + if ( is_numeric( $key ) && is_numeric( $value ) ) { |
|
285 | + $item = new GetPaid_Form_Item( $key ); |
|
282 | 286 | |
283 | - // $item_id => $quantity (buy buttons) |
|
284 | - if ( is_numeric( $key ) && is_numeric( $value ) ) { |
|
285 | - $item = new GetPaid_Form_Item( $key ); |
|
287 | + if ( $item->can_purchase() ) { |
|
286 | 288 | |
287 | - if ( $item->can_purchase() ) { |
|
289 | + $value = (int) $value; |
|
290 | + $item->set_quantity( $value ); |
|
291 | + if ( 0 == $value ) { |
|
292 | + $item->set_quantity( 1 ); |
|
293 | + $item->set_allow_quantities( true ); |
|
294 | + } |
|
288 | 295 | |
289 | - $value = (int) $value; |
|
290 | - $item->set_quantity( $value ); |
|
291 | - if ( 0 == $value ) { |
|
292 | - $item->set_quantity( 1 ); |
|
293 | - $item->set_allow_quantities( true ); |
|
294 | - } |
|
296 | + $prepared[] = $item; |
|
297 | + } |
|
295 | 298 | |
296 | - $prepared[] = $item; |
|
297 | - } |
|
299 | + continue; |
|
300 | + } |
|
298 | 301 | |
299 | - continue; |
|
300 | - } |
|
302 | + // Items saved via payment forms editor. |
|
303 | + if ( is_array( $value ) && isset( $value['id'] ) ) { |
|
301 | 304 | |
302 | - // Items saved via payment forms editor. |
|
303 | - if ( is_array( $value ) && isset( $value['id'] ) ) { |
|
305 | + $item = new GetPaid_Form_Item( $value['id'] ); |
|
304 | 306 | |
305 | - $item = new GetPaid_Form_Item( $value['id'] ); |
|
307 | + if ( ! $item->can_purchase() ) { |
|
308 | + continue; |
|
309 | + } |
|
306 | 310 | |
307 | - if ( ! $item->can_purchase() ) { |
|
308 | - continue; |
|
309 | - } |
|
311 | + // Sub-total (Cart items). |
|
312 | + if ( isset( $value['subtotal'] ) ) { |
|
313 | + $item->set_price( $value['subtotal'] ); |
|
314 | + } |
|
310 | 315 | |
311 | - // Sub-total (Cart items). |
|
312 | - if ( isset( $value['subtotal'] ) ) { |
|
313 | - $item->set_price( $value['subtotal'] ); |
|
314 | - } |
|
316 | + if ( isset( $value['quantity'] ) ) { |
|
317 | + $item->set_quantity( $value['quantity'] ); |
|
318 | + } |
|
315 | 319 | |
316 | - if ( isset( $value['quantity'] ) ) { |
|
317 | - $item->set_quantity( $value['quantity'] ); |
|
318 | - } |
|
320 | + if ( isset( $value['allow_quantities'] ) ) { |
|
321 | + $item->set_allow_quantities( $value['allow_quantities'] ); |
|
322 | + } |
|
319 | 323 | |
320 | - if ( isset( $value['allow_quantities'] ) ) { |
|
321 | - $item->set_allow_quantities( $value['allow_quantities'] ); |
|
322 | - } |
|
324 | + if ( isset( $value['required'] ) ) { |
|
325 | + $item->set_is_required( $value['required'] ); |
|
326 | + } |
|
323 | 327 | |
324 | - if ( isset( $value['required'] ) ) { |
|
325 | - $item->set_is_required( $value['required'] ); |
|
326 | - } |
|
328 | + if ( isset( $value['description'] ) ) { |
|
329 | + $item->set_custom_description( $value['description'] ); |
|
330 | + } |
|
327 | 331 | |
328 | - if ( isset( $value['description'] ) ) { |
|
329 | - $item->set_custom_description( $value['description'] ); |
|
330 | - } |
|
332 | + $prepared[] = $item; |
|
333 | + continue; |
|
331 | 334 | |
332 | - $prepared[] = $item; |
|
333 | - continue; |
|
335 | + } |
|
336 | + |
|
337 | + // $item_id => array( 'price' => 10 ) (item variations) |
|
338 | + if ( is_numeric( $key ) && is_array( $value ) ) { |
|
339 | + $item = new GetPaid_Form_Item( $key ); |
|
340 | + |
|
341 | + if ( isset( $value['price'] ) && $item->user_can_set_their_price() ) { |
|
342 | + $item->set_price( $value['price'] ); |
|
343 | + } |
|
344 | + |
|
345 | + if ( $item->can_purchase() ) { |
|
346 | + $prepared[] = $item; |
|
347 | + } |
|
348 | + |
|
349 | + continue; |
|
350 | + } |
|
351 | + |
|
352 | + } |
|
353 | + |
|
354 | + if ( 'objects' == $return && 'view' == $context ) { |
|
355 | + return $prepared; |
|
356 | + } |
|
357 | + |
|
358 | + $items = array(); |
|
359 | + foreach ( $prepared as $item ) { |
|
360 | + $items[] = $item->prepare_data_for_use(); |
|
361 | + } |
|
362 | + |
|
363 | + return $items; |
|
364 | + } |
|
365 | + |
|
366 | + /** |
|
367 | + * Get a single item belonging to the form. |
|
368 | + * |
|
369 | + * @since 1.0.19 |
|
370 | + * @param int $item_id The item id to return. |
|
371 | + * @return GetPaid_Form_Item|bool |
|
372 | + */ |
|
373 | + public function get_item( $item_id ) { |
|
374 | + |
|
375 | + if ( empty( $item_id ) || ! is_numeric( $item_id ) ) { |
|
376 | + return false; |
|
377 | + } |
|
378 | + |
|
379 | + foreach( $this->get_items() as $item ) { |
|
380 | + if ( $item->get_id() == (int) $item_id ) { |
|
381 | + return $item; |
|
382 | + } |
|
383 | + } |
|
384 | + |
|
385 | + return false; |
|
386 | + |
|
387 | + } |
|
388 | + |
|
389 | + /** |
|
390 | + * Gets a single element. |
|
391 | + * |
|
392 | + * @since 1.0.19 |
|
393 | + * @param string $element_type The element type to return. |
|
394 | + * @return array|bool |
|
395 | + */ |
|
396 | + public function get_element_type( $element_type ) { |
|
397 | + |
|
398 | + if ( empty( $element_type ) || ! is_scalar( $element_type ) ) { |
|
399 | + return false; |
|
400 | + } |
|
401 | + |
|
402 | + foreach ( $this->get_prop( 'elements' ) as $element ) { |
|
403 | + |
|
404 | + if ( $element['type'] == $element_type ) { |
|
405 | + return $element; |
|
406 | + } |
|
407 | + |
|
408 | + } |
|
409 | + |
|
410 | + return false; |
|
411 | + |
|
412 | + } |
|
413 | + |
|
414 | + /** |
|
415 | + * Get the total amount earned via this form. |
|
416 | + * |
|
417 | + * @since 1.0.19 |
|
418 | + * @param string $context View or edit context. |
|
419 | + * @return array |
|
420 | + */ |
|
421 | + public function get_earned( $context = 'view' ) { |
|
422 | + return $this->get_prop( 'earned', $context ); |
|
423 | + } |
|
424 | + |
|
425 | + /** |
|
426 | + * Get the total amount refunded via this form. |
|
427 | + * |
|
428 | + * @since 1.0.19 |
|
429 | + * @param string $context View or edit context. |
|
430 | + * @return array |
|
431 | + */ |
|
432 | + public function get_refunded( $context = 'view' ) { |
|
433 | + return $this->get_prop( 'refunded', $context ); |
|
434 | + } |
|
334 | 435 | |
335 | - } |
|
436 | + /** |
|
437 | + * Get the total amount cancelled via this form. |
|
438 | + * |
|
439 | + * @since 1.0.19 |
|
440 | + * @param string $context View or edit context. |
|
441 | + * @return array |
|
442 | + */ |
|
443 | + public function get_cancelled( $context = 'view' ) { |
|
444 | + return $this->get_prop( 'cancelled', $context ); |
|
445 | + } |
|
336 | 446 | |
337 | - // $item_id => array( 'price' => 10 ) (item variations) |
|
338 | - if ( is_numeric( $key ) && is_array( $value ) ) { |
|
339 | - $item = new GetPaid_Form_Item( $key ); |
|
447 | + /** |
|
448 | + * Get the total amount failed via this form. |
|
449 | + * |
|
450 | + * @since 1.0.19 |
|
451 | + * @param string $context View or edit context. |
|
452 | + * @return array |
|
453 | + */ |
|
454 | + public function get_failed( $context = 'view' ) { |
|
455 | + return $this->get_prop( 'failed', $context ); |
|
456 | + } |
|
340 | 457 | |
341 | - if ( isset( $value['price'] ) && $item->user_can_set_their_price() ) { |
|
342 | - $item->set_price( $value['price'] ); |
|
343 | - } |
|
344 | - |
|
345 | - if ( $item->can_purchase() ) { |
|
346 | - $prepared[] = $item; |
|
347 | - } |
|
348 | - |
|
349 | - continue; |
|
350 | - } |
|
351 | - |
|
352 | - } |
|
353 | - |
|
354 | - if ( 'objects' == $return && 'view' == $context ) { |
|
355 | - return $prepared; |
|
356 | - } |
|
357 | - |
|
358 | - $items = array(); |
|
359 | - foreach ( $prepared as $item ) { |
|
360 | - $items[] = $item->prepare_data_for_use(); |
|
361 | - } |
|
362 | - |
|
363 | - return $items; |
|
364 | - } |
|
365 | - |
|
366 | - /** |
|
367 | - * Get a single item belonging to the form. |
|
368 | - * |
|
369 | - * @since 1.0.19 |
|
370 | - * @param int $item_id The item id to return. |
|
371 | - * @return GetPaid_Form_Item|bool |
|
372 | - */ |
|
373 | - public function get_item( $item_id ) { |
|
374 | - |
|
375 | - if ( empty( $item_id ) || ! is_numeric( $item_id ) ) { |
|
376 | - return false; |
|
377 | - } |
|
378 | - |
|
379 | - foreach( $this->get_items() as $item ) { |
|
380 | - if ( $item->get_id() == (int) $item_id ) { |
|
381 | - return $item; |
|
382 | - } |
|
383 | - } |
|
384 | - |
|
385 | - return false; |
|
386 | - |
|
387 | - } |
|
388 | - |
|
389 | - /** |
|
390 | - * Gets a single element. |
|
391 | - * |
|
392 | - * @since 1.0.19 |
|
393 | - * @param string $element_type The element type to return. |
|
394 | - * @return array|bool |
|
395 | - */ |
|
396 | - public function get_element_type( $element_type ) { |
|
397 | - |
|
398 | - if ( empty( $element_type ) || ! is_scalar( $element_type ) ) { |
|
399 | - return false; |
|
400 | - } |
|
401 | - |
|
402 | - foreach ( $this->get_prop( 'elements' ) as $element ) { |
|
403 | - |
|
404 | - if ( $element['type'] == $element_type ) { |
|
405 | - return $element; |
|
406 | - } |
|
407 | - |
|
408 | - } |
|
409 | - |
|
410 | - return false; |
|
411 | - |
|
412 | - } |
|
413 | - |
|
414 | - /** |
|
415 | - * Get the total amount earned via this form. |
|
416 | - * |
|
417 | - * @since 1.0.19 |
|
418 | - * @param string $context View or edit context. |
|
419 | - * @return array |
|
420 | - */ |
|
421 | - public function get_earned( $context = 'view' ) { |
|
422 | - return $this->get_prop( 'earned', $context ); |
|
423 | - } |
|
424 | - |
|
425 | - /** |
|
426 | - * Get the total amount refunded via this form. |
|
427 | - * |
|
428 | - * @since 1.0.19 |
|
429 | - * @param string $context View or edit context. |
|
430 | - * @return array |
|
431 | - */ |
|
432 | - public function get_refunded( $context = 'view' ) { |
|
433 | - return $this->get_prop( 'refunded', $context ); |
|
434 | - } |
|
435 | - |
|
436 | - /** |
|
437 | - * Get the total amount cancelled via this form. |
|
438 | - * |
|
439 | - * @since 1.0.19 |
|
440 | - * @param string $context View or edit context. |
|
441 | - * @return array |
|
442 | - */ |
|
443 | - public function get_cancelled( $context = 'view' ) { |
|
444 | - return $this->get_prop( 'cancelled', $context ); |
|
445 | - } |
|
446 | - |
|
447 | - /** |
|
448 | - * Get the total amount failed via this form. |
|
449 | - * |
|
450 | - * @since 1.0.19 |
|
451 | - * @param string $context View or edit context. |
|
452 | - * @return array |
|
453 | - */ |
|
454 | - public function get_failed( $context = 'view' ) { |
|
455 | - return $this->get_prop( 'failed', $context ); |
|
456 | - } |
|
457 | - |
|
458 | - /** |
|
459 | - * Get the currency. |
|
460 | - * |
|
461 | - * @since 1.0.19 |
|
462 | - * @param string $context View or edit context. |
|
463 | - * @return string |
|
464 | - */ |
|
465 | - public function get_currency() { |
|
466 | - $currency = empty( $this->invoice ) ? wpinv_get_currency() : $this->invoice->get_currency(); |
|
467 | - return apply_filters( 'getpaid-payment-form-currency', $currency, $this ); |
|
468 | - } |
|
458 | + /** |
|
459 | + * Get the currency. |
|
460 | + * |
|
461 | + * @since 1.0.19 |
|
462 | + * @param string $context View or edit context. |
|
463 | + * @return string |
|
464 | + */ |
|
465 | + public function get_currency() { |
|
466 | + $currency = empty( $this->invoice ) ? wpinv_get_currency() : $this->invoice->get_currency(); |
|
467 | + return apply_filters( 'getpaid-payment-form-currency', $currency, $this ); |
|
468 | + } |
|
469 | 469 | |
470 | 470 | /* |
471 | 471 | |-------------------------------------------------------------------------- |
@@ -478,22 +478,22 @@ discard block |
||
478 | 478 | */ |
479 | 479 | |
480 | 480 | /** |
481 | - * Set plugin version when the item was created. |
|
482 | - * |
|
483 | - * @since 1.0.19 |
|
484 | - */ |
|
485 | - public function set_version( $value ) { |
|
486 | - $this->set_prop( 'version', $value ); |
|
481 | + * Set plugin version when the item was created. |
|
482 | + * |
|
483 | + * @since 1.0.19 |
|
484 | + */ |
|
485 | + public function set_version( $value ) { |
|
486 | + $this->set_prop( 'version', $value ); |
|
487 | 487 | } |
488 | 488 | |
489 | 489 | /** |
490 | - * Set date when the item was created. |
|
491 | - * |
|
492 | - * @since 1.0.19 |
|
493 | - * @param string $value Value to set. |
|
490 | + * Set date when the item was created. |
|
491 | + * |
|
492 | + * @since 1.0.19 |
|
493 | + * @param string $value Value to set. |
|
494 | 494 | * @return bool Whether or not the date was set. |
495 | - */ |
|
496 | - public function set_date_created( $value ) { |
|
495 | + */ |
|
496 | + public function set_date_created( $value ) { |
|
497 | 497 | $date = strtotime( $value ); |
498 | 498 | |
499 | 499 | if ( $date ) { |
@@ -506,13 +506,13 @@ discard block |
||
506 | 506 | } |
507 | 507 | |
508 | 508 | /** |
509 | - * Set date when the item was last modified. |
|
510 | - * |
|
511 | - * @since 1.0.19 |
|
512 | - * @param string $value Value to set. |
|
509 | + * Set date when the item was last modified. |
|
510 | + * |
|
511 | + * @since 1.0.19 |
|
512 | + * @param string $value Value to set. |
|
513 | 513 | * @return bool Whether or not the date was set. |
514 | - */ |
|
515 | - public function set_date_modified( $value ) { |
|
514 | + */ |
|
515 | + public function set_date_modified( $value ) { |
|
516 | 516 | $date = strtotime( $value ); |
517 | 517 | |
518 | 518 | if ( $date ) { |
@@ -525,118 +525,118 @@ discard block |
||
525 | 525 | } |
526 | 526 | |
527 | 527 | /** |
528 | - * Set the item name. |
|
529 | - * |
|
530 | - * @since 1.0.19 |
|
531 | - * @param string $value New name. |
|
532 | - */ |
|
533 | - public function set_name( $value ) { |
|
534 | - $this->set_prop( 'name', sanitize_text_field( $value ) ); |
|
535 | - } |
|
536 | - |
|
537 | - /** |
|
538 | - * Alias of self::set_name(). |
|
539 | - * |
|
540 | - * @since 1.0.19 |
|
541 | - * @param string $value New name. |
|
542 | - */ |
|
543 | - public function set_title( $value ) { |
|
544 | - $this->set_name( $value ); |
|
545 | - } |
|
546 | - |
|
547 | - /** |
|
548 | - * Set the owner of the item. |
|
549 | - * |
|
550 | - * @since 1.0.19 |
|
551 | - * @param int $value New author. |
|
552 | - */ |
|
553 | - public function set_author( $value ) { |
|
554 | - $this->set_prop( 'author', (int) $value ); |
|
555 | - } |
|
556 | - |
|
557 | - /** |
|
558 | - * Set the form elements. |
|
559 | - * |
|
560 | - * @since 1.0.19 |
|
561 | - * @param array $value Form elements. |
|
562 | - */ |
|
563 | - public function set_elements( $value ) { |
|
564 | - if ( is_array( $value ) ) { |
|
565 | - $this->set_prop( 'elements', $value ); |
|
566 | - } |
|
567 | - } |
|
568 | - |
|
569 | - /** |
|
570 | - * Set the form items. |
|
571 | - * |
|
572 | - * @since 1.0.19 |
|
573 | - * @param array $value Form elements. |
|
574 | - */ |
|
575 | - public function set_items( $value ) { |
|
576 | - if ( is_array( $value ) ) { |
|
577 | - $this->set_prop( 'items', $value ); |
|
578 | - } |
|
579 | - } |
|
580 | - |
|
581 | - /** |
|
582 | - * Set the total amount earned via this form. |
|
583 | - * |
|
584 | - * @since 1.0.19 |
|
585 | - * @param float $value Amount earned. |
|
586 | - */ |
|
587 | - public function set_earned( $value ) { |
|
588 | - $value = max( (float) $value, 0 ); |
|
589 | - $this->set_prop( 'earned', $value ); |
|
590 | - } |
|
591 | - |
|
592 | - /** |
|
593 | - * Set the total amount refunded via this form. |
|
594 | - * |
|
595 | - * @since 1.0.19 |
|
596 | - * @param float $value Amount refunded. |
|
597 | - */ |
|
598 | - public function set_refunded( $value ) { |
|
599 | - $value = max( (float) $value, 0 ); |
|
600 | - $this->set_prop( 'refunded', $value ); |
|
601 | - } |
|
602 | - |
|
603 | - /** |
|
604 | - * Set the total amount cancelled via this form. |
|
605 | - * |
|
606 | - * @since 1.0.19 |
|
607 | - * @param float $value Amount cancelled. |
|
608 | - */ |
|
609 | - public function set_cancelled( $value ) { |
|
610 | - $value = max( (float) $value, 0 ); |
|
611 | - $this->set_prop( 'cancelled', $value ); |
|
612 | - } |
|
613 | - |
|
614 | - /** |
|
615 | - * Set the total amount failed via this form. |
|
616 | - * |
|
617 | - * @since 1.0.19 |
|
618 | - * @param float $value Amount cancelled. |
|
619 | - */ |
|
620 | - public function set_failed( $value ) { |
|
621 | - $value = max( (float) $value, 0 ); |
|
622 | - $this->set_prop( 'failed', $value ); |
|
623 | - } |
|
528 | + * Set the item name. |
|
529 | + * |
|
530 | + * @since 1.0.19 |
|
531 | + * @param string $value New name. |
|
532 | + */ |
|
533 | + public function set_name( $value ) { |
|
534 | + $this->set_prop( 'name', sanitize_text_field( $value ) ); |
|
535 | + } |
|
536 | + |
|
537 | + /** |
|
538 | + * Alias of self::set_name(). |
|
539 | + * |
|
540 | + * @since 1.0.19 |
|
541 | + * @param string $value New name. |
|
542 | + */ |
|
543 | + public function set_title( $value ) { |
|
544 | + $this->set_name( $value ); |
|
545 | + } |
|
546 | + |
|
547 | + /** |
|
548 | + * Set the owner of the item. |
|
549 | + * |
|
550 | + * @since 1.0.19 |
|
551 | + * @param int $value New author. |
|
552 | + */ |
|
553 | + public function set_author( $value ) { |
|
554 | + $this->set_prop( 'author', (int) $value ); |
|
555 | + } |
|
556 | + |
|
557 | + /** |
|
558 | + * Set the form elements. |
|
559 | + * |
|
560 | + * @since 1.0.19 |
|
561 | + * @param array $value Form elements. |
|
562 | + */ |
|
563 | + public function set_elements( $value ) { |
|
564 | + if ( is_array( $value ) ) { |
|
565 | + $this->set_prop( 'elements', $value ); |
|
566 | + } |
|
567 | + } |
|
568 | + |
|
569 | + /** |
|
570 | + * Set the form items. |
|
571 | + * |
|
572 | + * @since 1.0.19 |
|
573 | + * @param array $value Form elements. |
|
574 | + */ |
|
575 | + public function set_items( $value ) { |
|
576 | + if ( is_array( $value ) ) { |
|
577 | + $this->set_prop( 'items', $value ); |
|
578 | + } |
|
579 | + } |
|
580 | + |
|
581 | + /** |
|
582 | + * Set the total amount earned via this form. |
|
583 | + * |
|
584 | + * @since 1.0.19 |
|
585 | + * @param float $value Amount earned. |
|
586 | + */ |
|
587 | + public function set_earned( $value ) { |
|
588 | + $value = max( (float) $value, 0 ); |
|
589 | + $this->set_prop( 'earned', $value ); |
|
590 | + } |
|
591 | + |
|
592 | + /** |
|
593 | + * Set the total amount refunded via this form. |
|
594 | + * |
|
595 | + * @since 1.0.19 |
|
596 | + * @param float $value Amount refunded. |
|
597 | + */ |
|
598 | + public function set_refunded( $value ) { |
|
599 | + $value = max( (float) $value, 0 ); |
|
600 | + $this->set_prop( 'refunded', $value ); |
|
601 | + } |
|
602 | + |
|
603 | + /** |
|
604 | + * Set the total amount cancelled via this form. |
|
605 | + * |
|
606 | + * @since 1.0.19 |
|
607 | + * @param float $value Amount cancelled. |
|
608 | + */ |
|
609 | + public function set_cancelled( $value ) { |
|
610 | + $value = max( (float) $value, 0 ); |
|
611 | + $this->set_prop( 'cancelled', $value ); |
|
612 | + } |
|
613 | + |
|
614 | + /** |
|
615 | + * Set the total amount failed via this form. |
|
616 | + * |
|
617 | + * @since 1.0.19 |
|
618 | + * @param float $value Amount cancelled. |
|
619 | + */ |
|
620 | + public function set_failed( $value ) { |
|
621 | + $value = max( (float) $value, 0 ); |
|
622 | + $this->set_prop( 'failed', $value ); |
|
623 | + } |
|
624 | 624 | |
625 | 625 | /** |
626 | 626 | * Create an item. For backwards compatibilty. |
627 | 627 | * |
628 | 628 | * @deprecated |
629 | - * @return int item id |
|
629 | + * @return int item id |
|
630 | 630 | */ |
631 | 631 | public function create( $data = array() ) { |
632 | 632 | |
633 | - // Set the properties. |
|
634 | - if ( is_array( $data ) ) { |
|
635 | - $this->set_props( $data ); |
|
636 | - } |
|
633 | + // Set the properties. |
|
634 | + if ( is_array( $data ) ) { |
|
635 | + $this->set_props( $data ); |
|
636 | + } |
|
637 | 637 | |
638 | - // Save the item. |
|
639 | - return $this->save(); |
|
638 | + // Save the item. |
|
639 | + return $this->save(); |
|
640 | 640 | |
641 | 641 | } |
642 | 642 | |
@@ -644,7 +644,7 @@ discard block |
||
644 | 644 | * Updates an item. For backwards compatibilty. |
645 | 645 | * |
646 | 646 | * @deprecated |
647 | - * @return int item id |
|
647 | + * @return int item id |
|
648 | 648 | */ |
649 | 649 | public function update( $data = array() ) { |
650 | 650 | return $this->create( $data ); |
@@ -660,22 +660,22 @@ discard block |
||
660 | 660 | */ |
661 | 661 | |
662 | 662 | /** |
663 | - * Checks whether this is the default payment form. |
|
664 | - * |
|
665 | - * @since 1.0.19 |
|
666 | - * @return bool |
|
667 | - */ |
|
663 | + * Checks whether this is the default payment form. |
|
664 | + * |
|
665 | + * @since 1.0.19 |
|
666 | + * @return bool |
|
667 | + */ |
|
668 | 668 | public function is_default() { |
669 | 669 | $is_default = $this->get_id() == wpinv_get_default_payment_form(); |
670 | 670 | return (bool) apply_filters( 'wpinv_is_default_payment_form', $is_default, $this->get_id(), $this ); |
671 | - } |
|
671 | + } |
|
672 | 672 | |
673 | 673 | /** |
674 | - * Checks whether the form is active. |
|
675 | - * |
|
676 | - * @since 1.0.19 |
|
677 | - * @return bool |
|
678 | - */ |
|
674 | + * Checks whether the form is active. |
|
675 | + * |
|
676 | + * @since 1.0.19 |
|
677 | + * @return bool |
|
678 | + */ |
|
679 | 679 | public function is_active() { |
680 | 680 | $is_active = 0 !== (int) $this->get_id(); |
681 | 681 | |
@@ -684,76 +684,76 @@ discard block |
||
684 | 684 | } |
685 | 685 | |
686 | 686 | return (bool) apply_filters( 'wpinv_is_payment_form_active', $is_active, $this ); |
687 | - } |
|
688 | - |
|
689 | - /** |
|
690 | - * Checks whether the form has a given item. |
|
691 | - * |
|
692 | - * @since 1.0.19 |
|
693 | - * @return bool |
|
694 | - */ |
|
687 | + } |
|
688 | + |
|
689 | + /** |
|
690 | + * Checks whether the form has a given item. |
|
691 | + * |
|
692 | + * @since 1.0.19 |
|
693 | + * @return bool |
|
694 | + */ |
|
695 | 695 | public function has_item( $item_id ) { |
696 | 696 | return false !== $this->get_item( $item_id ); |
697 | - } |
|
698 | - |
|
699 | - /** |
|
700 | - * Checks whether the form has a given element. |
|
701 | - * |
|
702 | - * @since 1.0.19 |
|
703 | - * @return bool |
|
704 | - */ |
|
697 | + } |
|
698 | + |
|
699 | + /** |
|
700 | + * Checks whether the form has a given element. |
|
701 | + * |
|
702 | + * @since 1.0.19 |
|
703 | + * @return bool |
|
704 | + */ |
|
705 | 705 | public function has_element_type( $element_type ) { |
706 | 706 | return false !== $this->get_element_type( $element_type ); |
707 | - } |
|
708 | - |
|
709 | - /** |
|
710 | - * Checks whether this form is recurring or not. |
|
711 | - * |
|
712 | - * @since 1.0.19 |
|
713 | - * @return bool |
|
714 | - */ |
|
707 | + } |
|
708 | + |
|
709 | + /** |
|
710 | + * Checks whether this form is recurring or not. |
|
711 | + * |
|
712 | + * @since 1.0.19 |
|
713 | + * @return bool |
|
714 | + */ |
|
715 | 715 | public function is_recurring() { |
716 | 716 | |
717 | - if ( ! empty( $this->invoice ) ) { |
|
718 | - return $this->invoice->is_recurring(); |
|
719 | - } |
|
717 | + if ( ! empty( $this->invoice ) ) { |
|
718 | + return $this->invoice->is_recurring(); |
|
719 | + } |
|
720 | 720 | |
721 | - foreach ( $this->get_items() as $item ) { |
|
721 | + foreach ( $this->get_items() as $item ) { |
|
722 | 722 | |
723 | - if ( $item->is_recurring() ) { |
|
724 | - return true; |
|
725 | - } |
|
723 | + if ( $item->is_recurring() ) { |
|
724 | + return true; |
|
725 | + } |
|
726 | 726 | |
727 | - } |
|
727 | + } |
|
728 | 728 | |
729 | 729 | return false; |
730 | - } |
|
730 | + } |
|
731 | 731 | |
732 | - /** |
|
733 | - * Retrieves the form's html. |
|
734 | - * |
|
735 | - * @since 1.0.19 |
|
736 | - */ |
|
732 | + /** |
|
733 | + * Retrieves the form's html. |
|
734 | + * |
|
735 | + * @since 1.0.19 |
|
736 | + */ |
|
737 | 737 | public function get_html( $extra_markup = '' ) { |
738 | 738 | |
739 | - // Return the HTML. |
|
740 | - return wpinv_get_template_html( |
|
741 | - 'payment-forms/form.php', |
|
742 | - array( |
|
743 | - 'form' => $this, |
|
744 | - 'extra_markup' => $extra_markup, |
|
745 | - ) |
|
746 | - ); |
|
747 | - |
|
748 | - } |
|
749 | - |
|
750 | - /** |
|
751 | - * Displays the payment form. |
|
752 | - * |
|
753 | - * @since 1.0.19 |
|
754 | - */ |
|
739 | + // Return the HTML. |
|
740 | + return wpinv_get_template_html( |
|
741 | + 'payment-forms/form.php', |
|
742 | + array( |
|
743 | + 'form' => $this, |
|
744 | + 'extra_markup' => $extra_markup, |
|
745 | + ) |
|
746 | + ); |
|
747 | + |
|
748 | + } |
|
749 | + |
|
750 | + /** |
|
751 | + * Displays the payment form. |
|
752 | + * |
|
753 | + * @since 1.0.19 |
|
754 | + */ |
|
755 | 755 | public function display( $extra_markup = '' ) { |
756 | - echo $this->get_html( $extra_markup ); |
|
756 | + echo $this->get_html( $extra_markup ); |
|
757 | 757 | } |
758 | 758 | |
759 | 759 | } |
@@ -12,79 +12,79 @@ |
||
12 | 12 | */ |
13 | 13 | class GetPaid_Payment_Form_Submission_Items { |
14 | 14 | |
15 | - /** |
|
16 | - * Submission items. |
|
17 | - * @var GetPaid_Form_Item[] |
|
18 | - */ |
|
19 | - public $items = array(); |
|
20 | - |
|
21 | 15 | /** |
22 | - * Class constructor |
|
23 | - * |
|
24 | - * @param GetPaid_Payment_Form_Submission $submission |
|
25 | - */ |
|
26 | - public function __construct( $submission ) { |
|
27 | - |
|
28 | - $data = $submission->get_data(); |
|
29 | - $payment_form = $submission->get_payment_form(); |
|
30 | - |
|
31 | - // Prepare the selected items. |
|
32 | - $selected_items = array(); |
|
33 | - if ( ! empty( $data['getpaid-items'] ) ) { |
|
34 | - $selected_items = wpinv_clean( $data['getpaid-items'] ); |
|
35 | - } |
|
36 | - |
|
37 | - // For default forms, ensure that an item has been set. |
|
38 | - if ( $payment_form->is_default() && ! $submission->has_invoice() && isset( $data['getpaid-form-items'] ) ) { |
|
39 | - $form_items = wpinv_clean( $data['getpaid-form-items'] ); |
|
40 | - $payment_form->set_items( getpaid_convert_items_to_array( $form_items ) ); |
|
41 | - } |
|
16 | + * Submission items. |
|
17 | + * @var GetPaid_Form_Item[] |
|
18 | + */ |
|
19 | + public $items = array(); |
|
42 | 20 | |
43 | - // Process each individual item. |
|
44 | - foreach ( $payment_form->get_items() as $item ) { |
|
45 | - $this->process_item( $item, $selected_items ); |
|
46 | - } |
|
47 | - |
|
48 | - } |
|
21 | + /** |
|
22 | + * Class constructor |
|
23 | + * |
|
24 | + * @param GetPaid_Payment_Form_Submission $submission |
|
25 | + */ |
|
26 | + public function __construct( $submission ) { |
|
27 | + |
|
28 | + $data = $submission->get_data(); |
|
29 | + $payment_form = $submission->get_payment_form(); |
|
30 | + |
|
31 | + // Prepare the selected items. |
|
32 | + $selected_items = array(); |
|
33 | + if ( ! empty( $data['getpaid-items'] ) ) { |
|
34 | + $selected_items = wpinv_clean( $data['getpaid-items'] ); |
|
35 | + } |
|
36 | + |
|
37 | + // For default forms, ensure that an item has been set. |
|
38 | + if ( $payment_form->is_default() && ! $submission->has_invoice() && isset( $data['getpaid-form-items'] ) ) { |
|
39 | + $form_items = wpinv_clean( $data['getpaid-form-items'] ); |
|
40 | + $payment_form->set_items( getpaid_convert_items_to_array( $form_items ) ); |
|
41 | + } |
|
42 | + |
|
43 | + // Process each individual item. |
|
44 | + foreach ( $payment_form->get_items() as $item ) { |
|
45 | + $this->process_item( $item, $selected_items ); |
|
46 | + } |
|
47 | + |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * Process a single item. |
|
52 | - * |
|
53 | - * @param GetPaid_Form_Item $item |
|
54 | - * @param array $selected_items |
|
55 | - */ |
|
56 | - public function process_item( $item, $selected_items ) { |
|
50 | + /** |
|
51 | + * Process a single item. |
|
52 | + * |
|
53 | + * @param GetPaid_Form_Item $item |
|
54 | + * @param array $selected_items |
|
55 | + */ |
|
56 | + public function process_item( $item, $selected_items ) { |
|
57 | 57 | |
58 | - // Abort if this is an optional item and it has not been selected. |
|
59 | - if ( ! $item->is_required() && ! isset( $selected_items[ $item->get_id() ] ) ) { |
|
60 | - return; |
|
61 | - } |
|
58 | + // Abort if this is an optional item and it has not been selected. |
|
59 | + if ( ! $item->is_required() && ! isset( $selected_items[ $item->get_id() ] ) ) { |
|
60 | + return; |
|
61 | + } |
|
62 | 62 | |
63 | - // (maybe) let customers change the quantities and prices. |
|
64 | - if ( isset( $selected_items[ $item->get_id() ] ) ) { |
|
63 | + // (maybe) let customers change the quantities and prices. |
|
64 | + if ( isset( $selected_items[ $item->get_id() ] ) ) { |
|
65 | 65 | |
66 | - // Maybe change the quantities. |
|
67 | - if ( $item->allows_quantities() ) { |
|
68 | - $item->set_quantity( (int) $selected_items[ $item->get_id() ]['quantity'] ); |
|
69 | - } |
|
66 | + // Maybe change the quantities. |
|
67 | + if ( $item->allows_quantities() ) { |
|
68 | + $item->set_quantity( (int) $selected_items[ $item->get_id() ]['quantity'] ); |
|
69 | + } |
|
70 | 70 | |
71 | - // Maybe change the price. |
|
72 | - if ( $item->user_can_set_their_price() ) { |
|
73 | - $price = (float) wpinv_sanitize_amount( $selected_items[ $item->get_id() ]['price'] ); |
|
71 | + // Maybe change the price. |
|
72 | + if ( $item->user_can_set_their_price() ) { |
|
73 | + $price = (float) wpinv_sanitize_amount( $selected_items[ $item->get_id() ]['price'] ); |
|
74 | 74 | |
75 | - if ( $item->get_minimum_price() > $price ) { |
|
76 | - throw new Exception( sprintf( __( 'The minimum allowed amount is %s', 'invoicing' ), wpinv_sanitize_amount( $item->get_minimum_price() ) ) ); |
|
77 | - } |
|
75 | + if ( $item->get_minimum_price() > $price ) { |
|
76 | + throw new Exception( sprintf( __( 'The minimum allowed amount is %s', 'invoicing' ), wpinv_sanitize_amount( $item->get_minimum_price() ) ) ); |
|
77 | + } |
|
78 | 78 | |
79 | - $item->set_price( $price ); |
|
79 | + $item->set_price( $price ); |
|
80 | 80 | |
81 | - } |
|
81 | + } |
|
82 | 82 | |
83 | - } |
|
83 | + } |
|
84 | 84 | |
85 | - // Save the item. |
|
86 | - $this->items[] = $item; |
|
85 | + // Save the item. |
|
86 | + $this->items[] = $item; |
|
87 | 87 | |
88 | - } |
|
88 | + } |
|
89 | 89 | |
90 | 90 | } |