| 1 |
<?php |
| 2 |
/** |
| 3 |
* Tools for Migration from Cooked Classic |
| 4 |
* |
| 5 |
* @package Cooked |
| 6 |
* @subpackage Migration |
| 7 |
* @since 1.3.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
// Exit if accessed directly |
| 11 |
if ( !defined( 'ABSPATH' ) ) exit; |
| 12 |
|
| 13 |
/** |
| 14 |
* Cooked_Migration Class |
| 15 |
* |
| 16 |
* This class handles the migration from Cooked Classic. |
| 17 |
* |
| 18 |
* @since 1.3.0 |
| 19 |
*/ |
| 20 |
class Cooked_Migration { |
| 21 |
|
| 22 |
public function __construct() { |
| 23 |
add_action('plugins_loaded', [&$this, 'init']); |
| 24 |
} |
| 25 |
|
| 26 |
public static function init() { |
| 27 |
// Check for recipes from Cooked Classic and display an "Update" notice |
| 28 |
$old_recipes = self::get_cooked_classic_recipes(); |
| 29 |
|
| 30 |
if ($old_recipes != 'complete' && !empty($old_recipes)) { |
| 31 |
$total_old_recipes = count( $old_recipes ); |
| 32 |
|
| 33 |
if ($total_old_recipes > 0) { |
| 34 |
add_filter('cooked_settings_tabs_fields', ['Cooked_Migration', 'settings_filter'], 10, 1); |
| 35 |
add_action('admin_notices', ['Cooked_Migration', 'old_recipes_message'], 10); |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
public static function settings_filter($settings) { |
| 41 |
$old_recipes = self::get_cooked_classic_recipes(); |
| 42 |
|
| 43 |
if ($old_recipes != 'complete' && !empty($old_recipes)) { |
| 44 |
$total = count($old_recipes); |
| 45 |
|
| 46 |
/* translators: for displaying singular or plural versions depending on the number of recipes. */ |
| 47 |
$html_desc = sprintf( esc_html( _n( 'There is %1$s recipe that should be migrated from %2$s to take advantage of new features and reliability.', 'There are %1$s recipes that should be migrated from %2$s to take advantage of new features and reliability.', $total, 'cooked' ) ), '<strong>' . number_format( $total ) . '</strong>', '<strong>Cooked Classic</strong>' ); |
| 48 |
$html_desc .= '<br>'; |
| 49 |
$html_desc .= __( 'Please click the button below to migrate these recipes. Here is what will happen to your recipes:', 'cooked' ); |
| 50 |
$html_desc .= '<ul class="cooked-admin-ul">'; |
| 51 |
$html_desc .= '<li>' . __( 'NO DATA LOSS, all fields will be remapped.', 'cooked' ) . '</li>'; |
| 52 |
$html_desc .= '<li>' . __( 'Remapped fields will greatly speed up recipe loading times.', 'cooked' ) . '</li>'; |
| 53 |
$html_desc .= '<li>' . __( 'If recipe excerpt exists, the short description will be moved to the top of the recipe template.', 'cooked' ) . '</li>'; |
| 54 |
$html_desc .= '<li>' . __( 'If no recipe excerpt exists, the short description will be used instead.', 'cooked' ) . '</li>'; |
| 55 |
$html_desc .= '<li>' . __( 'Version number will be applied to each recipe.', 'cooked' ) . '</li>'; |
| 56 |
$html_desc .= '</ul>'; |
| 57 |
if ($total > 2000) { |
| 58 |
$html_desc .= '<p><strong>' . __('Wow, you have a lot of recipes!', 'cooked') . '</strong><br><em style="color:#333;">' . __( 'It is definitely recommended that you get yourself a cup of coffee or tea after clicking this button.', 'cooked' ) . '</em></p>'; |
| 59 |
} else { |
| 60 |
$html_desc .= '<p><strong>' . __('Note:', 'cooked' ) . '</strong> ' . __( 'The more recipes you have, the longer this will take.', 'cooked') . '</p>'; |
| 61 |
} |
| 62 |
|
| 63 |
if ($total > 0) { |
| 64 |
$settings['migration'] = [ |
| 65 |
'name' => __('Migration', 'cooked'), |
| 66 |
'icon' => 'migrate', |
| 67 |
'fields' => [ |
| 68 |
'cooked_migrate_button' => [ |
| 69 |
'title' => 'Cooked Classic <i class="cooked-icon cooked-icon-angle-right"></i> Cooked', |
| 70 |
'desc' => $html_desc, |
| 71 |
'type' => 'migrate_button' |
| 72 |
] |
| 73 |
] |
| 74 |
]; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
return $settings; |
| 79 |
} |
| 80 |
|
| 81 |
public static function old_recipes_message() { |
| 82 |
$old_recipes = get_transient('cooked_classic_recipes'); |
| 83 |
|
| 84 |
if ($old_recipes != 'complete') { |
| 85 |
$total = count($old_recipes); |
| 86 |
|
| 87 |
if ($total > 0) { |
| 88 |
$class = 'notice notice-error'; |
| 89 |
/* translators: for displaying singular or plural versions depending on the number of recipes. */ |
| 90 |
$message = sprintf( esc_html( _n( 'There is %1$s recipe that is from an older version of Cooked. Please %2$s to migrate this recipe.', 'There are %1$s recipes that are from an older version of Cooked. Please %2$s to migrate these recipes.', $total, 'cooked' ) ), '<strong>' . number_format( $total ) . '</strong>', '<strong><a href="' . esc_url( add_query_arg(['page' => 'cooked_settings', 'cm' => '1'], admin_url( 'admin.php' ) ) ) . '#migration">' . __( 'click here', 'cooked' ) . '</a></strong>' ); |
| 91 |
printf('<div class="%1$s" style="padding:10px 20px"><p style="font-size:1.2em">%2$s</p></div>', esc_attr($class), $message); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
public static function get_cooked_classic_recipes() { |
| 97 |
$classic_recipes = get_transient('cooked_classic_recipes'); |
| 98 |
|
| 99 |
if (empty($classic_recipes) && $classic_recipes != 'complete') { |
| 100 |
|
| 101 |
$classic_recipes = []; |
| 102 |
|
| 103 |
$args = [ |
| 104 |
'post_type' => 'cp_recipe', |
| 105 |
'posts_per_page' => -1, |
| 106 |
'post_status' => 'any', |
| 107 |
'fields' => 'ids', |
| 108 |
'meta_query' => [ |
| 109 |
'settings_clause' => [ |
| 110 |
'key' => '_recipe_settings', |
| 111 |
'compare' => 'NOT EXISTS', |
| 112 |
] |
| 113 |
] |
| 114 |
]; |
| 115 |
|
| 116 |
$_recipes = Cooked_Recipes::get($args, false, true); |
| 117 |
|
| 118 |
if (!empty($_recipes)) { |
| 119 |
foreach ($_recipes as $rid) { |
| 120 |
$recipe_settings = Cooked_Recipes::get_settings($rid, false); |
| 121 |
if (!isset($recipe_settings['cooked_version'])) { |
| 122 |
$classic_recipes[] = $rid; |
| 123 |
} |
| 124 |
} |
| 125 |
} else { |
| 126 |
$classic_recipes = []; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
if (empty($classic_recipes)) { |
| 131 |
set_transient('cooked_classic_recipes', 'complete', 60 * 60 * 24 * 7); |
| 132 |
return 'complete'; |
| 133 |
} else { |
| 134 |
set_transient('cooked_classic_recipes', $classic_recipes, 60 * 60); |
| 135 |
return $classic_recipes; |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|