Пример #1
0
 /**
  * Retrieves the Language object for a given State ID
  *
  * @param  int $post_id
  * @return Language
  * @since  0.4.0
  */
 public function language_by_state_id($state_id)
 {
     $terms = get_the_terms($state_id, 'wpgp_language');
     if (empty($terms)) {
         return $this->adapter->build('language')->blank();
     }
     $term = array_pop($terms);
     return $this->adapter->build('language')->by_slug($term->slug);
 }
Пример #2
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']);
 }
Пример #3
0
 /**
  * Responds to AJAX request to create new Gistpen
  *
  * @since  0.2.0
  */
 public function create_gistpen()
 {
     $this->check_security();
     $zip_data = array('description' => $_POST['wp-gistfile-description'], 'status' => $_POST['post_status']);
     $zip = $this->adapter->build('zip')->by_array($zip_data);
     $file_data = array('slug' => $_POST['wp-gistpenfile-slug'], 'code' => $_POST['wp-gistpenfile-code']);
     $file = $this->adapter->build('file')->by_array($file_data);
     $language = $this->adapter->build('language')->by_slug($_POST['wp-gistpenfile-language']);
     $file->set_language($language);
     $zip->add_file($file);
     $result = $this->database->persist()->by_zip($zip);
     $this->check_error($result);
     wp_send_json_success(array('id' => $result['zip']));
 }
Пример #4
0
 /**
  * Retrieves the all the files for a zip's WP_Post object
  *
  * @param  WP_Post $post
  * @return array       array of Files
  * @since  0.4.0
  */
 public function files_by_post($post)
 {
     $file_posts = get_children(array('post_type' => 'gistpen', 'post_parent' => $post->ID, 'post_status' => $post->post_status, 'order' => 'ASC', 'orderby' => 'date'));
     if (empty($file_posts)) {
         return $file_posts;
     }
     $files = array();
     foreach ($file_posts as $file_post) {
         $file = $this->adapter->build('file')->by_post($file_post);
         $file->set_language($this->language_by_post_id($file_post->ID));
         $files[$file_post->ID] = $file;
     }
     return $files;
 }
Пример #5
0
 /**
  * Retrieves an individual Gist
  *
  * @param  string $id Gist id
  * @return array      array response with Zip and commit version sha
  * @since  0.5.0
  */
 public function get_gist($id)
 {
     $result = $this->set_up_client();
     if (is_wp_error($result)) {
         return $result;
     }
     try {
         $response = $this->call()->show($id);
     } catch (\Exception $e) {
         $response = new \WP_Error($e->getCode(), $e->getMessage());
     }
     if (is_wp_error($response)) {
         return $response;
     }
     $zip = $this->adapter->build('zip')->by_gist($response);
     foreach ($response['files'] as $filename => $file_data) {
         $file = $this->adapter->build('file')->by_gist($file_data);
         $file->set_language($this->adapter->build('language')->by_gist($file_data['language']));
         $zip->add_file($file);
     }
     $result = array('zip' => $zip, 'version' => $response['history'][0]['version']);
     return $result;
 }
Пример #6
0
 /**
  * Update the database to version 0.4.0
  *
  * @since 0.4.0
  */
 public function update_to_0_4_0()
 {
     // We removed this post_type and taxonomy, so we need to add them to use them
     register_post_type('gistpens', array());
     register_taxonomy('language', array('gistpens'));
     // 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');
     $terms = get_terms('language', 'hide_empty=0');
     foreach ($terms as $term) {
         // We're going to move the current term to a holdover
         $result = wp_update_term($term->term_id, 'language', array('slug' => $term->slug . '-old', 'name' => $term->name . '-old'));
         if (is_wp_error($result)) {
             // Deactivate and quit
             deactivate_plugins('WP-Gistpen');
             // and provide an error
             print __("Failed to successfully set holdover for language {$term->slug}. Error: " . $result->get_error_message(), $this->plugin_name);
         }
         // So we can create new terms with the old slug/name combo
         $result = wp_insert_term($term->name, 'wpgp_language', array('slug' => $term->slug, 'name' => $term->name));
         if (is_wp_error($result)) {
             // Deactivate and quit
             deactivate_plugins('WP-Gistpen');
             // and provide an error
             print __("Failed to successfully insert language {$term->slug}. Error: " . $result->get_error_message(), $this->plugin_name);
         }
     }
     // Get all the Gistpens to work with
     $posts = get_posts(array('post_type' => 'gistpens', 'posts_per_page' => -1, 'post_status' => 'any'));
     foreach ($posts as $post) {
         // Cache and clear content
         $content = $post->post_content;
         $post->post_content = '';
         // Update post type to remove the 's'
         $post->post_type = 'gistpen';
         $zip = $this->adapter->build('zip')->by_post($post);
         $file = $this->adapter->build('file')->blank();
         // Migrate title to file's name
         $file->set_slug($post->post_title);
         // Migrate description to Gistpen title and remove post_meta
         $zip->set_description(get_post_meta($post->ID, '_wpgp_gistpen_description', true));
         $result = delete_post_meta($post->ID, '_wpgp_gistpen_description');
         if (is_wp_error($result)) {
             // Deactivate and quit
             deactivate_plugins('WP-Gistpen');
             // and provide an error
             print __("Failed to successfully delete description meta from {$post->ID}. Error: " . $result->get_error_message(), $this->plugin_name);
         }
         // Set content
         $file->set_code($content);
         // Migrate Gistpen's language and remove
         // @todo move this into helper function?
         $terms = get_the_terms($post->ID, 'language');
         if ($terms) {
             $lang = array_pop($terms);
         } else {
             $lang = '';
         }
         // Don't forget to remove that holdover!
         $file->set_language($this->adapter->build('language')->by_slug(str_replace('-old', '', $lang->slug)));
         $result = wp_set_object_terms($post->ID, array(), 'language', false);
         if (is_wp_error($result)) {
             // Deactivate and quit
             deactivate_plugins('WP-Gistpen');
             // and provide an error
             print __("Failed to successfully delete language from {$post->ID}. Error: " . $result->get_error_message(), $this->plugin_name);
         }
         $zip->add_file($file);
         $result = $this->database->persist()->by_zip($zip);
         if (is_wp_error($result)) {
             // Deactivate and quit
             deactivate_plugins(__FILE__);
             // and provide an error
             print __("Failed to successfully save {$post->ID} in new format. Error: " . $result->get_error_message(), $this->plugin_name);
         }
     }
     $terms = get_terms('language', 'hide_empty=0');
     foreach ($terms as $term) {
         wp_delete_term($term->term_id, 'language');
     }
     flush_rewrite_rules(true);
 }