PluginProbe
Cooked – Recipe Management / 1.9.5
Cooked – Recipe Management v1.9.5
1.16.1 1.16.0 1.15.0 trunk 1.10.0 1.11.0 1.11.1 1.11.2 1.11.3 1.11.4 1.12.0 1.13.0 1.14.0 1.7.10 1.7.11 1.7.12 1.7.13 1.7.15.1 1.7.15.3 1.7.15.4 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 All 37 releases
cooked / includes / class.cooked-ajax.php

class.cooked-ajax.php in Cooked – Recipe Management 1.9.5, at includes/class.cooked-ajax.php

362 lines 11.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Cooked AJAX-Specific Functions
4 *
5 * @package Cooked
6 * @subpackage AJAX-Specific Functions
7 * @since 1.0.0
8 */
9
10 // Exit if accessed directly
11 if ( ! defined( 'ABSPATH' ) ) exit;
12
13 /**
14 * Cooked_Ajax Class
15 *
16 * This class handles the Cooked Recipe Meta Box creation.
17 *
18 * @since 1.0.0
19 */
20 class Cooked_Ajax {
21
22 function __construct() {
23 /**
24 * Back-End Ajax
25 */
26
27 // Save Default Template
28 add_action( 'wp_ajax_cooked_save_default', [&$this, 'save_default'] );
29
30 // Save Default Template in Bulk
31 add_action( 'wp_ajax_cooked_save_default_bulk', [&$this, 'save_default_bulk'] );
32
33 // Load Default Template
34 add_action( 'wp_ajax_cooked_load_default', [&$this, 'load_default'] );
35
36 // Get JSON list of Recipe IDs
37 add_action( 'wp_ajax_cooked_get_recipe_ids', [&$this, 'get_recipe_ids'] );
38
39 // Get JSON list of Recipe IDs, ready for Migration
40 add_action( 'wp_ajax_cooked_get_migrate_ids', [&$this, 'get_migrate_ids'] );
41
42 // Get JSON list of Recipe IDs, ready for Import
43 add_action( 'wp_ajax_cooked_get_import_ids', [&$this, 'get_import_ids']);
44
45 // Migrate Recipes
46 add_action( 'wp_ajax_cooked_migrate_recipes', [&$this, 'migrate_recipes'] );
47
48 // Import Recipes
49 add_action( 'wp_ajax_cooked_import_recipes', [&$this, 'import_recipes']);
50 }
51
52 public function get_migrate_ids() {
53 if (!current_user_can('edit_cooked_recipes')):
54 wp_die();
55 endif;
56
57 $old_recipes = get_transient('cooked_classic_recipes');
58 if ($old_recipes != 'complete'):
59 $total = count($old_recipes);
60
61 if ($total > 0):
62 echo wp_json_encode($old_recipes);
63 else:
64 echo 'false';
65 endif;
66 else:
67 echo 'false';
68 endif;
69
70 wp_die();
71 }
72
73 public function get_import_ids() {
74 if (!current_user_can('edit_cooked_recipes')):
75 wp_die();
76 endif;
77
78 $import_type = $_POST['import_type'];
79
80 $recipes = [];
81
82 if ($import_type === 'delicious_recipes') {
83 $args = [
84 'post_type' => 'recipe',
85 'posts_per_page' => -1,
86 'post_status' => 'any',
87 'fields' => 'ids',
88 'meta_query' => [
89 [
90 'key' => 'delicious_recipes_metadata',
91 'compare' => 'EXISTS',
92 ],
93 ],
94 ];
95 } elseif ($import_type === 'wp_recipe_maker') {
96 $args = [
97 'post_type' => 'recipe',
98 'posts_per_page' => -1,
99 'post_status' => 'any',
100 'fields' => 'ids',
101 'meta_query' => [
102 [
103 'key' => 'recipe_maker_metadata',
104 'compare' => 'EXISTS',
105 ],
106 ],
107 ];
108 }
109
110 $_recipes = new WP_Query( $args );
111
112 if (!empty($_recipes->posts)) {
113 foreach ($_recipes->posts as $rid) {
114 $recipes[] = $rid;
115 }
116 }
117
118 if (!empty($recipes)) {
119 $total = count($recipes);
120
121 if ($total > 0) {
122 echo wp_json_encode($recipes);
123 } else {
124 echo 'false';
125 }
126 } else {
127 echo 'false';
128 }
129
130 wp_die();
131 }
132
133 public function migrate_recipes() {
134 $bulk_amount = 10;
135
136 if (!current_user_can('edit_cooked_recipes')) {
137 wp_die();
138 }
139
140 if ( isset($_POST['recipe_ids']) ) {
141 // Sanitize Recipe IDs
142 $recipe_ids = json_decode( $_POST['recipe_ids'], true );
143
144 if ( is_array( $recipe_ids ) && !empty( $recipe_ids ) ) {
145 $_recipe_ids = [];
146 foreach ( $recipe_ids as $_rid ) {
147 $safe_id = intval( $_rid );
148 if ( $safe_id ) {
149 $_recipe_ids[] = $_rid;
150 }
151 }
152 $recipe_ids = $_recipe_ids;
153 } else {
154 return false;
155 }
156
157 $leftover_recipe_ids = array_slice( $recipe_ids, $bulk_amount );
158 $recipe_ids = array_slice( $recipe_ids, 0, $bulk_amount );
159
160 if ( !empty($recipe_ids) ) {
161 foreach( $recipe_ids as $rid ) {
162
163 $recipe_settings = Cooked_Recipes::get_settings( $rid );
164
165 if ( !empty( $recipe_settings ) && !isset( $recipe_settings['cooked_version'] ) || !empty( $recipe_settings ) && isset( $recipe_settings['cooked_version'] ) && !$recipe_settings['cooked_version'] ) {
166
167 $recipe_settings['cooked_version'] = COOKED_VERSION;
168
169 // Migrate the recipe settings.
170 update_post_meta( $rid, '_recipe_settings', $recipe_settings );
171 $recipe_excerpt = isset($recipe_settings['excerpt']) && $recipe_settings['excerpt'] ? $recipe_settings['excerpt'] : get_the_title( $rid );
172
173 $seo_content = apply_filters( 'cooked_seo_recipe_content', '[cooked-excerpt]<h2>' . __('Ingredients','cooked') . '</h2>[cooked-ingredients checkboxes=false]<h2>' . __('Directions','cooked') . '</h2>[cooked-directions numbers=false]' );
174 $seo_content = do_shortcode( $seo_content );
175
176 wp_update_post([
177 'ID' => $rid,
178 'post_excerpt' => $recipe_excerpt,
179 'post_content' => $seo_content
180 ]);
181
182 }
183 }
184
185 if ( !empty( $leftover_recipe_ids ) ) {
186 echo wp_json_encode( $leftover_recipe_ids );
187 wp_die();
188 }
189
190 }
191
192 set_transient( 'cooked_classic_recipes', 'complete', 60 * 60 * 24 * 7 );
193 echo 'false';
194 wp_die();
195
196 }
197
198 wp_die();
199 }
200
201 public function import_recipes() {
202 if (!current_user_can('edit_cooked_recipes')) {
203 wp_die();
204 }
205
206 require_once COOKED_DIR . 'includes/class.cooked-delicious-recipes.php';
207
208 $bulk_amount = 10;
209
210 if ( isset($_POST['recipe_ids']) ) {
211 // Sanitize Recipe IDs
212 $recipe_ids = json_decode( $_POST['recipe_ids'], true );
213
214 if ( is_array( $recipe_ids ) && !empty( $recipe_ids ) ) {
215 $_recipe_ids = [];
216 foreach ( $recipe_ids as $_rid ) {
217 $safe_id = intval( $_rid );
218 if ( $safe_id ) {
219 $_recipe_ids[] = $_rid;
220 }
221 }
222 $recipe_ids = $_recipe_ids;
223 } else {
224 return false;
225 }
226
227 $leftover_recipe_ids = array_slice( $recipe_ids, $bulk_amount );
228 $recipe_ids = array_slice( $recipe_ids, 0, $bulk_amount );
229
230 $import_type = $_POST['import_type'];
231
232 if ( !empty($recipe_ids) ) {
233 foreach ( $recipe_ids as $rid ) {
234 if ($import_type === 'delicious_recipes') {
235 Cooked_Delicious_Recipes::import_recipe( $rid );
236 } /* elseif ($import_type === 'wp_recipe_maker') {
237 Cooked_Recipe_Maker_Recipes::import_recipe( $rid );
238 } */
239 }
240
241 if ( !empty( $leftover_recipe_ids ) ) {
242 echo wp_json_encode( $leftover_recipe_ids );
243 wp_die();
244 } else {
245 if ($import_type === 'delicious_recipes') {
246 update_option( 'cooked_delicious_recipes_imported', true );
247 } /* elseif ($import_type === 'wp_recipe_maker') {
248 update_option( 'cooked_wp_recipe_maker_imported', true );
249 } */
250 }
251 }
252
253 echo 'false';
254 wp_die();
255 }
256
257 wp_die();
258 }
259
260 public function get_recipe_ids() {
261 if (!wp_verify_nonce($_POST['nonce'], 'cooked_save_default_bulk') || !current_user_can('edit_cooked_default_template')) {
262 wp_die();
263 }
264
265 $args = [
266 'post_type' => 'cp_recipe',
267 'posts_per_page' => -1,
268 'post_status' => 'any',
269 'fields' => 'ids'
270 ];
271
272 $_recipe_ids = Cooked_Recipes::get($args, false, true);
273 echo wp_json_encode($_recipe_ids);
274 wp_die();
275 }
276
277 public function save_default_bulk() {
278 $bulk_amount = 5;
279
280 if (!wp_verify_nonce($_POST['nonce'], 'cooked_save_default_bulk') || !current_user_can('edit_cooked_default_template')) {
281 wp_die();
282 }
283
284 if (isset($_POST['recipe_ids'])) {
285 $recipe_ids = json_decode($_POST['recipe_ids'], true);
286 if (is_array($recipe_ids) && !empty($recipe_ids)) {
287 $_recipe_ids = [];
288 foreach ($recipe_ids as $_rid) {
289 $safe_id = intval($_rid);
290 if ($safe_id) {
291 $_recipe_ids[] = $_rid;
292 }
293 }
294 $recipe_ids = $_recipe_ids;
295 } else {
296 return false;
297 }
298
299 $leftover_recipe_ids = array_slice($recipe_ids, $bulk_amount);
300 $recipe_ids = array_slice($recipe_ids, 0, $bulk_amount);
301
302 if (empty($recipe_ids)) {
303 echo 'false';
304 wp_die();
305 } else {
306 foreach ($recipe_ids as $rid) {
307 $recipe_settings = get_post_meta($rid, '_recipe_settings', true);
308 if (!empty($recipe_settings)) {
309 $recipe_settings['content'] = wp_kses_post($_POST['default_content']);
310 update_post_meta($rid, '_recipe_settings', $recipe_settings);
311 }
312 }
313
314 if (!empty($leftover_recipe_ids)) {
315 echo wp_json_encode($leftover_recipe_ids);
316 wp_die();
317 } else {
318 echo 'false';
319 wp_die();
320 }
321 }
322 }
323
324 wp_die();
325 }
326
327 public function save_default() {
328 global $_cooked_settings;
329
330 if (!wp_verify_nonce($_POST['nonce'], 'cooked_save_default') || !current_user_can('edit_cooked_default_template')) {
331 wp_die();
332 }
333
334 if (isset($_POST['default_content'])) {
335 $_cooked_settings['default_content'] = wp_kses_post( $_POST['default_content'] );
336 update_option('cooked_settings', $_cooked_settings);
337 } else {
338 echo 'No default content provided.';
339 }
340
341 wp_die();
342 }
343
344 public function load_default() {
345 global $_cooked_settings;
346
347 if (!current_user_can('edit_cooked_recipes')) {
348 wp_die();
349 }
350
351 if (isset($_cooked_settings['default_content'])) {
352 $default_content = stripslashes($_cooked_settings['default_content']);
353 } else {
354 $default_content = Cooked_Recipes::default_content();
355 }
356
357 echo wp_kses_post($default_content);
358
359 wp_die();
360 }
361 }
362