PluginProbe
Sharing Image / 3.3
Sharing Image v3.3
3.10 trunk 2.0 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.16 2.0.17 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 3.0 3.1 3.2 3.3 All 29 releases
sharing-image / classes / class-migrations.php

class-migrations.php in Sharing Image 3.3, at classes/class-migrations.php

244 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Process all migration on plugin update.
4 *
5 * @package sharing-image
6 * @author Anton Lukin
7 */
8
9 namespace Sharing_Image;
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 die;
13 }
14
15 /**
16 * Migrations class.
17 *
18 * @class Migrations
19 */
20 class Migrations {
21 /**
22 * Sharing Image migrations options name.
23 *
24 * @var string
25 */
26 const OPTION_MIGRATION = 'sharing_image_migration';
27
28 /**
29 * Init class actions and filters.
30 */
31 public static function init() {
32 add_action( 'admin_init', array( __CLASS__, 'migrate_templates' ) );
33 add_action( 'load-post.php', array( __CLASS__, 'migrate_post_fieldset' ) );
34 }
35
36 /**
37 * Migrate database templates to new format.
38 */
39 public static function migrate_templates() {
40 $legacy = get_option( 'sharing_image_templates' );
41
42 if ( empty( $legacy ) ) {
43 return;
44 }
45
46 $migration = get_option( self::OPTION_MIGRATION, null );
47
48 if ( ! is_null( $migration ) ) {
49 return;
50 }
51
52 self::add_templates_index( $legacy );
53
54 delete_option( 'sharing_image_templates' );
55 }
56
57 /**
58 * Migrate fieldset in post or term editor screen.
59 */
60 public static function migrate_post_fieldset() {
61 // phpcs:ignore WordPress.Security.NonceVerification
62 $post_id = isset( $_GET['post'] ) ? absint( $_GET['post'] ) : 0;
63
64 if ( empty( $post_id ) ) {
65 return;
66 }
67
68 $source = get_post_meta( $post_id, Widget::META_SOURCE, true );
69
70 if ( empty( $source['fieldset'] ) ) {
71 return;
72 }
73
74 $migration = get_option( self::OPTION_MIGRATION, array() );
75
76 if ( empty( $migration['layers'] ) ) {
77 $migration['layers'] = array();
78 }
79
80 $source = self::update_source_template( $source, $migration );
81
82 $prepared = array();
83 $fieldset = $source['fieldset'];
84
85 foreach ( $migration['layers'] as $key => $layer ) {
86 list( $field, $id, $pos ) = array_pad( explode( '-', $layer ), 3, null );
87
88 if ( is_null( $pos ) && ! empty( $fieldset[ $id ][ $field ] ) ) {
89 $prepared[ $key ] = $fieldset[ $id ][ $field ];
90 }
91
92 if ( ! is_null( $pos ) && ! empty( $fieldset[ $id ][ $field ][ $pos ] ) ) {
93 $prepared[ $key ] = $fieldset[ $id ][ $field ][ $pos ];
94 }
95 }
96
97 unset( $source['fieldset'] );
98
99 update_post_meta( $post_id, Widget::META_SOURCE, $source );
100 update_post_meta( $post_id, Widget::META_FIELDSET, $prepared );
101 }
102
103 /**
104 * Update source template from id to index.
105 *
106 * @param array $source Source post meta data.
107 * @param array $migration Migration options.
108 *
109 * @return array Update source post meta data.
110 */
111 private static function update_source_template( $source, $migration ) {
112 if ( empty( $migration['templates'] ) ) {
113 return $source;
114 }
115
116 if ( ! isset( $source['template'] ) ) {
117 return $source;
118 }
119
120 $id = $source['template'];
121
122 if ( isset( $migration['templates'][ $id ] ) ) {
123 $source['template'] = $migration['templates'][ $id ];
124 }
125
126 return $source;
127 }
128
129 /**
130 * Add keys to templates and layers for legacy settings.
131 *
132 * @param array $legacy Legacy templates data.
133 */
134 private static function add_templates_index( $legacy ) {
135 $migration = array();
136 $templates = array();
137
138 foreach ( $legacy as $id => $template ) {
139 $index = Templates::create_unique_index();
140
141 $migration['templates'][ $id ] = $index;
142
143 if ( ! empty( $template['layers'] ) ) {
144 $template['layers'] = self::replace_layers_key( $template['layers'], $id, $migration );
145 }
146
147 $template = self::create_background_layer( $template, $id, $migration );
148
149 // Add template with a new key.
150 $templates[ $index ] = $template;
151 }
152
153 update_option( self::OPTION_MIGRATION, $migration );
154 update_option( Templates::OPTION_TEMPLATES, $templates );
155 }
156
157 /**
158 * Replace key for the template layer.
159 *
160 * @param array $layers List of template layers.
161 * @param int $id Legacy template id.
162 * @param array $migration Migration options. Passed by reference.
163 *
164 * @return array Updated list of template layers.
165 */
166 private static function replace_layers_key( $layers, $id, &$migration ) {
167 foreach ( $layers as $pos => $layer ) {
168 $key = Templates::generate_layer_key();
169
170 // Add layer with a new key.
171 $layers[ $key ] = $layer;
172
173 $migration['layers'][ $key ] = 'captions-' . $id . '-' . $pos;
174
175 unset( $layers[ $pos ] );
176 }
177
178 $layers = array_reverse( $layers );
179
180 return $layers;
181 }
182
183 /**
184 * Create new background layer from template data.
185 *
186 * @param array $template Template data.
187 * @param int $id Legacy template id.
188 * @param array $migration Migration options. Passed by reference.
189 *
190 * @return array Updated template.
191 */
192 private static function create_background_layer( $template, $id, &$migration ) {
193 if ( empty( $template['background'] ) ) {
194 return $template;
195 }
196
197 $background = $template['background'];
198
199 if ( ! in_array( $background, array( 'dynamic', 'permanent' ), true ) ) {
200 return $template;
201 }
202
203 $key = Templates::generate_layer_key();
204
205 if ( ! isset( $template['layers'] ) ) {
206 $template['layers'] = array();
207 }
208
209 $layer = array(
210 'type' => 'image',
211 'x' => 0,
212 'y' => 0,
213 'width' => 1200,
214 'height' => 630,
215 );
216
217 if ( 'dynamic' === $background ) {
218 $layer['dynamic'] = 'dynamic';
219 $layer['preset'] = 'featured';
220 }
221
222 if ( ! empty( $template['attachment'] ) ) {
223 $layer['attachment'] = $template['attachment'];
224 }
225
226 if ( ! empty( $template['width'] ) ) {
227 $layer['width'] = $template['width'];
228 }
229
230 if ( ! empty( $template['height'] ) ) {
231 $layer['height'] = $template['height'];
232 }
233
234 $migration['layers'][ $key ] = 'attachment-' . $id;
235
236 // Add new layer to first layers position.
237 $template['layers'] = array_merge( array( $key => $layer ), $template['layers'] );
238
239 unset( $template['attachment'] );
240
241 return $template;
242 }
243 }
244