From 6c872b54624c74d3f286eb13feeb85b63de3e9a3 Mon Sep 17 00:00:00 2001 From: Jeremy Albuixech Date: Wed, 9 Sep 2015 14:19:51 -0400 Subject: [PATCH 1/2] Added the set_post_meta and set_post_featured_image functions. --- class.postcontroller.php | 162 ++++++++++++++++++++++++--------------- 1 file changed, 102 insertions(+), 60 deletions(-) diff --git a/class.postcontroller.php b/class.postcontroller.php index e7ae54d..a111c38 100644 --- a/class.postcontroller.php +++ b/class.postcontroller.php @@ -3,16 +3,16 @@ * Wrapper Class to create/manage/update Wordpress posts and pages. * * @author Harri Bell-Thomas - * @created January, 2014 + * @created January, 2014 * @version 1.0.0 * @license Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) * @license url : http://creativecommons.org/licenses/by-sa/3.0/ -*/ +*/ if (!isset($wp_rewrite)) $wp_rewrite = new WP_Rewrite(); - + class PostController { - + // Variables for Post Data public $PC_title; public $PC_type; @@ -22,23 +22,23 @@ class PostController { public $PC_slug; public $PC_auth_id; public $PC_status = "publish"; - + // Variables for Post Updating public $PC_current_post; public $PC_current_post_id; public $PC_current_post_permalink; - + // Error Array public $PC_errors; - + // Creation functions public function create() { if(isset($this->PC_title) ) { - if ($this->PC_type == 'page') + if ($this->PC_type == 'page') $post = get_page_by_title( $this->PC_title, 'OBJECT', $this->PC_type ); - else + else $post = get_page_by_title( $this->PC_title, 'OBJECT', $this->PC_type ); - + $post_data = array( 'post_title' => wp_strip_all_tags($this->PC_title), 'post_name' => $this->PC_slug, @@ -60,57 +60,99 @@ public function create() { $this->errors[] = 'That page already exists. Try updating instead. Control passed to the update() function.'; return FALSE; } - } + } else { $this->errors[] = 'Title has not been set.'; return FALSE; } } - - // SET POST'S TITLE + + // SET POST'S FEATURED IMAGE + public function set_post_featured_image($attach_id = null) + { + + if (isset($this->PC_current_post_id)) { + + $post_id = $this->PC_current_post_id; + + if ( !empty($attach_id) ) + { + set_post_thumbnail( $post_id, $attach_id ); + } + + } + return($errors); + } + + // SET POST'S META + public function set_post_meta($field_name, $value = '' ) + { + + if (isset($this->PC_current_post_id)) { + + $post_id = $this->PC_current_post_id; + + if ( $value === false ) + { + delete_post_meta( $post_id, $field_name ); + } + elseif ( ! get_post_meta( $post_id, $field_name ) ) + { + add_post_meta( $post_id, $field_name, $value ); + } + else + { + update_post_meta( $post_id, $field_name, $value ); + } + } + + return($errors); + } + + // SET POST'S TITLE public function set_title($name){ - $this->PC_title = $name; + $this->PC_title = $name; return $this->PC_title; } - - // SET POST'S TYPE + + // SET POST'S TYPE public function set_type($type){ - $this->PC_type = $type; + $this->PC_type = $type; return $this->PC_type; } - - // SET POST'S CONTENT + + // SET POST'S CONTENT public function set_content($content){ - $this->PC_content = $content; + $this->PC_content = $content; return $this->PC_content; } - - // SET POST'S AUTHOR ID + + // SET POST'S AUTHOR ID public function set_author_id($auth_id){ - $this->PC_auth_id = $auth_id; + $this->PC_auth_id = $auth_id; return $this->PC_auth_id; } - - // SET POST'S STATE + + // SET POST'S STATE public function set_post_state($content){ $this->PC_status = $content; return $this->PC_status; } - + // SET POST SLUG public function set_post_slug($slug){ $args = array('name' => $slug); $posts_query = get_posts( $args ); if( !get_posts( $args ) && !get_page_by_path( $this->PC_slug ) ) { - $this->PC_slug = $slug; - return $this->PC_slug; + $this->PC_slug = $slug; + return $this->PC_slug; } else { $this->errors[] = 'Slug already in use.'; return FALSE; } } - + // SET PAGE TEMPLATE public function set_page_template($content){ if ($this->PC_type == "page") { @@ -122,7 +164,7 @@ public function set_page_template($content){ return FALSE; } } - + // ADD CATEGORY IDs TO THE CATEGORIES ARRAY public function add_category($IDs){ if(is_array($IDs)) { @@ -139,7 +181,7 @@ public function add_category($IDs){ return FALSE; } } - + // Search for Post function public function search($by, $data){ switch ($by) { @@ -154,7 +196,7 @@ public function search($by, $data){ return FALSE; } break; - + case 'title' : $post = get_page_by_title($data); $id = $post->ID; @@ -168,11 +210,11 @@ public function search($by, $data){ return FALSE; } break; - + case 'slug' : $args = array('name' => $data, 'max_num_posts' => 1); $posts_query = get_posts( $args ); - if( $posts_query ) + if( $posts_query ) $id = $posts_query[0]->ID; else $this->errors[] = 'No post found with that slug.'; @@ -185,42 +227,42 @@ public function search($by, $data){ $this->errors[] = 'No post found with that slug.'; return FALSE; } - + break; - + default: $this->errors[] = 'No post found.'; return FALSE; break; } } - + // Update Post public function update(){ if (isset($this->PC_current_post_id)) { - + // Declare ID of Post to be updated $PC_post['ID'] = $this->PC_current_post_id; - + // Declare ID of Post to be updated if (isset($this->PC_title) && $this->PC_title !== $this->PC_current_post->post_title) $PC_post['post_title'] = $this->PC_title; - + if (isset($this->PC_type) && $this->PC_type !== $this->PC_current_post->post_type) $PC_post['post_type'] = $this->PC_type; - + if (isset($this->PC_auth_id) && $this->PC_auth_id !== $this->PC_current_post->post_type) $PC_post['post_author'] = $this->PC_auth_id; - + if (isset($this->PC_status) && $this->PC_status !== $this->PC_current_post->post_status) $PC_post['post_status'] = $this->PC_status; - + if (isset($this->PC_category) && $this->PC_category !== $this->PC_current_post->post_category) $PC_post['post_category'] = $this->PC_category; - + if (isset($this->PC_template) && $this->PC_template !== $this->PC_current_post->page_template && ($PC_post['post_type'] == 'page' || $this->PC_current_post->post_type == 'page')) $PC_post['page_template'] = $this->PC_template; - + if (isset($this->PC_slug) && $this->PC_slug !== $this->PC_current_post->post_name) { $args = array('name' => $this->PC_slug); if( !get_posts( $args ) && !get_page_by_path( $this->PC_slug ) ) @@ -228,46 +270,46 @@ public function update(){ else $errors[] = 'Slug Defined is Not Unique'; } - + if (isset($this->PC_content) && $this->PC_content !== $this->PC_status->post_content ) $PC_post['post_content'] = $this->PC_content; - + wp_update_post( $PC_post ); } return($errors); } - + // General functions public function get_content(){ if (isset($this->PC_status->post_content ) ) return $this->PC_status->post_content; } - + public function get_var($name){ $name = 'PC_'.$name; if (isset($this->$name)) return $this->$name; } - + public function unset_all(){ - foreach (get_class_vars(get_class($this)) as $name => $default) - $this->$name = $default; + foreach (get_class_vars(get_class($this)) as $name => $default) + $this->$name = $default; } - - public function __toString() { + + public function __toString() { return 'Use the PrettyPrint function to return the contents of this Object. E.g;
$my_post->PrettyPrintAll();
'; - } - + } + public function PrettyPrint($data){ echo "
";
 		print_r($data);
-		echo "
"; + echo ""; } - + public function PrettyPrintAll(){ echo "
";
 		print_r($this);
-		echo "
"; + echo ""; } } -?> \ No newline at end of file +?> From 4f88197376af177f28358f27e179e710d5a81aeb Mon Sep 17 00:00:00 2001 From: Jeremy Albuixech Date: Wed, 9 Sep 2015 14:26:16 -0400 Subject: [PATCH 2/2] Handled error in the two new methods --- class.postcontroller.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/class.postcontroller.php b/class.postcontroller.php index a111c38..81b2838 100644 --- a/class.postcontroller.php +++ b/class.postcontroller.php @@ -79,8 +79,14 @@ public function set_post_featured_image($attach_id = null) { set_post_thumbnail( $post_id, $attach_id ); } + else { + $errors[] = 'No attachment found with that ID.'; + } } + else { + $errors[] = 'No post found.'; + } return($errors); } @@ -105,6 +111,9 @@ public function set_post_meta($field_name, $value = '' ) update_post_meta( $post_id, $field_name, $value ); } } + else { + $errors[] = 'No post found.'; + } return($errors); }