Advertisement
geminilabs

Untitled

May 4th, 2024
1,161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.73 KB | None | 0 0
  1. class Glsr_Fix_Assigned_Posts
  2. {
  3.     protected int $current;
  4.     protected int $fixed;
  5.     protected int $limit;
  6.  
  7.     public function __construct()
  8.     {
  9.         $this->current = 0;
  10.         $this->fixed = 0;
  11.         $this->limit = 100;
  12.     }
  13.  
  14.     public function run()
  15.     {
  16.         if (!function_exists('glsr')) {
  17.             return;
  18.         }
  19.         wp_raise_memory_limit('admin');
  20.         glsr('Database')->beginTransaction('assigned_posts');
  21.         while (true) {
  22.             $results = $this->unassigned();
  23.             if (empty($results)) {
  24.                 break;
  25.             }
  26.             $values = $this->prepareValues($results);
  27.             if (!empty($values)) {
  28.                 glsr('Database')->insertBulk('assigned_posts', $values, [
  29.                     'rating_id',
  30.                     'post_id',
  31.                     'is_published',
  32.                 ]);
  33.                 $this->fixed += count($values);
  34.             }
  35.         }
  36.         glsr('Database')->finishTransaction('assigned_posts');
  37.         glsr_log()->info("{$this->fixed} review assignments fixed!");
  38.     }
  39.  
  40.     public function prepareValues(array $results): array
  41.     {
  42.         $values = [];
  43.         foreach ($results as &$result) {
  44.             $submitted = maybe_unserialize($result['submitted']);
  45.             $postIds = \GeminiLabs\SiteReviews\Helpers\Arr::getAs('array', $submitted, 'assigned_posts');
  46.             $postIds = glsr('Modules\Sanitizer')->sanitizePostIds($postIds);
  47.             foreach ($postIds as $postId) {
  48.                 $isPublished = 'publish' === get_post_status($postId);
  49.                 $values[] = [
  50.                     'rating_id' => (int) $result['rating_id'],
  51.                     'post_id' => (int) $postId,
  52.                     'is_published' => (int) $isPublished,
  53.                 ];
  54.             }
  55.         }
  56.         $last = end($results);
  57.         $this->current = $last['rating_id'] ?? 0;
  58.         return $values;
  59.     }
  60.  
  61.     public function unassigned(): array
  62.     {
  63.         $sql = "
  64.            SELECT r.ID AS rating_id, pm.meta_value AS submitted
  65.            FROM table|ratings AS r
  66.            INNER JOIN table|postmeta AS pm ON (pm.post_id = r.review_id)
  67.            WHERE 1=1
  68.            AND r.ID > %s
  69.            AND r.ID NOT IN (
  70.                SELECT apt.rating_id
  71.                FROM table|assigned_posts apt
  72.            )
  73.            AND pm.meta_key = '_submitted'
  74.            ORDER BY r.ID
  75.            LIMIT 0, %d
  76.        ";
  77.         return glsr('Database')->dbGetResults(
  78.             glsr('Database\Query')->sql($sql, $this->current, $this->limit),
  79.             ARRAY_A
  80.         );
  81.     }
  82. }
  83.  
  84. add_action('admin_init', function () {
  85.     $command = new \Glsr_Fix_Assigned_Posts();
  86.     $command->run();   
  87. });
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement