/wp-content/plugins/ewww-image-optimizer/classes/class-ewwwio-relative-migration.php

https://github.com/livinglab/openlab · PHP · 202 lines · 128 code · 14 blank · 60 comment · 18 complexity · c8bee0484332931b6c46a7e0ecfefe6f MD5 · raw file

  1. <?php
  2. /**
  3. * Class file for EWWWIO_Relative_Migration
  4. *
  5. * Performs the migration from storing absolute paths to using relative paths in the ewwwio_images table.
  6. *
  7. * @link https://ewww.io
  8. * @package EWWW_Image_Optimizer
  9. * @since 4.5.2
  10. */
  11. if ( ! defined( 'ABSPATH' ) ) {
  12. exit;
  13. }
  14. /**
  15. * Migrates absolute paths to relative paths (once).
  16. */
  17. class EWWWIO_Relative_Migration {
  18. /**
  19. * The offset/position in the database.
  20. *
  21. * @var int $id
  22. */
  23. private $offset = 0;
  24. /**
  25. * The time when we started processing.
  26. *
  27. * @var int $started
  28. */
  29. private $started;
  30. /**
  31. * Sets up the the migration.
  32. */
  33. function __construct() {
  34. if ( 'done' === get_option( 'ewww_image_optimizer_relative_migration_status' ) ) {
  35. return;
  36. }
  37. if ( ! $this->table_exists() ) {
  38. $this->unschedule();
  39. update_option( 'ewww_image_optimizer_relative_migration_status', 'done' );
  40. delete_option( 'ewww_image_optimizer_relative_migration_offset' );
  41. }
  42. if ( ! get_option( 'ewww_image_optimizer_relative_migration_status' ) ) {
  43. update_option( 'ewww_image_optimizer_relative_migration_status', 'started' );
  44. }
  45. $this->maybe_schedule();
  46. }
  47. /**
  48. * Check to see if the ewwwio_images table actually exists.
  49. *
  50. * @return bool True if does, false if it don't.
  51. */
  52. private function table_exists() {
  53. global $wpdb;
  54. return $wpdb->get_var( "SHOW TABLES LIKE '$wpdb->ewwwio_images'" ) === $wpdb->ewwwio_images;
  55. }
  56. /**
  57. * Retrieves a batch of records based on the current offset.
  58. */
  59. private function get_records() {
  60. ewwwio_debug_message( '<b>' . __METHOD__ . '()</b>' );
  61. global $wpdb;
  62. if ( strpos( $wpdb->charset, 'utf8' ) === false ) {
  63. ewww_image_optimizer_db_init();
  64. global $ewwwdb;
  65. } else {
  66. $ewwwdb = $wpdb;
  67. }
  68. $query = "SELECT id,path,updated FROM $ewwwdb->ewwwio_images WHERE pending=0 AND image_size > 0 ORDER BY id DESC LIMIT $this->offset,500";
  69. $records = $ewwwdb->get_results( $query, ARRAY_A );
  70. $this->offset += 500;
  71. if ( is_array( $records ) ) {
  72. return $records;
  73. }
  74. return array();
  75. }
  76. /**
  77. * Called via wp_cron to initiate the migration effort.
  78. */
  79. public function migrate() {
  80. ewwwio_debug_message( '<b>' . __METHOD__ . '()</b>' );
  81. $this->started = time();
  82. $this->offset = (int) get_option( 'ewww_image_optimizer_relative_migration_offset' );
  83. $records = $this->get_records();
  84. ewwwio_debug_message( 'starting at ' . gmdate( 'Y-m-d H:i:s', $this->started ) . " with offset $this->offset" );
  85. while ( ! empty( $records ) ) {
  86. foreach ( $records as $record ) {
  87. if ( $this->already_migrated( $record['path'] ) ) {
  88. ewwwio_debug_message( 'already migrated' );
  89. continue;
  90. }
  91. // Relativize the path, and store it back in the db.
  92. $relative_path = ewww_image_optimizer_relativize_path( $record['path'] );
  93. if ( $record['path'] !== $relative_path ) {
  94. $record['path'] = $relative_path;
  95. $this->update_relative_record( $record );
  96. }
  97. }
  98. if ( time() - $this->started > 20 ) {
  99. update_option( 'ewww_image_optimizer_relative_migration_offset', $this->offset, false );
  100. return;
  101. }
  102. $records = $this->get_records();
  103. }
  104. $this->unschedule();
  105. update_option( 'ewww_image_optimizer_relative_migration_status', 'done' );
  106. delete_option( 'ewww_image_optimizer_relative_migration_offset' );
  107. }
  108. /**
  109. * Checks to see if the record already has been migrated.
  110. *
  111. * @param string $path Path of file as retrieved from database.
  112. */
  113. private function already_migrated( $path ) {
  114. if ( strpos( $path, 'EWWW_IMAGE_OPTIMIZER_RELATIVE_FOLDER' ) === 0 ) {
  115. return true;
  116. }
  117. if ( strpos( $path, 'ABSPATH' ) === 0 ) {
  118. return true;
  119. }
  120. if ( strpos( $path, 'WP_CONTENT_DIR' ) === 0 ) {
  121. return true;
  122. }
  123. return false;
  124. }
  125. /**
  126. * Updates the db record with the relativized path.
  127. *
  128. * @param array $record Includes a relative path, the ID, and the updated timestamp.
  129. */
  130. private function update_relative_record( $record ) {
  131. ewwwio_debug_message( '<b>' . __METHOD__ . '()</b>' );
  132. global $wpdb;
  133. if ( strpos( $wpdb->charset, 'utf8' ) === false ) {
  134. ewww_image_optimizer_db_init();
  135. global $ewwwdb;
  136. } else {
  137. $ewwwdb = $wpdb;
  138. }
  139. $ewwwdb->update(
  140. $ewwwdb->ewwwio_images,
  141. array(
  142. 'path' => $record['path'],
  143. 'updated' => $record['updated'],
  144. ),
  145. array(
  146. 'id' => $record['id'],
  147. )
  148. );
  149. }
  150. /**
  151. * Schedule the migration.
  152. */
  153. private function maybe_schedule() {
  154. ewwwio_debug_message( '<b>' . __METHOD__ . '()</b>' );
  155. // Create 5 minute wp_cron schedule.
  156. add_filter( 'cron_schedules', array( $this, 'add_migration_schedule' ) );
  157. add_action( 'ewww_image_optimizer_relative_migration', array( $this, 'migrate' ) );
  158. // Schedule migration function.
  159. if ( ! wp_next_scheduled( 'ewww_image_optimizer_relative_migration' ) ) {
  160. ewwwio_debug_message( 'scheduling migration' );
  161. wp_schedule_event( time(), 'ewwwio_relative_migration_interval', 'ewww_image_optimizer_relative_migration' );
  162. }
  163. }
  164. /**
  165. * Clean up the scheduled event.
  166. */
  167. private function unschedule() {
  168. ewwwio_debug_message( '<b>' . __METHOD__ . '()</b>' );
  169. $timestamp = wp_next_scheduled( 'ewww_image_optimizer_relative_migration' );
  170. if ( $timestamp ) {
  171. wp_unschedule_event( $timestamp, 'ewww_image_optimizer_relative_migration' );
  172. }
  173. }
  174. /**
  175. * Adds a custom cron schedule: every 5 minutes.
  176. *
  177. * @param array $schedules An array of custom cron schedules.
  178. */
  179. public function add_migration_schedule( $schedules ) {
  180. ewwwio_debug_message( '<b>' . __METHOD__ . '()</b>' );
  181. $schedules['ewwwio_relative_migration_interval'] = array(
  182. 'interval' => MINUTE_IN_SECONDS * 5,
  183. 'display' => 'Every 5 Minutes until complete',
  184. );
  185. return $schedules;
  186. }
  187. }
  188. new EWWWIO_Relative_Migration();