ia id. * * @return int */ public function get_pending_count( $media_id = false ) { global $wpdb; $rtmedia_model = new RTMediaModel(); $query_pending = "SELECT COUNT(*) as pending from {$rtmedia_model->table_name} where file_size IS NULL AND media_type in ('photo','video','document','music','other')"; if ( $media_id ) { $media_id = intval( $media_id ); $query_pending = $wpdb->prepare( "SELECT COUNT(*) as pending from {$rtmedia_model->table_name} where file_size IS NULL AND media_type in ('photo','video','document','music','other') AND id > %d", $media_id ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared } // Direct query is required for custom table. safe because SQL is prepared. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $pending_count = $wpdb->get_results( $query_pending ); if ( $pending_count && count( $pending_count ) > 0 ) { return $pending_count[0]->pending; } return 0; } /** * Get total count. * * @return int */ public function get_total_count() { global $wpdb; $rtmedia_model = new RTMediaModel(); $query_total = "SELECT COUNT(*) as total from {$rtmedia_model->table_name} where media_type in ('photo','video','document','music','other') "; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared // Direct query is required for custom table. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared $total_count = $wpdb->get_results( $query_total ); if ( $total_count && count( $total_count ) > 0 ) { return $total_count[0]->total; } return 0; } /** * Media size import. * * @param int $lastid Last id. * @param int $limit Limit of rows. */ public function rtmedia_media_size_import( $lastid = 0, $limit = 1 ) { global $wpdb; if ( check_ajax_referer( 'rtmedia_media_size_import_nonce', 'nonce' ) ) { $rtmedia_model = new RTMediaModel(); $get_media_sql = $wpdb->prepare( "SELECT * from {$rtmedia_model->table_name} where file_size is NULL and media_type in ('photo','video','document','music','other') order by id limit %d", $limit ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared $lastid = filter_input( INPUT_POST, 'last_id', FILTER_SANITIZE_NUMBER_INT ); if ( ! empty( $lastid ) ) { $get_media_sql = $wpdb->prepare( "SELECT * from {$rtmedia_model->table_name} where id > %d AND file_size is NULL and media_type in ('photo','video','document','music','other') order by id limit %d", $lastid, $limit ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared } // Direct query is required for custom table. safe because SQL is prepared. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared $result = $wpdb->get_results( $get_media_sql ); if ( $result && count( $result ) > 0 ) { $migrate = $this->migrate_single_media( $result[0] ); } $this->return_migration( $result[0], $migrate ); } else { echo '0'; wp_die(); } } /** * Migrate single media. * * @param object $result Object of media. * * @return bool */ public function migrate_single_media( $result ) { global $wpdb; $rtmedia_model = new RTMediaModel(); $attached_file = get_attached_file( $result->media_id ); $return = true; if ( file_exists( $attached_file ) ) { $file_size = filesize( $attached_file ); } else { error_log( 'rtMedia size importer: file not exist. Media ID: ' . esc_html( $result->id ) . ', File: ' . esc_html( $attached_file ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log return false; } $post = get_post( $result->media_id ); $post_date = $post->post_date; $rtmedia_model->update( array( 'upload_date' => $post_date, 'file_size' => $file_size, ), array( 'id' => $result->id ) ); return $return; } /** * Return migration data. * * @param bool|object $media Media object. * @param bool $migrate Migrate done or not. */ public function return_migration( $media = false, $migrate = true ) { $total = $this->get_total_count(); $pending = $this->get_pending_count( $media->id ); $done = $total - $pending; if ( $pending < 0 ) { $pending = 0; $done = $total; } if ( $done > $total ) { $done = $total; } rtmedia_update_site_option( 'rtmedia_media_size_import_pending_count', $pending ); $pending_time = rtmedia_migrate_formatseconds( $pending ) . ' (estimated)'; echo wp_json_encode( array( 'status' => true, 'done' => $done, 'total' => $total, 'pending' => $pending_time, 'media_id' => $media->id, 'imported' => $migrate, ) ); die(); } }