/** * Update the database to version 0.5.0 * * Takes care of: * * Deleting all the current (useless) post revisions * * Add a new revision at the current layout * * Add gist_id = none post_meta to all Gistpens * * @since 0.5.0 */ public function update_to_0_5_0() { delete_option('wp_gistpens_languages_installed'); delete_option('wp_gistpen_langs_installed'); // Need to remove these filters first remove_filter('the_content', 'wpautop'); remove_filter('the_content', 'wptexturize'); remove_filter('the_content', 'capital_P_dangit'); remove_filter('the_content', 'convert_chars'); remove_filter('get_the_excerpt', 'wp_trim_excerpt'); $posts = get_posts(array('post_type' => 'revision', 'post_status' => 'any', 'nopaging' => 'true')); foreach ($posts as $post) { if (get_post_type($post->post_parent) === 'gistpen') { wp_delete_post($post->ID, true); } } $posts = get_posts(array('post_type' => 'gistpen', 'post_status' => 'any', 'nopaging' => 'true', 'post_parent' => 0)); foreach ($posts as $post) { $ids = array(); $zip = $this->database->query('head')->by_post($post); $ids['zip'] = $zip->get_ID(); $ids['files'] = array(); $files = $zip->get_files(); foreach ($files as $file) { $ids['files'][] = $file->get_ID(); } $this->database->persist('commit')->by_ids($ids); update_post_meta($post->ID, '_wpgp_gist_id', 'none'); } }
/** * Imports a Gist into Gistpen by ID * * @param string $gist_id Gist ID * @return string|\WP_Error Gist ID on success, WP_Error on failure * @since 0.5.0 */ public function import_gist($gist_id) { // Exit if this gist has already been imported $query = $this->database->query('head')->by_gist_id($gist_id); if ($query instanceof Zip) { return $query; } $response = $this->gist->get_gist($gist_id); if (is_wp_error($response)) { return $response; } $zip = $response['zip']; $version = $response['version']; unset($response); $result = $this->database->persist('head')->by_zip($zip); if (is_wp_error($result)) { return $result; } $ids = $result; unset($result); $result = $this->database->persist('commit')->by_ids($ids); if (is_wp_error($result)) { return $result; } $result = $this->database->persist('commit')->set_gist_id($result, $version); if (is_wp_error($result)) { return $result; } return $ids['zip']; }
/** * Render the file column on the Gistpen edit screen * * @param string $column_name the custom column name * @param int $post_id the ID of the current post * @since 0.4.0 */ public function manage_posts_custom_column($column_name, $post_id) { if ('gistpen_files' === $column_name) { $zip = $this->database->query()->by_id($post_id); foreach ($zip->get_files() as $file) { echo $file->get_filename(); echo '<br>'; } } }
/** * Deletes the files when a zip gets deleted * * @param int $post_id post ID of the zip being deleted * @since 0.5.0 */ public function delete_files($post_id) { $post = get_post($post_id); if ('gistpen' === $post->post_type && 0 === $post->post_parent) { $zip = $this->database->query()->by_post($post); $files = $zip->get_files(); foreach ($files as $file) { wp_delete_post($file->get_ID(), true); } do_action('wpgp_after_delete', $zip); } }
/** * Register the shortcode to embed the Gistpen * * @param array $atts attributes passed into the shortcode * @return string * @since 0.1.0 */ public function add_shortcode($atts) { $args = shortcode_atts(array('id' => null, 'highlight' => null), $atts, 'gistpen'); // If the user didn't provide an ID, raise an error if ($args['id'] === null) { return '<div class="wp-gistpen-error">No Gistpen ID was provided.</div>'; } $zip = $this->database->query()->by_id($args['id']); if (is_wp_error($zip)) { // @todo each error return; } return $zip->get_shortcode_content($args['highlight']); }
/** * Get all the Gist IDs for the user from * Gist and check if they've been imported already * * @since 0.5.0 */ public function get_new_user_gists() { $this->check_security(); $gists = $this->gist->get_gists(); $this->check_error($gists); $new_user_gists = array(); foreach ($gists as $gist) { $result = $this->database->query('head')->by_gist_id($gist); if (empty($result)) { $new_user_gists[] = $gist; } } if (empty($new_user_gists)) { wp_send_json_error(array('code' => 'error', 'message' => __('No Gists to import.', $this->plugin_name))); } wp_send_json_success(array('gist_ids' => $new_user_gists)); }