| 1 |
<?php |
| 2 |
|
| 3 |
namespace FileBird\Controller\Attachment; |
| 4 |
|
| 5 |
class SizeMeta { |
| 6 |
private static $instance = null; |
| 7 |
private $optionState = 'fbv_generate_attachment_size'; |
| 8 |
private $updatedState = 'fbv_updated_attachment_size'; |
| 9 |
private $processState = array( |
| 10 |
'isProcessing' => false, |
| 11 |
'total' => null, |
| 12 |
'current' => 0, |
| 13 |
'ids' => array(), |
| 14 |
); |
| 15 |
|
| 16 |
public $meta_key = 'fb_filesize'; |
| 17 |
|
| 18 |
public static function getInstance() { |
| 19 |
if ( null == self::$instance ) { |
| 20 |
self::$instance = new self(); |
| 21 |
} |
| 22 |
return self::$instance; |
| 23 |
} |
| 24 |
|
| 25 |
public function doHooks() { |
| 26 |
add_action( 'added_post_meta', array( $this, 'added_post_meta' ), 10, 4 ); |
| 27 |
} |
| 28 |
|
| 29 |
public function prepare( $state = array() ) { |
| 30 |
$query = new \WP_Query( |
| 31 |
array( |
| 32 |
'post_type' => 'attachment', |
| 33 |
'posts_per_page' => -1, |
| 34 |
'post_status' => 'inherit', |
| 35 |
'fields' => 'ids', |
| 36 |
) |
| 37 |
); |
| 38 |
|
| 39 |
$state['total'] = $query->found_posts; |
| 40 |
$state['ids'] = $query->posts; |
| 41 |
$state['isProcessing'] = true; |
| 42 |
$this->update_state( $state ); |
| 43 |
} |
| 44 |
|
| 45 |
public function processing( $generateAll = false ) { |
| 46 |
$state = $this->get_state(); |
| 47 |
$ids = $state['ids']; |
| 48 |
$updatedIds = get_option( $this->updatedState, array() ); |
| 49 |
$updatedCounter = 0; |
| 50 |
if ( ! $generateAll ) { |
| 51 |
$ids = array_diff( $ids, $updatedIds ); |
| 52 |
if ( empty( $ids ) ) { |
| 53 |
$updatedCounter = count( $state['ids'] ); |
| 54 |
} else { |
| 55 |
$updatedCounter = count( $updatedIds ); |
| 56 |
} |
| 57 |
} |
| 58 |
foreach ( $ids as $index => $post_id ) { |
| 59 |
$state['current'] = $index + 1; |
| 60 |
// usleep( 10 * 1000 ); |
| 61 |
$this->generate_meta_size( $post_id ); |
| 62 |
$this->update_state( $state ); |
| 63 |
} |
| 64 |
|
| 65 |
$state['isProcessing'] = false; |
| 66 |
$state['current'] = $state['current'] + $updatedCounter; |
| 67 |
update_option( $this->updatedState, $state['ids'] ); |
| 68 |
$this->update_state( $state ); |
| 69 |
} |
| 70 |
|
| 71 |
public function get_state() { |
| 72 |
return get_option( $this->optionState, $this->processState ); |
| 73 |
} |
| 74 |
|
| 75 |
public function update_state( $new_state = array() ) { |
| 76 |
update_option( $this->optionState, $new_state, false ); |
| 77 |
} |
| 78 |
|
| 79 |
public function cancel_process() { |
| 80 |
delete_option( $this->optionState ); |
| 81 |
} |
| 82 |
|
| 83 |
public function apiCallback() { |
| 84 |
$generateAll = true; |
| 85 |
$state = $this->get_state(); |
| 86 |
if ( $state['current'] >= intval( $state['total'] ) ) { |
| 87 |
$this->cancel_process(); |
| 88 |
$state = $this->get_state(); |
| 89 |
} |
| 90 |
|
| 91 |
if ( ! $state['isProcessing'] ) { |
| 92 |
if ( is_null( $state['total'] ) ) { |
| 93 |
$this->prepare( $state ); |
| 94 |
} |
| 95 |
if ( $state['current'] !== $state['total'] ) { |
| 96 |
$this->processing( $generateAll ); |
| 97 |
$this->cancel_process(); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
return $this->get_state(); |
| 102 |
} |
| 103 |
|
| 104 |
public function generate_meta_size( $post_id ) { |
| 105 |
$is_file_exist = \file_exists( \get_attached_file( $post_id ) ); |
| 106 |
$file_size = ''; |
| 107 |
if ( $is_file_exist ) { |
| 108 |
$file_size = \filesize( \get_attached_file( $post_id ) ); |
| 109 |
update_post_meta( $post_id, $this->meta_key, $file_size ); |
| 110 |
} |
| 111 |
return $file_size; |
| 112 |
} |
| 113 |
|
| 114 |
public function added_post_meta( $meta_id, $post_id, $meta_key, $meta_value ) { |
| 115 |
if ( '_wp_attachment_metadata' === $meta_key ) { |
| 116 |
$this->generate_meta_size( $post_id ); |
| 117 |
} |
| 118 |
} |
| 119 |
} |