Esempio n. 1
0
 /**
  * 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');
     }
 }
Esempio n. 2
0
 /**
  * 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'];
 }
Esempio n. 3
0
 /**
  * Update a Zip and save a new revision
  *
  * @param  array  $zip_data  Array of zip data
  * @return int|\WP_Error      Zip ID on success, WP_Error on failure
  * @since  0.5.0
  */
 public function update($zip_data)
 {
     if ('auto-draft' === $zip_data['status']) {
         $zip_data['status'] = 'draft';
     }
     $zip = $this->adapter->build('zip')->by_array($zip_data);
     // Check user permissions
     if ($zip->get_ID()) {
         if (!current_user_can('edit_post', $zip->get_ID())) {
             return new \WP_Error('no_perms', __('User does not have permission to edit post ', \WP_Gistpen::$plugin_name) . $zip->get_ID());
         }
     } else {
         if (!current_user_can('edit_posts')) {
             return new \WP_Error('no_perms', __('User does not have permission to edit post ', \WP_Gistpen::$plugin_name) . $zip->get_ID());
         }
     }
     foreach ($zip_data['files'] as $file_data) {
         $file = $this->adapter->build('file')->by_array($file_data);
         $file->set_language($this->adapter->build('language')->by_slug($file_data['language']));
         $zip->add_file($file);
         unset($file);
     }
     $results = $this->database->persist('head')->by_zip($zip);
     if (is_wp_error($results)) {
         return $results;
     }
     $result = $this->database->persist('commit')->by_ids($results);
     if (is_wp_error($result)) {
         return $result;
     }
     /**
      * After Update filter hook.
      *
      * Can hook in and return WP_Error objects
      * if something happens that results in an error.
      * This response is important when communicating
      * with GitHub's API. If something goes wrong,
      * we want to return that error to the user
      * so they can go fix it. This response is returned
      * by the Ajax API.
      */
     return apply_filters('wpgp_after_update', $results['zip']);
 }
Esempio n. 4
0
 /**
  * AJAX hook to trigger export of Gistpen
  *
  * @since 0.5.0
  */
 public function create_gist_from_gistpen_id()
 {
     $this->check_security();
     $id = intval($_POST['gistpen_id']);
     if (0 === $id) {
         wp_send_json_error(array('code' => 'error', 'message' => __('Invalid Gistpen ID.', $this->plugin_name)));
     }
     $this->database->persist('head')->set_sync($id, 'on');
     $result = $this->sync->export_gistpen($id);
     $this->check_error($result);
     // This will slow the API exporting process when calling
     // it from the settings page, as the next call
     // won't start until a response has been returned.
     // However, we need to implement a more effective
     // rate limiting, as this API can still receive
     // multiple requests at once and this sleep will
     // do nothing about it.
     sleep(1);
     wp_send_json_success(array('code' => 'success', 'message' => __('Successfully exported Gistpen #', $this->plugin_name) . $result));
 }