PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / 1.2.1
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster v1.2.1
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 1.8.1 All 42 releases
sellkit / includes / admin / class-posts.php

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

265 lines 6.2 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 * Forms class.
7 *
8 * @since 1.1.0
9 */
10 class Sellkit_Admin_Posts {
11
12 /**
13 * Class instance.
14 *
15 * @since 1.1.0
16 * @var Sellkit_Admin_Posts
17 */
18 private static $instance = null;
19
20 /**
21 * Sub Action.
22 *
23 * @since 1.1.0
24 * @var string
25 */
26 public $sub_action = '';
27
28 /**
29 * Get a class instance.
30 *
31 * @since 1.1.0
32 *
33 * @return Sellkit_Admin_Posts 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 add_action( 'wp_ajax_sellkit_post_save', [ $this, 'save_post' ] );
50 add_action( 'wp_ajax_sellkit_post_get', [ $this, 'get_post' ] );
51 add_action( 'wp_ajax_sellkit_post_remove', [ $this, 'remove_post' ] );
52 }
53
54 /**
55 * Remove post.
56 *
57 * @since 1.1.0
58 */
59 public function remove_post() {
60 check_ajax_referer( 'sellkit', 'nonce' );
61
62 $post_id = sellkit_htmlspecialchars( INPUT_POST, 'post_id' );
63
64 $result = wp_delete_post( $post_id );
65
66 if ( empty( $result ) ) {
67 wp_send_json_error( __( 'something went wrong', 'sellkit' ) );
68 }
69
70 wp_send_json_success( __( 'The process has been completed.', 'sellkit' ) );
71 }
72
73 /**
74 * Get post data.
75 *
76 * @since 1.1.0
77 */
78 public function get_post() {
79 check_ajax_referer( 'sellkit', 'nonce' );
80
81 $post_id = sellkit_htmlspecialchars( INPUT_GET, 'post_id' );
82
83 $post_data = get_post_meta( $post_id );
84
85 // Get post meta (without key) returns array by default, Now converts them to singles.
86 $post_data = array_map( function( $meta ) {
87 return $meta[0];
88 }, $post_data );
89
90 if ( ! empty( $post_data['conditions'] ) ) {
91 $post_data['conditions'] = unserialize( $post_data['conditions'] ); // phpcs:ignore WordPress.PHP
92 }
93
94 if ( ! empty( $post_data['filters'] ) ) {
95 $post_data['filters'] = unserialize( $post_data['filters'] ); // phpcs:ignore WordPress.PHP
96 }
97
98 if ( ! empty( $post_data['sellkit_steps'] ) ) {
99 $post_data['sellkit_steps'] = unserialize( $post_data['sellkit_steps'] ); // phpcs:ignore WordPress.PHP
100 }
101
102 $post_data['sellkit_post_title'] = html_entity_decode( get_the_title( $post_id ) );
103 $post_data['sellkit_post_slug'] = get_post_field( 'post_name', $post_id );
104 $post_data['sellkit_post_status'] = get_post_status( $post_id );
105
106 wp_send_json_success( [ 'post_data' => $post_data ] );
107 }
108
109 /**
110 * Save post.
111 *
112 * @since 1.1.0
113 */
114 public function save_post() {
115 check_ajax_referer( 'sellkit', 'nonce' );
116
117 $fields = sellkit_post( 'fields' );
118 $post_id = sellkit_htmlspecialchars( INPUT_POST, 'post_id' );
119 $post_type = sellkit_htmlspecialchars( INPUT_POST, 'post_type' );
120 $this->sub_action = sellkit_htmlspecialchars( INPUT_POST, 'sub_action' );
121
122 $sanitized_fields = $this->sanitize_fields( $fields );
123
124 $this->validate_fields( $sanitized_fields );
125
126 $target_post_id = $this->handle_save_post( $sanitized_fields, $post_type, $post_id );
127
128 if ( empty( $target_post_id ) ) {
129 wp_send_json_error( [ __( 'something went wrong', 'sellkit' ) ] );
130 }
131
132 if ( $post_id ) {
133 wp_send_json_success( [
134 'post_id' => $post_id,
135 'message' => __( 'The update process has been completed.', 'sellkit' ),
136 ] );
137 }
138
139 wp_send_json_success( [
140 'post_id' => $target_post_id,
141 'message' => __( 'The creation process has been completed.', 'sellkit' ),
142 ] );
143 }
144
145 /**
146 * Update or add new post.
147 *
148 * @param array $data All data.
149 * @param string $post_type The post type.
150 * @param bool $post_id The post Id.
151 * @return bool|integer
152 *
153 * @since 1.1.0
154 * @SuppressWarnings(PHPMD.NPathComplexity)
155 */
156 private function handle_save_post( $data, $post_type, $post_id = false ) {
157 $title = sanitize_text_field( $data['sellkit_post_title'] );
158 $slug = sanitize_text_field( $data['sellkit_post_slug'] );
159 $post_status = 'publish' === $data['sellkit_post_status'] ? 'publish' : 'draft';
160
161 $post_args = [
162 'post_title' => $title,
163 'post_name' => $slug ?: sanitize_title( $title ),
164 'post_type' => $post_type,
165 'post_status' => $post_status,
166 ];
167
168 if ( ! $post_id ) {
169 $new_post_id = wp_insert_post( $post_args );
170 }
171
172 if ( $post_id ) {
173 $post_args = array_merge( $post_args, [ 'ID' => $post_id ] );
174 $new_post_id = wp_update_post( $post_args );
175
176 do_action( "sellkit_posts_after_saving_$post_type", $post_id );
177 }
178
179 if ( empty( $new_post_id ) || is_wp_error( $new_post_id ) ) {
180 return false;
181 }
182
183 foreach ( $data as $name => $value ) {
184 if ( 'sellkit_post_title' === $name || 'sellkit_post_status' === $name ) {
185 continue;
186 }
187
188 update_post_meta( $new_post_id, $name, $value );
189
190 if ( 'sellkit_steps' === $name ) {
191 do_action( 'sellkit_funnels_update_steps', $value, $new_post_id );
192 }
193 }
194
195 if ( empty( $data['conditions'] ) ) {
196 delete_post_meta( $new_post_id, 'conditions' );
197 }
198
199 do_action( "sellkit_funnels_after_$this->sub_action", $post_id );
200
201 if ( empty( $data['sellkit_steps'] ) ) {
202 delete_post_meta( $new_post_id, 'sellkit_steps' );
203 }
204
205 return $new_post_id;
206 }
207
208 /**
209 * Sanitize fields.
210 *
211 * @param array $fields Fields.
212 *
213 * @return array
214 *
215 * @since 1.1.0
216 */
217 private function sanitize_fields( $fields ) {
218 $new_fields = [];
219
220 foreach ( $fields as $key => $field ) {
221 if ( is_array( $fields[ $key ] ) ) {
222 $new_fields[ $key ] = $this->sanitize_fields( $fields[ $key ] );
223 }
224
225 $field_value = sanitize_meta( $key, $field, 'post' );
226
227 if ( ! is_array( $fields[ $key ] ) ) {
228 $new_fields[ $key ] = $field_value;
229 }
230 }
231
232 if ( ! empty( $new_fields['conditions'] ) ) {
233 $new_fields['conditions'] = sellkit_condition_row_sanitization( $new_fields['conditions'] );
234 }
235
236 if ( ! empty( $new_fields['filters'] ) ) {
237 $new_fields['filters'] = sellkit_filter_row_sanitization( $new_fields['filters'] );
238 }
239
240 return $new_fields;
241 }
242
243 /**
244 * Validate fields.
245 *
246 * @param array $fields Fields.
247 *
248 * @since 1.1.0
249 */
250 public function validate_fields( $fields ) {
251 $errors = [];
252 if ( empty( $fields['sellkit_post_title'] ) ) {
253 $errors[] = __( 'Post title should not be empty.', 'sellkit' );
254 }
255
256 if ( empty( $errors ) ) {
257 return;
258 }
259
260 wp_send_json_error( $errors );
261 }
262 }
263
264 Sellkit_Admin_Posts::get_instance();
265