PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.84
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.84
5.5.84 5.5.83 5.5.82 5.5.81 5.5.80 5.5.79 5.5.77 5.5.76 5.5.75 5.5.73 5.5.72 5.5.22 5.5.23 5.5.29 5.5.3 5.5.31 5.5.32 5.5.34 5.5.35 5.5.36 5.5.37 5.5.4 5.5.40 5.5.41 5.5.42 All 160 releases
wp-data-access / WPDataAccess / Utilities / WPDA_Export_Scheduler.php

WPDA_Export_Scheduler.php in WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards 5.5.84, at WPDataAccess/Utilities/WPDA_Export_Scheduler.php

136 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPDataAccess\Utilities;
4
5 use WPDataAccess\Drive\WPDA_Drives;
6 use WPDataAccess\WPDA;
7 class WPDA_Export_Scheduler {
8 const SCHEDULER_HOOK_NAME = 'wpda_run_scheduled_export';
9
10 public static function wpda_cron_events() {
11 $cron = get_option( 'cron' );
12 if ( !is_array( $cron ) ) {
13 $cron = array();
14 }
15 $cron_wpda = array_filter( $cron, function ( $item ) {
16 if ( isset( $item[self::SCHEDULER_HOOK_NAME] ) ) {
17 return true;
18 }
19 } );
20 return $cron_wpda;
21 }
22
23 public static function run_scheduled_export( $args ) {
24 if ( isset(
25 $args['name'],
26 $args['params']['dbs'],
27 $args['params']['tbl'],
28 $args['params']['drv'],
29 $args['params']['eml'],
30 $args['params']['fkp']
31 ) ) {
32 $name = $args['name'];
33 $dbs = $args['params']['dbs'];
34 $tbl = $args['params']['tbl'];
35 $drv = $args['params']['drv'];
36 $eml = $args['params']['eml'];
37 $fkp = $args['params']['fkp'];
38 $drive = WPDA_Drives::get_drive( $drv, true );
39 if ( false !== $drive ) {
40 $refresh_token = $drive->refresh_token();
41 if ( null === $refresh_token || false === $refresh_token ) {
42 // Cannot refresh token
43 WPDA::wpda_log_wp_error( "Export {$name} failed (could not refresh token)" );
44 self::mail( $eml, "Export {$name} failed (could not refresh token)" );
45 } else {
46 if ( false === $drive->connect() ) {
47 // Cannot connect
48 WPDA::wpda_log_wp_error( "Export {$name} failed (could not connect)" );
49 self::mail( $eml, "Export {$name} failed (could not connect)" );
50 } else {
51 // Export tables to temporary file
52 $temporary_file = tmpfile();
53 $wpda_export = new WPDA_Export_Sql();
54 $wpda_export->set_output_stream( $temporary_file );
55 $tables = ( str_contains( $tbl, ',' ) !== false ? explode( ',', $tbl ) : $tbl );
56 $wpda_export->export_with_arguments(
57 'on',
58 'on',
59 'on',
60 $dbs,
61 $tables,
62 'table'
63 );
64 if ( WPDA_Remote_Call::max_size() < filesize( stream_get_meta_data( $temporary_file )['uri'] ) && !extension_loaded( 'zip' ) ) {
65 // File is too big to send in one chunk and zip archive is not installed.
66 WPDA::wpda_log_wp_error( "Export {$name} failed (ZipArchive not installed)" );
67 self::mail( $eml, "Export {$name} failed (ZipArchive not installed)" );
68 } else {
69 $separator = '-';
70 $file_ext = '.sql';
71 $remote_file = $name . $separator . gmdate( 'YmdHis' ) . $file_ext;
72 if ( extension_loaded( 'zip' ) ) {
73 $zip = new \ZipArchive();
74 $zipfile = sys_get_temp_dir() . '/' . $remote_file . '.zip';
75 if ( !$zip->open( $zipfile, \ZipArchive::CREATE | \ZipArchive::OVERWRITE ) ) {
76 WPDA::wpda_log_wp_error( "Export {$name} failed (could not create ZIP file)" );
77 self::mail( $eml, "Export {$name} failed (could not create ZIP file)" );
78 } else {
79 $zip->addFile( stream_get_meta_data( $temporary_file )['uri'], $remote_file );
80 $zip->close();
81 // Upload file
82 $drive->upload_file( $zipfile, $remote_file . '.zip' );
83 if ( '0' !== $fkp ) {
84 // Cleanup
85 $drive->cleanup( $name . $separator . '*' . $file_ext . '.zip', $fkp );
86 }
87 // Remove ZIP file
88 wp_delete_file( $zipfile );
89 self::mail( $eml, "Export {$name} successfully completed at " . gmdate( 'Y-m-d H:i:s' ) );
90 }
91 } else {
92 fseek( $temporary_file, 0 );
93 // Upload file
94 $drive->upload_file( stream_get_meta_data( $temporary_file )['uri'], $remote_file );
95 if ( '0' !== $fkp ) {
96 // Cleanup
97 $drive->cleanup( $name . $separator . '*' . $file_ext, $fkp );
98 }
99 self::mail( $eml, "Export {$name} successfully completed at " . gmdate( 'Y-m-d H:i:s' ) );
100 }
101 // Remove temporary file
102 // phpcs:disable WordPress.WP.AlternativeFunctions.file_system_operations_fclose
103 fclose( $temporary_file );
104 // phpcs:enable WordPress.WP.AlternativeFunctions.file_system_operations_fclose
105 }
106 }
107 }
108 } else {
109 WPDA::wpda_log_wp_error( "Export {$name} failed (drive not found)" );
110 self::mail( $eml, "Export {$name} failed (drive not found)" );
111 }
112 }
113 }
114
115 public static function get_export( $name ) {
116 $scheduled_exports = \WPDataAccess\Utilities\WPDA_Export_Scheduler::wpda_cron_events();
117 foreach ( $scheduled_exports as $time_stamp => $scheduled_export ) {
118 foreach ( $scheduled_export as $wpda_events ) {
119 foreach ( $wpda_events as $wpda_event ) {
120 if ( isset( $wpda_event['args']['args']['name'] ) && $name === $wpda_event['args']['args']['name'] ) {
121 return array(
122 'time_stamp' => $time_stamp,
123 'args' => $wpda_event['args'],
124 );
125 }
126 }
127 }
128 }
129 return false;
130 }
131
132 private static function mail( $to, $message, $attachments = array() ) {
133 }
134
135 }
136