@@ -173,13 +173,13 @@ public static function instance() {
173
173
174
174
add_action ( 'optml_start_processing_images ' , [ self ::$ instance , 'start_processing_images ' ], 10 , 5 );
175
175
add_action (
176
- 'optml_start_processing_images_by_id ' ,
176
+ 'optml_move_images_by_id ' ,
177
177
[
178
178
self ::$ instance ,
179
- 'start_processing_images_by_id ' ,
179
+ 'move_single_image ' ,
180
180
],
181
181
10 ,
182
- 4
182
+ 2
183
183
);
184
184
add_action ( 'init ' , [ self ::$ instance , 'maybe_reschedule ' ] );
185
185
if ( self ::$ is_legacy_install === null ) {
@@ -212,7 +212,7 @@ public function maybe_reschedule() {
212
212
return ;
213
213
}
214
214
self ::$ instance ->logger ->add_log ( $ transfer_type , 'Cron missed, attempt to reschedule. ' );
215
- self ::get_image_count ( $ transfer_type , false );
215
+ self ::move_images ( $ transfer_type , false );
216
216
}
217
217
218
218
/**
@@ -758,39 +758,17 @@ public function add_inline_media_action( $actions, $post ) {
758
758
if ( wp_check_filetype ( $ file , Optml_Config::$ all_extensions )['ext ' ] === false || ! current_user_can ( 'delete_post ' , $ post ->ID ) ) {
759
759
return $ actions ;
760
760
}
761
- if ( ! self ::is_uploaded_image ( $ file ) ) {
762
- $ upload_action_url = add_query_arg (
763
- [
764
- 'page ' => 'optimole ' ,
765
- 'optimole_action ' => 'offload_images ' ,
766
- '0 ' => $ post ->ID ,
767
- ],
768
- 'admin.php '
769
- );
770
-
771
- $ actions ['offload_images ' ] = sprintf (
772
- '<a href="%s" aria-label="%s">%s</a> ' ,
773
- $ upload_action_url ,
774
- esc_attr__ ( 'Offload to Optimole ' , 'optimole-wp ' ),
775
- esc_html__ ( 'Offload to Optimole ' , 'optimole-wp ' )
776
- );
777
- }
778
- if ( self ::is_uploaded_image ( $ file ) ) {
779
- $ rollback_action_url = add_query_arg (
780
- [
781
- 'page ' => 'optimole ' ,
782
- 'optimole_action ' => 'rollback_images ' ,
783
- '0 ' => $ post ->ID ,
784
- ],
785
- 'admin.php '
786
- );
787
- $ actions ['rollback_images ' ] = sprintf (
788
- '<a href="%s" aria-label="%s">%s</a> ' ,
789
- $ rollback_action_url ,
790
- esc_attr__ ( 'Restore image to media library ' , 'optimole-wp ' ),
791
- esc_html__ ( 'Restore image to media library ' , 'optimole-wp ' )
792
- );
793
- }
761
+ $ actions ['optml_actions ' ] = sprintf (
762
+ '<span class="spinner"></span><a class="move-image-optml %s" data-action="offload_image" href="#" aria-label="%s" data-id="%s">%s</a><a class="move-image-optml %s" data-action="rollback_image" href="#" aria-label="%s" data-id="%s">%s</a> ' ,
763
+ self ::is_uploaded_image ( $ file ) ? 'hidden ' : '' ,
764
+ esc_attr__ ( 'Offload to Optimole ' , 'optimole-wp ' ),
765
+ $ post ->ID ,
766
+ esc_html__ ( 'Offload to Optimole ' , 'optimole-wp ' ),
767
+ self ::is_uploaded_image ( $ file ) ? '' : 'hidden ' ,
768
+ esc_attr__ ( 'Restore image to media library ' , 'optimole-wp ' ),
769
+ $ post ->ID ,
770
+ esc_html__ ( 'Restore image to media library ' , 'optimole-wp ' )
771
+ );
794
772
795
773
return $ actions ;
796
774
}
@@ -1868,21 +1846,16 @@ public static function get_process_meta() {
1868
1846
*
1869
1847
* @param string $action The actions for which to get the number of images.
1870
1848
* @param bool $refresh Whether to refresh the cron or not.
1871
- * @param array $images The images to process.
1872
1849
*
1873
1850
* @return array Image count and Cron status.
1874
1851
*/
1875
- public static function get_image_count ( $ action , $ refresh, $ images = [] ) {
1852
+ public static function move_images ( $ action , $ refresh ) {
1876
1853
$ option = 'offload_images ' === $ action ? 'offloading_status ' : 'rollback_status ' ;
1877
1854
$ count = 0 ;
1878
1855
$ step = 0 ;
1879
1856
$ batch = apply_filters ( 'optimole_offload_batch ' , 20 ); // Reduce this to smaller if we have memory issues during testing.
1880
1857
1881
- if ( empty ( $ images ) ) {
1882
- $ count = Optml_Media_Offload::number_of_images_and_pages ( $ action );
1883
- } else {
1884
- $ count = Optml_Media_Offload::number_of_images_by_ids ( $ action , $ images );
1885
- }
1858
+ $ count = Optml_Media_Offload::number_of_images_and_pages ( $ action );
1886
1859
1887
1860
$ possible_batch = ceil ( $ count / 10 );
1888
1861
@@ -1901,15 +1874,12 @@ public static function get_image_count( $action, $refresh, $images = [] ) {
1901
1874
$ in_progress = false ;
1902
1875
}
1903
1876
$ type = 'offload_images ' === $ action ? 'offload ' : 'rollback ' ;
1904
- // We save the status if this ia multi step transfer.
1905
- if ( empty ( $ images ) ) {
1906
- self ::$ instance ->settings ->update ( 'transfer_status ' , $ action );
1907
- }
1877
+ self ::$ instance ->settings ->update ( 'transfer_status ' , $ action );
1908
1878
if ( false === $ refresh ) {
1909
1879
// We check also the alternative action to avoid doing both in the same time and disable the running one.
1910
1880
$ in_progress_b = self ::$ instance ->settings ->get ( 'rollback_images ' === $ action ? 'offloading_status ' : 'rollback_status ' ) !== 'disabled ' ;
1911
1881
// We do this only if there is a mass action in progress, not individual ones.
1912
- if ( $ in_progress_b && empty ( $ images ) ) {
1882
+ if ( $ in_progress_b ) {
1913
1883
// We stop the oposite action from going any further.
1914
1884
self ::$ instance ->settings ->update ( 'rollback_images ' === $ action ? 'offloading_status ' : 'rollback_status ' , 'disabled ' );
1915
1885
}
@@ -1927,32 +1897,18 @@ public static function get_image_count( $action, $refresh, $images = [] ) {
1927
1897
'action ' => $ type ,
1928
1898
];
1929
1899
}
1930
-
1931
- if ( empty ( $ images ) ) {
1932
- $ total = ceil ( $ count / $ batch );
1933
- self ::schedule_action (
1934
- time (),
1935
- 'optml_start_processing_images ' ,
1936
- [
1937
- $ action ,
1938
- $ batch ,
1939
- 1 ,
1940
- $ total ,
1941
- $ step ,
1942
- ]
1943
- );
1944
- } else {
1945
- self ::schedule_action (
1946
- time (),
1947
- 'optml_start_processing_images_by_id ' ,
1948
- [
1949
- $ action ,
1950
- $ batch ,
1951
- 1 ,
1952
- $ images ,
1953
- ]
1954
- );
1955
- }
1900
+ $ total = ceil ( $ count / $ batch );
1901
+ self ::schedule_action (
1902
+ time (),
1903
+ 'optml_start_processing_images ' ,
1904
+ [
1905
+ $ action ,
1906
+ $ batch ,
1907
+ 1 ,
1908
+ $ total ,
1909
+ $ step ,
1910
+ ]
1911
+ );
1956
1912
}
1957
1913
1958
1914
$ response = [
@@ -2016,100 +1972,50 @@ public static function is_scheduled( $hook ) {
2016
1972
* Start Processing Images by IDs
2017
1973
*
2018
1974
* @param string $action The action for which to get the number of images.
2019
- * @param int $batch The batch of images to process.
2020
- * @param int $page The page of images to process.
2021
- * @param array $image_ids The images to process.
1975
+ * @param int $id The images to process.
2022
1976
*
1977
+ * @throws Exception If there is an error.
2023
1978
* @return void
2024
1979
*/
2025
- public function start_processing_images_by_id ( $ action , $ batch , $ page , $ image_ids = [] ) {
2026
- $ option = 'offload_images ' === $ action ? 'offloading_status ' : 'rollback_status ' ;
2027
- $ type = 'offload_images ' === $ action ? Optml_Logger::LOG_TYPE_OFFLOAD : Optml_Logger::LOG_TYPE_ROLLBACK ;
2028
-
2029
- if ( self ::$ instance ->settings ->get ( $ option ) === 'disabled ' ) {
2030
- return ;
2031
- }
2032
-
1980
+ public function move_single_image ( $ action , $ id ) {
2033
1981
set_time_limit ( 0 );
2034
1982
2035
1983
// Only use the legacy offloaded attachments to query the pages that need to be updated.
2036
1984
// We can be confident that these IDs are already marked as offloaded.
2037
- $ legacy_offloaded = array_filter (
2038
- $ image_ids ,
2039
- function ( $ id ) {
2040
- return ! $ this ->is_new_offloaded_attachment ( $ id );
2041
- }
2042
- );
2043
-
2044
- // On the new mechanism, we don't update posts anymore when offloading.
2045
- $ page_in = $ action === 'offload_images ' ? [] : Optml_Media_Offload::get_posts_by_image_ids ( $ action , $ legacy_offloaded , $ batch , $ page );
2046
-
2047
- if ( empty ( $ image_ids ) && empty ( $ page_in ) && empty ( $ legacy_offloaded ) ) {
2048
- $ meta = self ::get_process_meta ();
2049
- self ::$ instance ->logger ->add_log ( $ type , 'Process finished with ' . $ meta ['count ' ] . ' items in ' . $ meta ['time_passed ' ] . ' minutes. ' );
2050
-
2051
- self ::$ instance ->settings ->update ( $ option , 'disabled ' );
2052
-
2053
- return ;
2054
- }
2055
-
2056
- try {
2057
- // This will be 0 in the case of offloading now.
2058
- if ( $ action === 'rollback_images ' && 0 !== count ( $ page_in ) ) {
2059
- $ to_update = Optml_Media_Offload::instance ()->update_content ( $ page , $ action , $ batch , $ page_in );
2060
-
1985
+ $ legacy_offloaded = ! $ this ->is_new_offloaded_attachment ( $ id );
1986
+ $ page_in = [];
1987
+ if ( $ legacy_offloaded ) {
1988
+ $ page_in = $ action === 'offload_images ' ? [] : Optml_Media_Offload::get_posts_by_image_ids ( $ action , [ $ id ] );
1989
+ }
1990
+ // This will be 0 in the case of offloading now.
1991
+ if ( $ action === 'rollback_images ' && 0 !== count ( $ page_in ) ) {
1992
+ $ page = 0 ;
1993
+ do {
1994
+ $ to_update = Optml_Media_Offload::instance ()->update_content ( $ page , $ action , 100 , $ page_in );
2061
1995
if ( isset ( $ to_update ['page ' ] ) ) {
2062
1996
if ( isset ( $ to_update ['imagesToUpdate ' ] ) && count ( $ to_update ['imagesToUpdate ' ] ) ) {
2063
1997
foreach ( $ to_update ['imagesToUpdate ' ] as $ post_id => $ images ) {
2064
- if ( ! empty ( $ image_ids ) ) {
2065
- $ images = array_intersect ( $ images , $ image_ids );
2066
- }
2067
-
1998
+ $ images = array_intersect ( $ images , [ $ id ] );
2068
1999
if ( empty ( $ images ) ) {
2069
2000
continue ;
2070
2001
}
2071
-
2072
2002
Optml_Media_Offload::instance ()->rollback_and_update_images ( $ images );
2073
2003
Optml_Media_Offload::instance ()->update_page ( $ post_id );
2074
2004
}
2075
2005
}
2076
2006
}
2077
-
2078
2007
$ page = $ page + 1 ;
2079
- } else {
2080
- // From $image_ids get the number as per $batch and save it in $page_in and update $images with the remaining images.
2081
- $ images = array_slice ( $ image_ids , 0 , $ batch );
2082
- $ image_ids = array_slice ( $ image_ids , $ batch );
2083
- $ action === 'rollback_images ' ?
2084
- Optml_Media_Offload::instance ()->rollback_images ( $ batch , $ images ) :
2085
- Optml_Media_Offload::instance ()->upload_images ( $ batch , $ images );
2086
- }
2008
+ } while ( ! empty ( $ to_update ['imagesToUpdate ' ] ) );
2087
2009
2088
- self ::schedule_action (
2089
- time (),
2090
- 'optml_start_processing_images_by_id ' ,
2091
- [
2092
- $ action ,
2093
- $ batch ,
2094
- $ page ,
2095
- $ image_ids ,
2096
- ]
2097
- );
2098
- } catch ( Exception $ e ) {
2099
- // Reschedule the cron to run again after a delay. Sometimes memory limit is exhausted.
2100
- $ delay_in_seconds = 10 ;
2101
- self ::$ instance ->logger ->add_log ( $ type , $ e ->getMessage () );
2102
-
2103
- self ::schedule_action (
2104
- time () + $ delay_in_seconds ,
2105
- 'optml_start_processing_images_by_id ' ,
2106
- [
2107
- $ action ,
2108
- $ batch ,
2109
- $ page ,
2110
- $ image_ids ,
2111
- ]
2112
- );
2010
+ } else {
2011
+ $ action === 'rollback_images ' ?
2012
+ Optml_Media_Offload::instance ()->rollback_images ( 1 , [ $ id ] ) :
2013
+ Optml_Media_Offload::instance ()->upload_images ( 1 , [ $ id ] );
2014
+ }
2015
+ if ( empty ( $ page_in ) && $ legacy_offloaded === false ) {
2016
+ $ meta = self ::get_process_meta ();
2017
+ self ::$ instance ->logger ->add_log ( $ action , 'Process finished with ' . $ meta ['count ' ] . ' items in ' . $ meta ['time_passed ' ] . ' minutes. ' );
2018
+ return ;
2113
2019
}
2114
2020
}
2115
2021
0 commit comments