| 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' => 'wprm_recipe', |
| 98 |
'posts_per_page' => -1, |
| 99 |
'post_status' => 'any', |
| 100 |
'fields' => 'ids', |
| 101 |
'meta_query' => [ |
| 102 |
[ |
| 103 |
'key' => 'wprm_type', |
| 104 |
'value' => 'food', |
| 105 |
'compare' => '=', |
| 106 |
], |
| 107 |
], |
| 108 |
]; |
| 109 |
} |
| 110 |
|
| 111 |
$_recipes = new WP_Query( $args ); |
| 112 |
|
| 113 |
if (!empty($_recipes->posts)) { |
| 114 |
foreach ($_recipes->posts as $rid) { |
| 115 |
$recipes[] = $rid; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
if (!empty($recipes)) { |
| 120 |
$total = count($recipes); |
| 121 |
|
| 122 |
if ($total > 0) { |
| 123 |
echo wp_json_encode($recipes); |
| 124 |
} else { |
| 125 |
echo 'false'; |
| 126 |
} |
| 127 |
} else { |
| 128 |
echo 'false'; |
| 129 |
} |
| 130 |
|
| 131 |
wp_die(); |
| 132 |
} |
| 133 |
|
| 134 |
public function migrate_recipes() { |
| 135 |
$bulk_amount = 10; |
| 136 |
|
| 137 |
if (!current_user_can('edit_cooked_recipes')) { |
| 138 |
wp_die(); |
| 139 |
} |
| 140 |
|
| 141 |
if ( isset($_POST['recipe_ids']) ) { |
| 142 |
// Sanitize Recipe IDs |
| 143 |
$recipe_ids = json_decode( $_POST['recipe_ids'], true ); |
| 144 |
|
| 145 |
if ( is_array( $recipe_ids ) && !empty( $recipe_ids ) ) { |
| 146 |
$_recipe_ids = []; |
| 147 |
foreach ( $recipe_ids as $_rid ) { |
| 148 |
$safe_id = intval( $_rid ); |
| 149 |
if ( $safe_id ) { |
| 150 |
$_recipe_ids[] = $_rid; |
| 151 |
} |
| 152 |
} |
| 153 |
$recipe_ids = $_recipe_ids; |
| 154 |
} else { |
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
$leftover_recipe_ids = array_slice( $recipe_ids, $bulk_amount ); |
| 159 |
$recipe_ids = array_slice( $recipe_ids, 0, $bulk_amount ); |
| 160 |
|
| 161 |
if ( !empty($recipe_ids) ) { |
| 162 |
foreach( $recipe_ids as $rid ) { |
| 163 |
|
| 164 |
$recipe_settings = Cooked_Recipes::get_settings( $rid ); |
| 165 |
|
| 166 |
if ( !empty( $recipe_settings ) && !isset( $recipe_settings['cooked_version'] ) || !empty( $recipe_settings ) && isset( $recipe_settings['cooked_version'] ) && !$recipe_settings['cooked_version'] ) { |
| 167 |
|
| 168 |
$recipe_settings['cooked_version'] = COOKED_VERSION; |
| 169 |
|
| 170 |
// Migrate the recipe settings. |
| 171 |
update_post_meta( $rid, '_recipe_settings', $recipe_settings ); |
| 172 |
$recipe_excerpt = isset($recipe_settings['excerpt']) && $recipe_settings['excerpt'] ? $recipe_settings['excerpt'] : get_the_title( $rid ); |
| 173 |
|
| 174 |
$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]' ); |
| 175 |
$seo_content = do_shortcode( $seo_content ); |
| 176 |
|
| 177 |
wp_update_post([ |
| 178 |
'ID' => $rid, |
| 179 |
'post_excerpt' => $recipe_excerpt, |
| 180 |
'post_content' => $seo_content |
| 181 |
]); |
| 182 |
|
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
if ( !empty( $leftover_recipe_ids ) ) { |
| 187 |
echo wp_json_encode( $leftover_recipe_ids ); |
| 188 |
wp_die(); |
| 189 |
} |
| 190 |
|
| 191 |
} |
| 192 |
|
| 193 |
set_transient( 'cooked_classic_recipes', 'complete', 60 * 60 * 24 * 7 ); |
| 194 |
echo 'false'; |
| 195 |
wp_die(); |
| 196 |
|
| 197 |
} |
| 198 |
|
| 199 |
wp_die(); |
| 200 |
} |
| 201 |
|
| 202 |
public function import_recipes() { |
| 203 |
if (!current_user_can('edit_cooked_recipes')) { |
| 204 |
wp_die(); |
| 205 |
} |
| 206 |
|
| 207 |
require_once COOKED_DIR . 'includes/class.cooked-delicious-recipes.php'; |
| 208 |
require_once COOKED_DIR . 'includes/class.cooked-recipe-maker.php'; |
| 209 |
|
| 210 |
$bulk_amount = 10; |
| 211 |
|
| 212 |
if ( isset($_POST['recipe_ids']) ) { |
| 213 |
// Sanitize Recipe IDs |
| 214 |
$recipe_ids = json_decode( $_POST['recipe_ids'], true ); |
| 215 |
|
| 216 |
if ( is_array( $recipe_ids ) && !empty( $recipe_ids ) ) { |
| 217 |
$_recipe_ids = []; |
| 218 |
foreach ( $recipe_ids as $_rid ) { |
| 219 |
$safe_id = intval( $_rid ); |
| 220 |
if ( $safe_id ) { |
| 221 |
$_recipe_ids[] = $_rid; |
| 222 |
} |
| 223 |
} |
| 224 |
$recipe_ids = $_recipe_ids; |
| 225 |
} else { |
| 226 |
return false; |
| 227 |
} |
| 228 |
|
| 229 |
$leftover_recipe_ids = array_slice( $recipe_ids, $bulk_amount ); |
| 230 |
$recipe_ids = array_slice( $recipe_ids, 0, $bulk_amount ); |
| 231 |
|
| 232 |
$import_type = $_POST['import_type']; |
| 233 |
|
| 234 |
if ( !empty($recipe_ids) ) { |
| 235 |
foreach ( $recipe_ids as $rid ) { |
| 236 |
if ($import_type === 'delicious_recipes') { |
| 237 |
Cooked_Delicious_Recipes::import_recipe( $rid ); |
| 238 |
} elseif ($import_type === 'wp_recipe_maker') { |
| 239 |
Cooked_Recipe_Maker_Recipes::import_recipe( $rid ); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
if ( !empty( $leftover_recipe_ids ) ) { |
| 244 |
echo wp_json_encode( $leftover_recipe_ids ); |
| 245 |
wp_die(); |
| 246 |
} else { |
| 247 |
if ($import_type === 'delicious_recipes') { |
| 248 |
update_option( 'cooked_delicious_recipes_imported', true ); |
| 249 |
} elseif ($import_type === 'wp_recipe_maker') { |
| 250 |
update_option( 'cooked_wp_recipe_maker_imported', true ); |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
echo 'false'; |
| 256 |
wp_die(); |
| 257 |
} |
| 258 |
|
| 259 |
wp_die(); |
| 260 |
} |
| 261 |
|
| 262 |
public function get_recipe_ids() { |
| 263 |
if (!wp_verify_nonce($_POST['nonce'], 'cooked_save_default_bulk') || !current_user_can('edit_cooked_default_template')) { |
| 264 |
wp_die(); |
| 265 |
} |
| 266 |
|
| 267 |
$args = [ |
| 268 |
'post_type' => 'cp_recipe', |
| 269 |
'posts_per_page' => -1, |
| 270 |
'post_status' => 'any', |
| 271 |
'fields' => 'ids' |
| 272 |
]; |
| 273 |
|
| 274 |
$_recipe_ids = Cooked_Recipes::get($args, false, true); |
| 275 |
echo wp_json_encode($_recipe_ids); |
| 276 |
wp_die(); |
| 277 |
} |
| 278 |
|
| 279 |
public function save_default_bulk() { |
| 280 |
$bulk_amount = 5; |
| 281 |
|
| 282 |
if (!wp_verify_nonce($_POST['nonce'], 'cooked_save_default_bulk') || !current_user_can('edit_cooked_default_template')) { |
| 283 |
wp_die(); |
| 284 |
} |
| 285 |
|
| 286 |
if (isset($_POST['recipe_ids'])) { |
| 287 |
$recipe_ids = json_decode($_POST['recipe_ids'], true); |
| 288 |
if (is_array($recipe_ids) && !empty($recipe_ids)) { |
| 289 |
$_recipe_ids = []; |
| 290 |
foreach ($recipe_ids as $_rid) { |
| 291 |
$safe_id = intval($_rid); |
| 292 |
if ($safe_id) { |
| 293 |
$_recipe_ids[] = $_rid; |
| 294 |
} |
| 295 |
} |
| 296 |
$recipe_ids = $_recipe_ids; |
| 297 |
} else { |
| 298 |
return false; |
| 299 |
} |
| 300 |
|
| 301 |
$leftover_recipe_ids = array_slice($recipe_ids, $bulk_amount); |
| 302 |
$recipe_ids = array_slice($recipe_ids, 0, $bulk_amount); |
| 303 |
|
| 304 |
if (empty($recipe_ids)) { |
| 305 |
echo 'false'; |
| 306 |
wp_die(); |
| 307 |
} else { |
| 308 |
foreach ($recipe_ids as $rid) { |
| 309 |
$recipe_settings = get_post_meta($rid, '_recipe_settings', true); |
| 310 |
if (!empty($recipe_settings)) { |
| 311 |
$recipe_settings['content'] = wp_kses_post($_POST['default_content']); |
| 312 |
update_post_meta($rid, '_recipe_settings', $recipe_settings); |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
if (!empty($leftover_recipe_ids)) { |
| 317 |
echo wp_json_encode($leftover_recipe_ids); |
| 318 |
wp_die(); |
| 319 |
} else { |
| 320 |
echo 'false'; |
| 321 |
wp_die(); |
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
wp_die(); |
| 327 |
} |
| 328 |
|
| 329 |
public function save_default() { |
| 330 |
global $_cooked_settings; |
| 331 |
|
| 332 |
if (!wp_verify_nonce($_POST['nonce'], 'cooked_save_default') || !current_user_can('edit_cooked_default_template')) { |
| 333 |
wp_die(); |
| 334 |
} |
| 335 |
|
| 336 |
if (isset($_POST['default_content'])) { |
| 337 |
$_cooked_settings['default_content'] = wp_kses_post( $_POST['default_content'] ); |
| 338 |
update_option('cooked_settings', $_cooked_settings); |
| 339 |
} else { |
| 340 |
echo 'No default content provided.'; |
| 341 |
} |
| 342 |
|
| 343 |
wp_die(); |
| 344 |
} |
| 345 |
|
| 346 |
public function load_default() { |
| 347 |
global $_cooked_settings; |
| 348 |
|
| 349 |
if (!current_user_can('edit_cooked_recipes')) { |
| 350 |
wp_die(); |
| 351 |
} |
| 352 |
|
| 353 |
if (isset($_cooked_settings['default_content'])) { |
| 354 |
$default_content = wp_unslash($_cooked_settings['default_content']); |
| 355 |
} else { |
| 356 |
$default_content = Cooked_Recipes::default_content(); |
| 357 |
} |
| 358 |
|
| 359 |
echo wp_kses_post($default_content); |
| 360 |
|
| 361 |
wp_die(); |
| 362 |
} |
| 363 |
} |
| 364 |
|