/** * Writes a log message. * * Can extract a message from WP_Error object. * * @param string|WP_Error $msg Message to log. */ protected function log($msg) { if (is_wp_error($msg)) { $msg = $msg->get_error_message(); } WordPress_GitHub_Sync::write_log($msg); }
/** * Imports a single blob content into matching post. * * @param stdClass $blob */ protected function import_blob($blob) { // Break out meta, if present preg_match('/(^---(.*?)---$)?(.*)/ms', $blob->content, $matches); $body = array_pop($matches); if (3 === count($matches)) { $meta = cyps_load($matches[2]); if (isset($meta['permalink'])) { $meta['permalink'] = str_replace(home_url(), '', get_permalink($meta['permalink'])); } } else { $meta = array(); } if (function_exists('wpmarkdown_markdown_to_html')) { $body = wpmarkdown_markdown_to_html($body); } $args = array('post_content' => apply_filters('wpghs_content_import', $body)); if (!empty($meta)) { if (array_key_exists('layout', $meta)) { $args['post_type'] = $meta['layout']; unset($meta['layout']); } if (array_key_exists('published', $meta)) { $args['post_status'] = true === $meta['published'] ? 'publish' : 'draft'; unset($meta['published']); } if (array_key_exists('post_title', $meta)) { $args['post_title'] = $meta['post_title']; unset($meta['post_title']); } if (array_key_exists('ID', $meta)) { $args['ID'] = $meta['ID']; unset($meta['ID']); } } if (!isset($args['ID'])) { // @todo create a revision when we add revision author support $post_id = wp_insert_post($args); } else { $post_id = wp_update_post($args); } /** @var WordPress_GitHub_Sync_Post $post */ $post = new WordPress_GitHub_Sync_Post($post_id); $post->set_sha($blob->sha); foreach ($meta as $key => $value) { update_post_meta($post_id, $key, $value); } WordPress_GitHub_Sync::write_log(__('Updated blob ', 'wordpress-github-sync') . $blob->sha); }
/** * Sets and kicks off the import cronjob */ public function start_import() { update_option('_wpghs_import_started', 'yes'); WordPress_GitHub_Sync::write_log(__('Starting import from GitHub.', WordPress_GitHub_Sync::$text_domain)); wp_schedule_single_event(time(), 'wpghs_import'); spawn_cron(); }
/** * Retrieve the saved tree we're building * or get the latest tree from the repo */ public function get_tree() { if (!empty($this->tree)) { return; } $tree = $this->api->last_tree_recursive(); if (is_wp_error($tree)) { WordPress_GitHub_Sync::write_log(__('Failed getting tree with error: ', WordPress_GitHub_Sync::$text_domain) . $tree->get_error_message()); return; } $this->tree = $tree; }
/** * Export all the posts in the database to GitHub */ public function export_all() { global $wpdb; if ($this->locked()) { WordPress_GitHub_Sync::write_log(__('Export locked. Terminating.', 'wordpress-github-sync')); return; } $post_statuses = $this->format_for_query($this->get_whitelisted_post_statuses()); $post_types = $this->format_for_query($this->get_whitelisted_post_types()); $post_ids = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE\n\t\t\tpost_status IN ( {$post_statuses} ) AND\n\t\t\tpost_type IN ( {$post_types} )"); $msg = apply_filters('wpghs_commit_msg_full', 'Full export from WordPress at ' . site_url() . ' (' . get_bloginfo('name') . ')') . ' - wpghs'; $export = new WordPress_GitHub_Sync_Export($post_ids, $msg); $export->run(); }
/** * Writes out the results of a successful export */ public function success() { update_option('_wpghs_export_complete', 'yes'); update_option('_wpghs_fully_exported', 'yes'); WordPress_GitHub_Sync::write_log(__('Export to GitHub completed successfully.', 'wordpress-github-sync'), 'success'); }
/** * 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; }