示例#1
0
 /**
  * Save a table
  *
  * @since 1.0.0
  *
  * @param array $table Table (needs to have $table['id']!)
  * @return string|WP_Error WP_Error on error, string table ID on success
  */
 public function save(array $table)
 {
     if (empty($table['id'])) {
         return new WP_Error('table_save_empty_table_id');
     }
     $post_id = $this->_get_post_id($table['id']);
     if (false === $post_id) {
         return new WP_Error('table_save_no_post_id_for_table_id', '', $table['id']);
     }
     $post = $this->_table_to_post($table, $post_id);
     $new_post_id = $this->model_post->update($post);
     if (is_wp_error($new_post_id)) {
         // Add an error code to the existing WP_Error
         $new_post_id->add('table_save_post_update', '', $post_id);
         return $new_post_id;
     }
     if ($post_id !== $new_post_id) {
         return new WP_Error('table_save_new_post_id_does_not_match', '', $new_post_id);
     }
     $options_saved = $this->_update_table_options($new_post_id, $table['options']);
     if (!$options_saved) {
         return new WP_Error('table_save_update_table_options_failed', '', $new_post_id);
     }
     $visibility_saved = $this->_update_table_visibility($new_post_id, $table['visibility']);
     if (!$visibility_saved) {
         return new WP_Error('table_save_update_table_visibility_failed', '', $new_post_id);
     }
     // at this point, post was successfully added
     // invalidate table output caches that belong to this table
     $this->_invalidate_table_output_cache($table['id']);
     // Flush caching plugins' caches
     $this->_flush_caching_plugins_caches();
     return $table['id'];
 }
示例#2
0
 /**
  * Save a table.
  *
  * @since 1.0.0
  *
  * @param array $table Table (needs to have $table['id']!).
  * @return string|WP_Error WP_Error on error, string table ID on success.
  */
 public function save(array $table)
 {
     if (empty($table['id'])) {
         return new WP_Error('table_save_empty_table_id');
     }
     $post_id = $this->_get_post_id($table['id']);
     if (false === $post_id) {
         return new WP_Error('table_save_no_post_id_for_table_id', '', $table['id']);
     }
     $post = $this->_table_to_post($table, $post_id);
     $new_post_id = $this->model_post->update($post);
     if (is_wp_error($new_post_id)) {
         // Add an error code to the existing WP_Error.
         $new_post_id->add('table_save_post_update', '', $post_id);
         return $new_post_id;
     }
     if ($post_id !== $new_post_id) {
         return new WP_Error('table_save_new_post_id_does_not_match', '', $new_post_id);
     }
     $options_saved = $this->_update_table_options($new_post_id, $table['options']);
     if (!$options_saved) {
         return new WP_Error('table_save_update_table_options_failed', '', $new_post_id);
     }
     $visibility_saved = $this->_update_table_visibility($new_post_id, $table['visibility']);
     if (!$visibility_saved) {
         return new WP_Error('table_save_update_table_visibility_failed', '', $new_post_id);
     }
     // At this point, post was successfully added.
     // Invalidate table output caches that belong to this table.
     $this->_invalidate_table_output_cache($table['id']);
     // Flush caching plugins' caches.
     $this->_flush_caching_plugins_caches();
     /**
      * Fires after a table has been saved.
      *
      * @since 1.5.0
      *
      * @param string $table_id ID of the added table.
      */
     do_action('tablepress_event_saved_table', $table['id']);
     return $table['id'];
 }