PluginProbe
Cooked – Recipe Management / 1.11.4
Cooked – Recipe Management v1.11.4
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-updates.php

class.cooked-updates.php in Cooked – Recipe Management 1.11.4, at includes/class.cooked-updates.php

263 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Updates and Data Migrations
4 *
5 * @package Cooked_Updates
6 * @subpackage Cooked_Updates / Core
7 * @since 1.11.2
8 */
9
10 // Exit if accessed directly
11 if ( !defined( 'ABSPATH' ) ) exit;
12
13 /**
14 * Cooked_Updates Class
15 *
16 * This class handles all version updates and data migrations for the Cooked plugin.
17 * It follows WordPress best practices for plugin updates and ensures data integrity.
18 *
19 * @since 1.11.2
20 */
21 class Cooked_Updates {
22
23 /**
24 * Current plugin version
25 */
26 private static $current_version;
27
28 /**
29 * Previous plugin version
30 */
31 private static $previous_version;
32
33 /**
34 * Current pro plugin version
35 */
36 private static $current_pro_version;
37
38 /**
39 * Previous pro plugin version
40 */
41 private static $previous_pro_version;
42
43 /**
44 * Cooked Settings Saved
45 */
46 private static $cooked_settings_saved;
47
48 /**
49 * Initialize the updates system
50 */
51 public function __construct() {
52 // Add action to check version and update settings at the end of page load.
53 add_action( 'shutdown', [__CLASS__, 'init'] );
54 }
55
56 /**
57 * Initialize the updates
58 */
59 public static function init() {
60 self::$cooked_settings_saved = get_option( 'cooked_settings_saved', false );
61 self::$current_version = COOKED_VERSION;
62 self::$previous_version = get_option( 'cooked_settings_version', '1.0.0' );
63 self::$current_pro_version = defined('COOKED_PRO_VERSION') ? COOKED_PRO_VERSION : null;
64 self::$previous_pro_version = get_option( 'cooked_pro_settings_version', '1.0.0' );
65
66 if ( !self::$cooked_settings_saved ) {
67 global $_cooked_settings;
68
69 if ( empty($_cooked_settings) ) {
70 $_cooked_settings = Cooked_Settings::get();
71 }
72
73 update_option( 'cooked_settings', $_cooked_settings );
74 self::$cooked_settings_saved = true;
75 }
76
77 // Only run updates if version has changed
78 if ( self::needs_update() ) {
79 self::run_updates();
80 }
81 }
82
83 /**
84 * Check if an update is needed
85 *
86 * @return bool True if update is needed
87 */
88 public static function needs_update() {
89 // Check both versions.
90 $cooked_version_compare = version_compare( self::$previous_version, self::$current_version );
91 $cooked_pro_version_compare = ( defined('COOKED_PRO_VERSION') && self::$current_pro_version !== null ) ? version_compare( self::$previous_pro_version, self::$current_pro_version ) : 0;
92
93 // Update if either version has changed.
94 if ( $cooked_version_compare < 0 || $cooked_pro_version_compare < 0 ) {
95 return true;
96 }
97
98 return false;
99 }
100
101 /**
102 * Run all necessary updates
103 */
104 private static function run_updates() {
105 // Store the previous version for logging
106 $old_version = self::$previous_version;
107
108 // Run version-specific updates
109 self::run_version_updates();
110
111 // Update both version numbers.
112 update_option( 'cooked_settings_version', self::$current_version );
113 if ( defined('COOKED_PRO_VERSION') ) {
114 update_option( 'cooked_pro_settings_version', self::$current_pro_version );
115 }
116
117 // Log the update
118 error_log( sprintf( 'Cooked: Updated from version %s to %s', $old_version, self::$current_version ) );
119 }
120
121 /**
122 * Run version-specific updates
123 */
124 private static function run_version_updates() {
125 $updates = self::get_version_updates();
126
127 foreach ( $updates as $version => $update_methods ) {
128 if ( version_compare( self::$previous_version, $version, '<' ) ) {
129 foreach ( $update_methods as $method ) {
130 if ( method_exists( __CLASS__, $method ) ) {
131 try {
132 call_user_func( [__CLASS__, $method] );
133 } catch ( Exception $e ) {
134 error_log( sprintf( 'Cooked: Error running update method %s: %s', $method, $e->getMessage() ) );
135 }
136 }
137 }
138 }
139 }
140 }
141
142 /**
143 * Define version-specific updates
144 *
145 * @return array Array of version updates with their corresponding methods
146 */
147 private static function get_version_updates() {
148 return apply_filters( 'cooked_version_updates', [
149 '1.9.0' => [
150 'update_rewrite_rules'
151 ],
152 '1.9.1' => [
153 'update_rewrite_rules'
154 ],
155 '1.9.2' => [
156 'update_rewrite_rules'
157 ],
158 '1.9.4' => [
159 'update_rewrite_rules'
160 ],
161 '1.9.5' => [
162 'update_rewrite_rules'
163 ],
164 '1.11.2' => [
165 'fix_recipe_line_endings',
166 'update_rewrite_rules'
167 ],
168 ]);
169 }
170
171 /**
172 * Fix line endings in existing recipes to prevent WordPress exporter/importer issues
173 *
174 * @since 1.11.2
175 */
176 private static function fix_recipe_line_endings() {
177 // Get all recipe posts
178 $recipes = get_posts([
179 'post_type' => 'cp_recipe',
180 'posts_per_page' => -1,
181 'post_status' => 'any'
182 ]);
183
184 if ( empty($recipes) ) {
185 return;
186 }
187
188 $updated_count = 0;
189
190 foreach ( $recipes as $recipe ) {
191 $recipe_settings = get_post_meta( $recipe->ID, '_recipe_settings', true );
192
193 if ( empty($recipe_settings) ) {
194 continue;
195 }
196
197 $needs_update = false;
198
199 // Fix content field
200 if ( isset( $recipe_settings['content'] ) ) {
201 $original_content = $recipe_settings['content'];
202 $recipe_settings['content'] = str_replace( ["\r\n", "\r"], "\n", $recipe_settings['content'] );
203 if ( $original_content !== $recipe_settings['content'] ) {
204 $needs_update = true;
205 }
206 }
207
208 // Fix excerpt field
209 if ( isset( $recipe_settings['excerpt'] ) ) {
210 $original_excerpt = $recipe_settings['excerpt'];
211 $recipe_settings['excerpt'] = str_replace( ["\r\n", "\r"], "\n", $recipe_settings['excerpt'] );
212 if ( $original_excerpt !== $recipe_settings['excerpt'] ) {
213 $needs_update = true;
214 }
215 }
216
217 // Fix notes field
218 if ( isset( $recipe_settings['notes'] ) ) {
219 $original_notes = $recipe_settings['notes'];
220 $recipe_settings['notes'] = str_replace( ["\r\n", "\r"], "\n", $recipe_settings['notes'] );
221 if ( $original_notes !== $recipe_settings['notes'] ) {
222 $needs_update = true;
223 }
224 }
225
226 // Fix directions content
227 if ( isset( $recipe_settings['directions'] ) && is_array( $recipe_settings['directions'] ) ) {
228 foreach ( $recipe_settings['directions'] as $key => $direction ) {
229 if ( isset( $direction['content'] ) ) {
230 $original_direction_content = $direction['content'];
231 $recipe_settings['directions'][$key]['content'] = str_replace( ["\r\n", "\r"], "\n", $direction['content'] );
232 if ( $original_direction_content !== $recipe_settings['directions'][$key]['content'] ) {
233 $needs_update = true;
234 }
235 }
236 }
237 }
238
239 // Update the recipe if changes were made
240 if ( $needs_update ) {
241 update_post_meta( $recipe->ID, '_recipe_settings', $recipe_settings );
242 $updated_count++;
243 }
244 }
245
246 // Log the update if any recipes were modified
247 if ( $updated_count > 0 ) {
248 error_log( sprintf( 'Cooked: Fixed line endings in %d recipes for WordPress exporter/importer compatibility.', $updated_count ) );
249 }
250 }
251
252 /**
253 * Update rewrite rules if needed
254 *
255 * @since 1.11.2
256 */
257 private static function update_rewrite_rules() {
258 flush_rewrite_rules();
259 error_log( 'Cooked: Flushed rewrite rules due to version update.' );
260 }
261
262 }
263