PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/attachments/classes/class.attachments.migrate.php

https://bitbucket.org/codemen_iftekhar/codemen
PHP | 235 lines | 157 code | 36 blank | 42 comment | 19 complexity | ef6e9cd43fe23acbbf56fef308daa4e3 MD5 | raw file
  1. <?php
  2. // Exit if accessed directly
  3. if( !defined( 'ABSPATH' ) ) exit;
  4. /**
  5. * Migration class for legacy Attachments data
  6. *
  7. * @since 3.1.3
  8. */
  9. class AttachmentsMigrate extends Attachments
  10. {
  11. function __construct()
  12. {
  13. parent::__construct();
  14. }
  15. /**
  16. * Migrate Attachments 1.x records to 3.0's format
  17. *
  18. * @since 3.0
  19. */
  20. function migrate( $instance = null, $title = null, $caption = null )
  21. {
  22. // sanitize
  23. if( is_null( $instance ) || empty( $instance ) || is_null( $title ) || is_null( $caption ) )
  24. return false;
  25. $instance = str_replace( '-', '_', sanitize_title( $instance ) );
  26. $title = empty( $title ) ? false : str_replace( '-', '_', sanitize_title( $title ) );
  27. $caption = empty( $caption ) ? false : str_replace( '-', '_', sanitize_title( $caption ) );
  28. // we need our deprecated functions
  29. include_once( ATTACHMENTS_DIR . '/deprecated/get-attachments.php' );
  30. $legacy_attachments_settings = get_option( 'attachments_settings' );
  31. if( $legacy_attachments_settings && is_array( $legacy_attachments_settings['post_types'] ) && count( $legacy_attachments_settings['post_types'] ) )
  32. {
  33. // we have legacy settings, so we're going to use the post types
  34. // that Attachments is currently utilizing
  35. // the keys are the actual CPT names, so we need those
  36. foreach( $legacy_attachments_settings['post_types'] as $post_type => $value )
  37. if( $value )
  38. $post_types[] = $post_type;
  39. // set up our WP_Query args to grab anything with legacy data
  40. $args = array(
  41. 'post_type' => isset( $post_types ) ? $post_types : array(),
  42. 'post_status' => 'any',
  43. 'posts_per_page' => -1,
  44. 'meta_key' => '_attachments',
  45. 'suppress_filters' => true,
  46. );
  47. $query = new WP_Query( $args );
  48. }
  49. $count = 0;
  50. // loop through each post
  51. if( $query ) { while( $query->have_posts() )
  52. {
  53. // set up postdata
  54. $query->the_post();
  55. // let's first decode our Attachments data
  56. $existing_attachments = get_post_meta( $query->post->ID, '_attachments', false );
  57. $post_attachments = array();
  58. // check to make sure we've got data
  59. if( is_array( $existing_attachments ) && count( $existing_attachments ) > 0 )
  60. {
  61. // loop through each existing attachment
  62. foreach( $existing_attachments as $attachment )
  63. {
  64. // decode and unserialize the data
  65. $data = unserialize( base64_decode( $attachment ) );
  66. array_push( $post_attachments, array(
  67. 'id' => stripslashes( $data['id'] ),
  68. 'title' => stripslashes( $data['title'] ),
  69. 'caption' => stripslashes( $data['caption'] ),
  70. 'order' => stripslashes( $data['order'] )
  71. ));
  72. }
  73. // sort attachments
  74. if( count( $post_attachments ) > 1 )
  75. {
  76. usort( $post_attachments, 'attachments_cmp' );
  77. }
  78. }
  79. // we have our Attachments entries
  80. // let's check to see if we're migrating after population has taken place
  81. $existing_attachments = get_post_meta( $query->post->ID, 'attachments', false );
  82. if( !isset( $existing_attachments[0] ) )
  83. $existing_attachments[0] = '';
  84. $existing_attachments = json_decode( $existing_attachments[0] );
  85. if( !is_object( $existing_attachments ) )
  86. $existing_attachments = new stdClass();
  87. // we'll loop through the legacy Attachments and save them in the new format
  88. foreach( $post_attachments as $legacy_attachment )
  89. {
  90. // convert to the new format
  91. $converted_attachment = array( 'id' => $legacy_attachment['id'] );
  92. // fields are technically optional so we'll add those separately
  93. // we're also going to encode them in the same way the main class does
  94. if( $title )
  95. $converted_attachment['fields'][$title] = htmlentities( stripslashes( $legacy_attachment['title'] ), ENT_QUOTES, 'UTF-8' );
  96. if( $caption )
  97. $converted_attachment['fields'][$caption] = htmlentities( stripslashes( $legacy_attachment['caption'] ), ENT_QUOTES, 'UTF-8' );
  98. // check to see if the existing Attachments have our target instance
  99. if( !isset( $existing_attachments->$instance ) )
  100. {
  101. // the instance doesn't exist so we need to create it
  102. $existing_attachments->$instance = array();
  103. }
  104. // we need to convert our array to an object
  105. $converted_attachment['fields'] = (object) $converted_attachment['fields'];
  106. $converted_attachment = (object) $converted_attachment;
  107. // append this legacy attachment to the existing instance
  108. array_push( $existing_attachments->$instance, $converted_attachment );
  109. }
  110. // we're done! let's save everything in our new format
  111. $existing_attachments = version_compare( PHP_VERSION, '5.4.0', '>=' ) ? json_encode( $existing_attachments, JSON_UNESCAPED_UNICODE ) : json_encode( $existing_attachments );
  112. // save it to the database
  113. update_post_meta( $query->post->ID, 'attachments', $existing_attachments );
  114. // increment our counter
  115. $count++;
  116. } }
  117. return $count;
  118. }
  119. /**
  120. * Step 1 of the migration process. Allows the user to define the target instance and field names.
  121. *
  122. * @since 3.2
  123. */
  124. function prepare_migration()
  125. {
  126. if( !wp_verify_nonce( $_GET['nonce'], 'attachments-migrate-1') ) wp_die( __( 'Invalid request', 'attachments' ) );
  127. ?>
  128. <h3><?php _e( 'Migration Step 1', 'attachments' ); ?></h3>
  129. <p><?php _e( "In order to migrate Attachments 1.x data, you need to set which instance and fields in version 3.0+ you'd like to use:", 'attachments' ); ?></p>
  130. <form action="options-general.php" method="get">
  131. <input type="hidden" name="page" value="attachments" />
  132. <input type="hidden" name="migrate" value="2" />
  133. <input type="hidden" name="nonce" value="<?php echo wp_create_nonce( 'attachments-migrate-2' ); ?>" />
  134. <table class="form-table">
  135. <tbody>
  136. <tr valign="top">
  137. <th scope="row">
  138. <label for="attachments-instance"><?php _e( 'Attachments 3.x Instance', 'attachments' ); ?></label>
  139. </th>
  140. <td>
  141. <input name="attachments-instance" id="attachments-instance" value="attachments" class="regular-text" />
  142. <p class="description"><?php _e( 'The instance name you would like to use in the migration. Required.', 'attachments' ); ?></p>
  143. </td>
  144. </tr>
  145. <tr valign="top">
  146. <th scope="row">
  147. <label for="attachments-title"><?php _e( 'Attachments 3.x Title', 'attachments' ); ?></label>
  148. </th>
  149. <td>
  150. <input name="attachments-title" id="attachments-title" value="title" class="regular-text" />
  151. <p class="description"><?php _e( 'The <code>Title</code> field data will be migrated to this field name in Attachments 3.x. Leave empty to disregard.', 'attachments' ); ?></p>
  152. </td>
  153. </tr>
  154. <tr valign="top">
  155. <th scope="row">
  156. <label for="attachments-caption"><?php _e( 'Attachments 3.x Caption', 'attachments' ); ?></label>
  157. </th>
  158. <td>
  159. <input name="attachments-caption" id="attachments-caption" value="caption" class="regular-text" />
  160. <p class="description"><?php _e( 'The <code>Caption</code> field data will be migrated to this field name in Attachments 3.x. Leave empty to disregard.', 'attachments' ); ?></p>
  161. </td>
  162. </tr>
  163. </tbody>
  164. </table>
  165. <p class="submit">
  166. <input type="submit" name="submit" id="submit" class="button button-primary" value="<?php esc_attr_e( 'Start Migration', 'attachments' ); ?>" />
  167. </p>
  168. </form>
  169. <?php
  170. }
  171. /**
  172. * Step 2 of the migration process. Validates migration requests and fires the migration method.
  173. *
  174. * @since 3.2
  175. */
  176. function init_migration()
  177. {
  178. if( !wp_verify_nonce( $_GET['nonce'], 'attachments-migrate-2') )
  179. wp_die( __( 'Invalid request', 'attachments' ) );
  180. $total = $this->migrate( $_GET['attachments-instance'], $_GET['attachments-title'], $_GET['attachments-caption'] );
  181. if( false == get_option( 'attachments_migrated' ) ) :
  182. ?>
  183. <h3><?php _e( 'Migration Complete!', 'attachments' ); ?></h3>
  184. <p><?php _e( 'The migration has completed.', 'attachments' ); ?> <strong><?php _e( 'Migrated', 'attachments'); ?>: <?php echo $total; ?></strong>.</p>
  185. <?php else : ?>
  186. <h3><?php _e( 'Migration has already Run!', 'attachments' ); ?></h3>
  187. <p><?php _e( 'The migration has already been run. The migration process has not been repeated.', 'attachments' ); ?></p>
  188. <?php endif;
  189. // make sure the database knows the migration has run
  190. add_option( 'attachments_migrated', true, '', 'no' );
  191. }
  192. }