/** * Combines a post and (potentially) a blob * * If no blob is provided, turns post into blob * * If blob is provided, compares blob to post * and updates blob data based on differences */ public function new_blob($post, $blob = array()) { if (empty($blob)) { $blob = $this->blob_from_post($post); } else { unset($blob->url); unset($blob->size); if ($blob->path !== $post->github_path()) { $blob->path = $post->github_path(); $this->changed = true; } $blob_data = $this->api->get_blob($blob->sha); if (base64_decode($blob_data->content) !== $post->github_content()) { unset($blob->sha); $blob->content = $post->github_content(); $this->changed = true; } } return $blob; }
/** * Checks if current position is valid * * @link http://php.net/manual/en/iterator.valid.php * @return boolean true on success, false on failure. */ public function valid() { global $wpdb; while (isset($this->tree[$this->position])) { $blob = $this->tree[$this->position]; // Skip the repo's readme if ('readme' === strtolower(substr($blob->path, 0, 6))) { WordPress_GitHub_Sync::write_log(__('Skipping README', 'wordpress-github-sync')); $this->next(); continue; } // If the blob sha already matches a post, then move on $id = $wpdb->get_var("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_sha' AND meta_value = '{$blob->sha}'"); if ($id) { WordPress_GitHub_Sync::write_log(sprintf(__('Already synced blob %s', 'wordpress-github-sync'), $blob->path)); $this->next(); continue; } $blob = $this->api->get_blob($blob->sha); if (is_wp_error($blob)) { WordPress_GitHub_Sync::write_log(sprintf(__('Failed getting blob with error: %s', 'wordpress-github-sync'), $blob->get_error_message())); $this->next(); continue; } $content = base64_decode($blob->content); // If it doesn't have YAML frontmatter, then move on if ('---' !== substr($content, 0, 3)) { WordPress_GitHub_Sync::write_log(sprintf(__('No front matter on blob %s', 'wordpress-github-sync'), $blob->sha)); $this->next(); continue; } $blob->content = $content; $this->current = $blob; return true; } return false; }