PluginProbe
Cooked – Recipe Management / 1.16.1
Cooked – Recipe Management v1.16.1
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-import.php

class.cooked-import.php in Cooked – Recipe Management 1.16.1, at includes/class.cooked-import.php

244 lines 15.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Register Import
4 *
5 * @package Cooked
6 * @subpackage Import
7 * @since 1.8.2
8 */
9
10 // Exit if accessed directly
11 if ( !defined( 'ABSPATH' ) ) exit;
12
13 /**
14 * Cooked_Import Class
15 *
16 * This class handles the import of recipes from other plugins.
17 *
18 * @since 1.8.2
19 */
20 class Cooked_Import {
21
22 public function __construct() {
23 add_filter( 'admin_init', [&$this, 'init'] );
24 add_filter( 'init', [&$this, 'init'] );
25 }
26
27 public static function init() {
28 register_setting( 'cooked_import_group', 'cooked_import', ['sanitize_callback' => [__CLASS__, 'sanitize_saved_flag']] );
29 register_setting( 'cooked_import_group', 'cooked_import_saved', ['sanitize_callback' => [__CLASS__, 'sanitize_saved_flag']] );
30 }
31
32 public static function sanitize_saved_flag( $value ) {
33 return rest_sanitize_boolean( $value );
34 }
35
36 public static function tabs_fields() {
37 $Cooked_Delicious_Recipes = new Cooked_Delicious_Recipes();
38 $delicious_recipes = $Cooked_Delicious_Recipes::get_recipes();
39 $cooked_delicious_recipes_imported = get_option( 'cooked_delicious_recipes_imported', false );
40
41 $Cooked_Recipe_Maker_Recipes = new Cooked_Recipe_Maker_Recipes();
42 $wp_recipe_maker_recipes = $Cooked_Recipe_Maker_Recipes::get_recipes();
43 $cooked_wp_recipe_maker_imported = get_option( 'cooked_wp_recipe_maker_imported', false );
44
45 $import_tabs = [];
46 if (!empty($delicious_recipes)) {
47 $total = count($delicious_recipes);
48
49 /* translators: for displaying singular or plural versions depending on the number of recipes. */
50 $html_desc = sprintf( esc_html( _n( 'There is %1$s recipe that should be imported from %2$s.', 'There are %1$s recipes that should be imported from %2$s.', $total, 'cooked' ) ), '<strong>' . number_format( $total ) . '</strong>', '<strong>WP Delicious (formerly Delicious Recipes)</strong>' );
51 $html_desc .= '<br>';
52 $html_desc .= __( 'Before you begin, please make sure you <b>backup your database</b> in case something goes wrong.', 'cooked' );
53 $html_desc .= '<br>';
54 $html_desc .= __( 'Click the button below to import these recipes. Here is what will happen to your recipes:', 'cooked' );
55 $html_desc .= '<ul class="cooked-admin-ul">';
56 $html_desc .= '<li>' . __( 'Recipes will be imported with the <b>\'Draft\'</b> status.', 'cooked' ) . '</li>';
57 $html_desc .= '<li>' . __( 'Comments and ratings data will also be imported (ratings are available in Cooked Pro).', 'cooked' ) . '</li>';
58 $html_desc .= '<li>' . __( 'After the import is complete, you can bulk edit the recipes and change their status to <b>\'Published\'</b>.', 'cooked' ) . '</li>';
59 $html_desc .= '<li>' . __( 'The existing WP Delicious recipes and data will not be modified or deleted.', 'cooked' ) . '</li>';
60 $html_desc .= '<li>' . __( 'Certain data that is not suppoted by Cooked will not be imported (such as Cooking Temp, Estimated Cost, Recipe Keywords, etc).', 'cooked' ) . '</li>';
61 $html_desc .= '<li>' . __( 'You can run the import multiple times, but keep in mind that duplicate recipes will be created.', 'cooked' ) . '</li>';
62 $html_desc .= '</ul>';
63
64 if ($total > 2000) {
65 $html_desc .= '<p class="cooked-import-note"><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>';
66 } else {
67 $html_desc .= '<p class="cooked-import-note"><strong>' . __( 'Note:', 'cooked' ) . '</strong> ' . __( 'The more recipes you have, the longer this will take.', 'cooked' ) . '</p>';
68 }
69
70 if ($total > 0) {
71 $import_tabs['delicious_recipes_import'] = [
72 'name' => __('WP Delicious - Import', 'cooked'),
73 'icon' => 'migrate',
74 'fields' => [
75 'cooked_import_button' => [
76 'title' => 'WP Delicious (formerly Delicious Recipes)&nbsp;&nbsp;<i class="cooked-icon cooked-icon-angle-right"></i>&nbsp;&nbsp;Cooked',
77 'desc' => $html_desc,
78 'type' => 'import_button',
79 'total' => $total,
80 'import_type' => $Cooked_Delicious_Recipes->import_type,
81 ]
82 ]
83 ];
84 }
85 } else {
86 $import_tabs['no_delicious_recipes'] = [
87 'name' => __('WP Delicious - Import', 'cooked'),
88 'icon' => 'migrate',
89 'fields' => [
90 'cooked_no_delicious_recipes' => [
91 'title' => 'WP Delicious (formerly Delicious Recipes)',
92 'desc' => '',
93 'type' => 'message',
94 'message' => 'There are no recipes to import from WP Delicious.',
95 ]
96 ]
97 ];
98 }
99
100 if (!empty($wp_recipe_maker_recipes)) {
101 $total = count($wp_recipe_maker_recipes);
102
103 /* translators: for displaying singular or plural versions depending on the number of recipes. */
104 $html_desc = sprintf( esc_html( _n( 'There is %1$s recipe that should be imported from %2$s.', 'There are %1$s recipes that should be imported from %2$s.', $total, 'cooked' ) ), '<strong>' . number_format( $total ) . '</strong>', '<strong>WP Recipe Maker</strong>' );
105 $html_desc .= '<br>';
106 $html_desc .= __( 'Before you begin, please make sure you <b>backup your database</b> in case something goes wrong.', 'cooked' );
107 $html_desc .= '<br>';
108 $html_desc .= __( 'Click the button below to import these recipes. Here is what will happen to your recipes:', 'cooked' );
109 $html_desc .= '<ul class="cooked-admin-ul">';
110 $html_desc .= '<li>' . __( 'Recipes will be imported with the <b>\'Draft\'</b> status.', 'cooked' ) . '</li>';
111 $html_desc .= '<li>' . __( 'Comments and ratings data will also be imported (ratings are available in Cooked Pro).', 'cooked' ) . '</li>';
112 $html_desc .= '<li>' . __( 'The difficulty level will be set to <b>Beginner</b> since it is not supported by WP Recipe Maker.', 'cooked' ) . '</li>';
113 $html_desc .= '<li>' . __( 'After the import is complete, you can bulk edit the recipes and change their status to <b>\'Published\'</b>.', 'cooked' ) . '</li>';
114 $html_desc .= '<li>' . __( 'The existing WP Recipe Maker and data will not be modified or deleted.', 'cooked' ) . '</li>';
115 $html_desc .= '<li>' . __( 'Certain data that is not suppoted by Cooked will not be imported (such as Cooking Temp, Estimated Cost, Recipe Keywords, etc).', 'cooked' ) . '</li>';
116 $html_desc .= '<li>' . __( 'You can run the import multiple times, but keep in mind that duplicate recipes will be created.', 'cooked' ) . '</li>';
117 $html_desc .= '</ul>';
118
119 if ($total > 2000) {
120 $html_desc .= '<p class="cooked-import-note"><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>';
121 } else {
122 $html_desc .= '<p class="cooked-import-note"><strong>' . __( 'Note:', 'cooked' ) . '</strong> ' . __( 'The more recipes you have, the longer this will take.', 'cooked' ) . '</p>';
123 }
124
125 if ($total > 0) {
126 $import_tabs['wp_recipe_maker_import'] = [
127 'name' => __('WP Recipe Maker - Import', 'cooked'),
128 'icon' => 'migrate',
129 'fields' => [
130 'cooked_import_button' => [
131 'title' => 'WP Recipe Maker&nbsp;&nbsp;<i class="cooked-icon cooked-icon-angle-right"></i>&nbsp;&nbsp;Cooked',
132 'desc' => $html_desc,
133 'type' => 'import_button',
134 'total' => $total,
135 'import_type' => $Cooked_Recipe_Maker_Recipes->import_type,
136 ]
137 ]
138 ];
139 }
140 } else {
141 $import_tabs['no_wp_recipe_maker_recipes'] = [
142 'name' => __('WP Recipe Maker - Import', 'cooked'),
143 'icon' => 'migrate',
144 'fields' => [
145 'cooked_no_wp_recipe_maker_recipes' => [
146 'title' => 'WP Recipe Maker',
147 'desc' => '',
148 'type' => 'message',
149 'message' => 'There are no recipes to import from WP Recipe Maker.',
150 ]
151 ]
152 ];
153 }
154
155 // CSV Import Tab
156 $html_desc = __( 'Import recipes from a CSV file. Your CSV file should include the following columns:', 'cooked' );
157 $html_desc .= '<ul class="cooked-admin-ul">';
158 $html_desc .= '<li><strong>title</strong> - ' . __( 'Recipe title (required)', 'cooked' ) . '</li>';
159 $html_desc .= '<li><strong>excerpt</strong> - ' . __( 'Excerpt/description', 'cooked' ) . '</li>';
160 $html_desc .= '<li><strong>prep_time</strong> - ' . __( 'Prep time in minutes', 'cooked' ) . '</li>';
161 $html_desc .= '<li><strong>cook_time</strong> - ' . __( 'Cook time in minutes', 'cooked' ) . '</li>';
162 $html_desc .= '<li><strong>difficulty_level</strong> - ' . __( 'Difficulty level (1=Beginner, 2=Intermediate, 3=Advanced)', 'cooked' ) . '</li>';
163 $html_desc .= '<li style="line-height: 1.6;"><strong>ingredients</strong> - ' . __( 'Ingredients, separated by pipe <code>(|)</code>. <br>Format: <code>"amount|measurement|name"</code> or <code>"name"</code> for simple ingredients.<br>Add substitutions with double pipe <code>(||)</code>: <code>"amount|measurement|name||sub_amount|sub_measurement|sub_name"</code>', 'cooked' ) . '</li>';
164 $html_desc .= '<li><strong>directions</strong> - ' . __( 'Directions/instructions, separated by pipe <code>(|)</code>', 'cooked' ) . '</li>';
165 $html_desc .= '<li><strong>notes</strong> - ' . __( 'Notes', 'cooked' ) . '</li>';
166 $html_desc .= '<li><strong>category</strong> - ' . __( 'Category, separated by comma', 'cooked' ) . '</li>';
167 if ( defined('COOKED_PRO_VERSION') ) {
168 $html_desc .= '<li><strong>cuisine</strong> - ' . __( 'Cuisine, separated by comma', 'cooked' ) . '</li>';
169 $html_desc .= '<li><strong>cooking_method</strong> - ' . __( 'Cooking method, separated by comma', 'cooked' ) . '</li>';
170 $html_desc .= '<li><strong>tags</strong> - ' . __( 'Tags, separated by comma', 'cooked' ) . '</li>';
171 $html_desc .= '<li><strong>diet</strong> - ' . __( 'Restricted diet type (Schema.org RestrictedDiet), separated by comma', 'cooked' ) . '</li>';
172 }
173 $html_desc .= '</ul>';
174 $html_desc .= '<p class="cooked-import-note"><strong>' . __( 'Note:', 'cooked' ) . '</strong> ' . __( 'Recipes will be imported with the <b>\'Draft\'</b> status. After the import is complete, you can bulk edit the recipes and change their status to <b>\'Published\'</b>.', 'cooked' ) . '</p>';
175
176 $import_tabs['csv_import'] = [
177 'name' => __('CSV Import', 'cooked'),
178 'icon' => 'migrate',
179 'fields' => [
180 'cooked_csv_import' => [
181 'title' => __('Import Recipes via CSV', 'cooked'),
182 'desc' => $html_desc,
183 'type' => 'csv_upload',
184 ]
185 ]
186 ];
187
188 $import_tabs['more_imports_coming_soon'] = [
189 'name' => __('More Imports are Coming Soon...', 'cooked'),
190 'icon' => 'migrate',
191 'fields' => [
192 'cooked_more_imports_coming_soon' => [
193 'title' => 'More Imports are Coming Soon...',
194 'desc' => '',
195 'type' => 'message',
196 'message' => 'More Imports are Coming Soon...',
197 ]
198 ]
199 ];
200
201 return apply_filters('cooked_import_tabs_fields', $import_tabs);
202 }
203
204 public static function field_import_button( $name, $field_options, $color, $field ) {
205 $total = $field['total'];
206 $import_type = $field['import_type'];
207
208 if ($total > 0) {
209 echo '<p>';
210 echo '<input id="cooked-import-button" type="button" class="button-secondary" data-import-type="' . esc_attr( $import_type ) . '" name="begin_cooked_migration" value="' . esc_attr__( 'Begin Import', 'cooked' ) . '">';
211 echo '</p>';
212 echo '<p>';
213 echo '<span id="cooked-import-progress" class="cooked-progress"><span class="cooked-progress-bar"></span></span><span id="cooked-import-progress-text" class="cooked-progress-text">0 / ' . esc_html( $total ) . '</span>';
214 echo '</p>';
215 echo '<p id="cooked-import-completed"><strong>Import Complete!</strong> You can now <a href="' . esc_url( add_query_arg(['page' => 'cooked_import'], esc_url( admin_url( 'admin.php' ) ) ) ) . '">' . esc_html__( 'reload', 'cooked' ) . '</a> the import screen.</p>';
216 }
217 }
218
219 public static function field_message( $name, $field_options, $color, $field ) {
220 echo '<p>' . wp_kses_post( $field['message'] ) . '</p>';
221 }
222
223 public static function field_csv_upload( $name, $field_options, $color, $field ) {
224 echo '<p class="cooked-import-note"><strong>' . esc_html__( 'Download sample CSV files:', 'cooked' ) . '</strong> ';
225 echo '<a href="' . esc_url( COOKED_URL . 'sample-data/recipes-small.csv' ) . '" download>' . esc_html__( 'Small (1 recipe)', 'cooked' ) . '</a>, ';
226 echo '<a href="' . esc_url( COOKED_URL . 'sample-data/recipes-medium.csv' ) . '" download>' . esc_html__( 'Medium (3 recipes)', 'cooked' ) . '</a>, ';
227 echo '<a href="' . esc_url( COOKED_URL . 'sample-data/recipes-large.csv' ) . '" download>' . esc_html__( 'Large (10 recipes)', 'cooked' ) . '</a></p>';
228 echo '<form id="cooked-csv-import-form" enctype="multipart/form-data">';
229 echo '<p>';
230 echo '<input type="file" id="cooked-csv-file" name="csv_file" accept=".csv" required>';
231 echo '</p>';
232 echo '<p>';
233 echo '<input id="cooked-csv-import-button" type="button" class="button-primary" value="' . esc_attr__( 'Upload and Import CSV', 'cooked' ) . '">';
234 echo '</p>';
235 echo '<p>';
236 echo '<span id="cooked-csv-import-progress" class="cooked-progress"><span class="cooked-progress-bar"></span></span><span id="cooked-csv-import-progress-text" class="cooked-progress-text"></span>';
237 echo '</p>';
238 echo '<p id="cooked-csv-import-completed" style="display:none;"><strong>' . esc_html__( 'Import Complete!', 'cooked' ) . '</strong> ' . esc_html__( 'You can now', 'cooked' ) . ' <a href="' . esc_url( add_query_arg(['page' => 'cooked_import'], admin_url( 'admin.php' ) ) ) . '">' . esc_html__( 'reload', 'cooked' ) . '</a> ' . esc_html__( 'the import screen or', 'cooked' ) . ' <a href="' . esc_url( admin_url( 'edit.php?post_type=cp_recipe' ) ) . '">' . esc_html__( 'view your recipes', 'cooked' ) . '</a>.</p>';
239 echo '<div id="cooked-csv-import-errors" style="display:none; color: #d63638; margin-top: 10px;"></div>';
240 echo '</form>';
241 }
242
243 }
244