PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / 1.1.0
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster v1.1.0
2.7.0 2.6.0 trunk 1.1.0 1.1.4 1.2.1 1.2.2 1.2.3 1.2.5 1.2.9 1.3.1 1.3.2 1.5.0 1.5.1 1.5.4 1.5.7 1.5.8 1.5.9 1.6.2 1.6.5 1.6.8 1.7.2 1.7.4 1.7.5 1.7.9 All 43 releases
sellkit / includes / admin / class-steps.php

class-steps.php in SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster 1.1.0, at includes/admin/class-steps.php

396 lines 10.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 defined( 'ABSPATH' ) || die();
4
5 /**
6 * Steps class.
7 *
8 * @since 1.1.0
9 */
10 class Sellkit_Admin_Steps {
11
12 /**
13 * Class instance.
14 *
15 * @since 1.1.0
16 * @var Sellkit_Admin_Steps
17 */
18 private static $instance = null;
19
20 /**
21 * Class instance.
22 *
23 * @since 1.1.0
24 * @var string
25 */
26 const SELLKIT_STEP_POST_TYPE = 'sellkit_step';
27
28 /**
29 * Get a class instance.
30 *
31 * @since 1.1.0
32 *
33 * @return Sellkit_Admin_Steps Class instance.
34 */
35 public static function get_instance() {
36 if ( null === self::$instance ) {
37 self::$instance = new self();
38 }
39
40 return self::$instance;
41 }
42
43 /**
44 * Class constructor.
45 *
46 * @since 1.1.0
47 */
48 public function __construct() {
49 $this->register_step_post_type();
50
51 add_action( 'wp_ajax_sellkit_steps_get_products', [ $this, 'get_products' ] );
52 add_action( 'wp_ajax_sellkit_steps_get_products_data', [ $this, 'get_products_data' ] );
53 add_action( 'sellkit_funnels_after_create_new_step', [ $this, 'after_create_step_callback' ], 10 );
54 add_action( 'sellkit_funnels_after_edit_step_name_slug', [ $this, 'edit_step_name_and_slug' ], 10 );
55 add_action( 'sellkit_funnels_after_delete_step_post', [ $this, 'delete_related_page' ], 10 );
56 add_action( 'sellkit_funnels_update_steps', [ $this, 'save_steps_data' ], 10, 2 );
57 add_filter( 'template_include', [ $this, 'load_page_template' ], 10 );
58 add_action( 'wp_ajax_sellkit_steps_get_coupons', [ $this, 'get_coupons' ] );
59 add_action( 'wp_loaded', [ $this, 'sellkit_steps_flush_rules' ] );
60 add_action( 'sellkit_after_toggle_status_sellkit-funnels', [ $this, 'update_steps_post_status' ], 10, 2 );
61 }
62
63 /**
64 * Get default funnels.
65 *
66 * @since 1.1.0
67 */
68 public static function get_default_funnel_steps() {
69 return [
70 'sales-page' => esc_html__( 'Sales Page', 'sellkit' ),
71 'checkout' => esc_html__( 'Checkout', 'sellkit' ),
72 'thankyou' => esc_html__( 'Thank You', 'sellkit' ),
73 'upsell' => esc_html__( 'Upsell', 'sellkit' ),
74 ];
75 }
76
77 /**
78 * Save_steps_data.
79 *
80 * @since 1.1.0
81 * @param array $steps Steps.
82 * @param int $funnel_id Funnel Id.
83 */
84 public static function save_steps_data( $steps, $funnel_id ) {
85 foreach ( $steps as $step_number => $step ) {
86 $step['funnel_id'] = $funnel_id;
87 $step['number'] = $step_number;
88
89 update_post_meta( $step['page_id'], 'step_data', $step );
90 }
91 }
92
93 /**
94 * Get all coupons.
95 *
96 * @since 1.1.0
97 */
98 public static function get_coupons() {
99 check_ajax_referer( 'sellkit', 'nonce' );
100
101 $input_value = filter_input( INPUT_GET, 'input_value', FILTER_SANITIZE_STRING );
102
103 $filtered_products = [];
104 $args = [
105 'post_type' => 'shop_coupon',
106 'post_status' => 'any',
107 's' => $input_value,
108 ];
109
110 $query = new WP_Query( $args );
111
112 if ( $query->have_posts() ) {
113 while ( $query->have_posts() ) {
114 $query->the_post();
115 $filtered_products[] = [
116 'label' => htmlspecialchars_decode( get_the_title() ),
117 'value' => get_the_ID(),
118 ];
119 }
120 }
121
122 wp_send_json_success( $filtered_products );
123 }
124
125 /**
126 * Get products.
127 *
128 * @since 1.1.0
129 */
130 public static function get_products() {
131 check_ajax_referer( 'sellkit', 'nonce' );
132
133 $input_value = filter_input( INPUT_GET, 'input_value', FILTER_SANITIZE_STRING );
134
135 $filtered_products = [];
136
137 $args = [
138 'status' => 'any',
139 'orderby' => 'name',
140 'order' => 'ASC',
141 's' => $input_value,
142 'posts_per_page' => -1,
143 ];
144
145 $all_products = wc_get_products( $args );
146
147 foreach ( $all_products as $product ) {
148 if ( $product->get_type() !== 'variable' ) {
149 $filtered_products[] = [
150 'label' => $product->get_title(),
151 'value' => $product->get_id(),
152 ];
153 }
154
155 if ( $product->get_type() === 'variable' ) {
156 foreach ( $product->get_available_variations() as $available_variation ) {
157 $attributes = $available_variation['attributes'];
158 $attributes = implode( ', ', array_values( $attributes ) );
159
160 $filtered_products[] = [
161 'label' => "{$product->get_title()} - {$attributes} (#{$available_variation['variation_id']})",
162 'value' => $available_variation['variation_id'],
163 ];
164 }
165 }
166 }
167
168 wp_send_json_success( $filtered_products );
169 }
170
171 /**
172 * Get products.
173 *
174 * @since 1.1.0
175 */
176 public static function get_products_data() {
177 check_ajax_referer( 'sellkit', 'nonce' );
178
179 $filtered_products = [];
180 $products_id = filter_input( INPUT_GET, 'products_id', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
181
182 if ( empty( $products_id ) ) {
183 wp_send_json_error( __( 'Please send some product ids' ) );
184 }
185
186 $products_id = is_array( $products_id ) ? $products_id : [ $products_id ];
187
188 foreach ( $products_id as $product_id ) {
189 $product = wc_get_product( $product_id );
190 $attributes = $product->get_attributes();
191 $label = $product->get_title();
192
193 if ( $product->get_type() === 'variation' ) {
194 $attributes = implode( ', ', array_values( $attributes ) );
195 $label = "{$product->get_title()} - $attributes (#{$product->get_id()})";
196 }
197
198 $filtered_products[] = [
199 'label' => $label,
200 'id' => $product->get_id(),
201 'title' => $label,
202 'thumbnail' => $product->get_image(),
203 'regular_price' => $product->get_regular_price( 'edit' ),
204 ];
205 }
206
207 wp_send_json_success( $filtered_products );
208 }
209
210 /**
211 * Filter step data before saving.
212 *
213 * @since 1.1.0
214 * @param string $post_id Post id.
215 */
216 public function after_create_step_callback( $post_id ) {
217 $steps = get_post_meta( $post_id, 'sellkit_steps', true );
218 $last_step = end( $steps );
219
220 array_pop( $steps );
221
222 $new_step_id = wp_insert_post( [
223 'post_type' => self::SELLKIT_STEP_POST_TYPE,
224 'post_title' => $last_step['title'],
225 'post_name' => sanitize_title( $last_step['title'] ),
226 'post_status' => 'publish',
227 ] );
228
229 $last_step['page_id'] = $new_step_id;
230 $last_step['slug'] = get_post_field( 'post_name', $new_step_id );
231
232 $step_data = $last_step;
233 $step_data['funnel_id'] = $post_id;
234 $step_data['number'] = count( $steps );
235
236 update_post_meta( $new_step_id, 'step_data', $step_data );
237
238 $new_steps = array_merge( $steps, [ $last_step ] );
239
240 update_post_meta( $post_id, 'sellkit_steps', $new_steps );
241 }
242
243 /**
244 * Loading page templates.
245 *
246 * @since 1.1.0
247 * @param string $template Template name.
248 */
249 public function load_page_template( $template ) {
250
251 global $post;
252
253 if ( 'string' !== gettype( $template ) || ! is_object( $post ) || self::SELLKIT_STEP_POST_TYPE !== $post->post_type ) {
254 return $template;
255 }
256
257 remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head' );
258 add_filter( 'next_post_rel_link', '__return_empty_string' );
259 add_filter( 'previous_post_rel_link', '__return_empty_string' );
260
261 $file = sellkit()->plugin_dir() . 'includes/templates/canvas.php';
262
263 if ( ! file_exists( $template ) ) {
264 return $template;
265 }
266
267 return $file;
268 }
269
270 /**
271 * Register step post type.
272 *
273 * @since 1.1.0
274 */
275 public function register_step_post_type() {
276 $labels = array(
277 'name' => esc_html_x( 'Steps', 'flow step general name', 'sellkit' ),
278 'singular_name' => esc_html_x( 'Step', 'flow step singular name', 'sellkit' ),
279 'search_items' => esc_html__( 'Search Steps', 'sellkit' ),
280 'all_items' => esc_html__( 'All Steps', 'sellkit' ),
281 'edit_item' => esc_html__( 'Edit Step', 'sellkit' ),
282 'view_item' => esc_html__( 'View Step', 'sellkit' ),
283 'add_new' => esc_html__( 'Add New', 'sellkit' ),
284 'update_item' => esc_html__( 'Update Step', 'sellkit' ),
285 'add_new_item' => esc_html__( 'Add New', 'sellkit' ),
286 'new_item_name' => esc_html__( 'New Step Name', 'sellkit' ),
287 );
288
289 $args = array(
290 'labels' => $labels,
291 'public' => true,
292 'query_var' => true,
293 'can_export' => true,
294 'exclude_from_search' => true,
295 'show_ui' => true,
296 'show_in_menu' => false,
297 'show_in_admin_bar' => true,
298 'show_in_rest' => true,
299 'publicly_queryable' => true,
300 'supports' => [ 'title', 'editor', 'elementor', 'revisions' ],
301 'capability_type' => 'post',
302 'capabilities' => [
303 'create_posts' => 'do_not_allow', // Prior to Wordpress 4.5, this was false.
304 ],
305 'map_meta_cap' => true,
306 );
307
308 register_post_type( self::SELLKIT_STEP_POST_TYPE, $args );
309 }
310
311 /**
312 * Edit step name or slug.
313 *
314 * @since 1.1.0
315 * @param string $funnel_id Funnel Id.
316 */
317 public function edit_step_name_and_slug( $funnel_id ) {
318 $sub_action_data = sellkit_post( 'sub_action_data' );
319 $title = ! empty( $sub_action_data['title'] ) ? $sub_action_data['title'] : '';
320 $slug = ! empty( $sub_action_data['slug'] ) ? $sub_action_data['slug'] : sanitize_title( $title );
321 $page_id = ! empty( $sub_action_data['page_id'] ) ? $sub_action_data['page_id'] : '';
322 $selected_step = $sub_action_data['selected_step'];
323
324 if ( empty( $page_id ) ) {
325 return;
326 }
327
328 wp_update_post( [
329 'ID' => $page_id,
330 'post_title' => $title,
331 'post_name' => $slug,
332 ] );
333
334 if ( get_post_field( 'post_name', $page_id ) === $slug ) {
335 return;
336 }
337
338 $post_data = get_post_meta( $funnel_id, 'sellkit_steps', true );
339 $post_data[ $selected_step ]['slug'] = get_post_field( 'post_name', $page_id );
340
341 update_post_meta( $funnel_id, 'sellkit_steps', $post_data );
342 }
343
344 /**
345 * Delete step page id.
346 *
347 * @since 1.1.0
348 */
349 public function delete_related_page() {
350 $sub_action_data = sellkit_post( 'sub_action_data' );
351
352 if ( empty( $sub_action_data['page_id'] ) ) {
353 return;
354 }
355
356 wp_delete_post( $sub_action_data['page_id'], true );
357 }
358
359 /**
360 * Flash rules.
361 *
362 * @since 1.1.0
363 */
364 public function sellkit_steps_flush_rules() {
365 $rules = get_option( 'rewrite_rules' );
366
367 if ( ! isset( $rules['sellkit_step/[^/]+/([^/]+)/?$'] ) ) {
368 flush_rewrite_rules();
369 }
370 }
371
372 /**
373 * Make publish steps page.
374 *
375 * @since 1.1.0
376 * @param string $funnel_id Funnel Id.
377 * @param string $new_status Status.
378 */
379 public function update_steps_post_status( $funnel_id, $new_status ) {
380 $steps = get_post_meta( $funnel_id, 'sellkit_steps', true );
381
382 foreach ( $steps as $step ) {
383 if ( $step['page_id'] === $funnel_id ) {
384 continue;
385 }
386
387 wp_update_post(array(
388 'ID' => $step['page_id'],
389 'post_status' => $new_status,
390 ) );
391 }
392 }
393 }
394
395 Sellkit_Admin_Steps::get_instance();
396