PluginProbe
Cooked – Recipe Management / trunk
Cooked – Recipe Management vtrunk
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 1.8.5 All 36 releases
cooked / includes / class.cooked-import.php

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

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