├── .gitattributes ├── .gitignore ├── README.md ├── class.postcontroller.php └── index.php /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | [Dd]ebug/ 46 | [Rr]elease/ 47 | *_i.c 48 | *_p.c 49 | *.ilk 50 | *.meta 51 | *.obj 52 | *.pch 53 | *.pdb 54 | *.pgc 55 | *.pgd 56 | *.rsp 57 | *.sbr 58 | *.tlb 59 | *.tli 60 | *.tlh 61 | *.tmp 62 | *.vspscc 63 | .builds 64 | *.dotCover 65 | 66 | ## TODO: If you have NuGet Package Restore enabled, uncomment this 67 | #packages/ 68 | 69 | # Visual C++ cache files 70 | ipch/ 71 | *.aps 72 | *.ncb 73 | *.opensdf 74 | *.sdf 75 | 76 | # Visual Studio profiler 77 | *.psess 78 | *.vsp 79 | 80 | # ReSharper is a .NET coding add-in 81 | _ReSharper* 82 | 83 | # Installshield output folder 84 | [Ee]xpress 85 | 86 | # DocProject is a documentation generator add-in 87 | DocProject/buildhelp/ 88 | DocProject/Help/*.HxT 89 | DocProject/Help/*.HxC 90 | DocProject/Help/*.hhc 91 | DocProject/Help/*.hhk 92 | DocProject/Help/*.hhp 93 | DocProject/Help/Html2 94 | DocProject/Help/html 95 | 96 | # Click-Once directory 97 | publish 98 | 99 | # Others 100 | [Bb]in 101 | [Oo]bj 102 | sql 103 | TestResults 104 | *.Cache 105 | ClientBin 106 | stylecop.* 107 | ~$* 108 | *.dbmdl 109 | Generated_Code #added for RIA/Silverlight projects 110 | 111 | # Backup & report files from converting an old project file to a newer 112 | # Visual Studio version. Backup files are not needed, because we have git ;-) 113 | _UpgradeReport_Files/ 114 | Backup*/ 115 | UpgradeLog*.XML 116 | 117 | 118 | 119 | ############ 120 | ## Windows 121 | ############ 122 | 123 | # Windows image file caches 124 | Thumbs.db 125 | 126 | # Folder config file 127 | Desktop.ini 128 | 129 | 130 | ############# 131 | ## Python 132 | ############# 133 | 134 | *.py[co] 135 | 136 | # Packages 137 | *.egg 138 | *.egg-info 139 | dist 140 | build 141 | eggs 142 | parts 143 | bin 144 | var 145 | sdist 146 | develop-eggs 147 | .installed.cfg 148 | 149 | # Installer logs 150 | pip-log.txt 151 | 152 | # Unit test / coverage reports 153 | .coverage 154 | .tox 155 | 156 | #Translations 157 | *.mo 158 | 159 | #Mr Developer 160 | .mr.developer.cfg 161 | 162 | # Mac crap 163 | .DS_Store 164 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
class.postcontroller.php
file in your Wordpress plugin/theme.
9 |
10 | You now have access to the class and its functions. Instantiate the class.
11 | 12 | $Poster = new PostController; 13 |14 |
This class has two major functions; Creating and Updating posts. We'll start with creating.
15 |See index.php
for working examples.
The minimum requisite for creating a post is having the title set. You can do this by using the following code;
18 |$Poster->set_title( "New Post" ); 19 |20 |
After this has been set, simply run the create method.
21 |$Poster->create();22 |
Note: The method will check if there is another post with the same name (just to ensure that it isn't making duplicates). Hence a unique title is required.
23 |There are many other attributes you can set. To do this, you simply run their respective method before calling the create method.
24 |25 |
Before we can update a post, we need to find it. To do this, we use the search method.
28 |$Poster->search( ' SEARCH_BY ' , ' DATA ' ); 29 |30 |
This method accepts 2 parameters, the attribute to search by and the data to search for.
31 |You can search by title;
32 |$Poster->search( 'title' , ' DATA ' ); 33 |34 |
You can search by ID;
35 |$Poster->search( 'id' , ' DATA ' ); 36 |37 |
And you can search by slug;
38 |$Poster->search( 'slug' , ' DATA ' );39 |
The data parameter will accept either a string (if searching by title or slug) or an integer (if searching by title).
40 |If a post or page cannot be found with the specified parameters, an error will be added to the $erros
array. You can call and display this with the following code.
$error = $Poster->get_var(errors); 42 | $Poster->PrettyPrint($error);43 |
Once a post has been found, you can assign it new attributes.
46 |Once the attributes have been set, simply call the update method.
47 |$Poster->update(); 48 |49 |
For example, if you wish to change the title of post 1 (ID = 1), you would use the following code.
50 |$Poster->search( 'id' , 1 ); 51 | $Poster->set_title( "New Title" ); 52 | $Poster->update(); 53 |54 |
All the attributes can be updated this way.
55 |56 |
We have already seen this method. It sets the post's title. No HTML is allowed here, and is stripped out.
59 |$Poster->set_title( "New Post" ); 60 |61 |
This method sets the post type of the page. Input the slug of the post type, eg. 'post'
and 'page'
. Custom post types are supported.
$Poster->set_type( "page" ); 64 |65 |
This method sets the post's content. HTML is allowed.
67 |$Poster->set_content( "<h1>This is my awesome new post!</h1>" ); 68 |
This sets the post's author. Simply specify the author ID of the new author. This must be an integer.
70 |$Poster->set_author_id( 12 ); 71 |72 |
This is the custom url path of the post (if enabled). Take care with this, as if the slug is already in use, it may cause some errors. I have included validation to try and avoid this at all costs though. No special characters or html allowed.
74 |$Poster->set_post_slug( "new_slug" ); 75 |76 |
This is an integer or array of integers. This function is non destructive, meaning it will preserve any categories already set (see 'Multiple Function Example'). The integers correspond to Category ID's, which will be unique to each Wordpress site. NB Category with ID 1 is created automatically and can't be deleted.
78 |$Poster->add_category( array( 1 , 2 ) ); // Adds post to both categories 1 and 2. 79 | $Poster->add_category( 1 ); // Adds post to category 1. 80 | // Multiple Function Example 81 | $Poster->add_category( 1 ); 82 | $Poster->add_category( 2 ); 83 | // This adds the post to both category 1 and 2. 84 |85 |
This method alows you to set your page's template (must be a page). If applied to a different post type it will be ignored, and an error will be added to the errors array. The format of the input will be 'php_template_name.php'
and will be unique to each theme.
$Poster->set_page_template( "fullwidth_page.php" ); 88 |
This method sets the post's state.
90 |Available options; [ 'draft' | 'publish' | 'pending'| 'future' | 'private' | custom registered status ]
91 |$Poster->set_post_state( "pending" ); 92 |
93 |
To retrieve defined variables you can use the get_vars method.
95 |96 | $Poster->get_var('title'); // Returns the title (if you have set it) 97 | $Poster->get_var('type'); // Returns the post type (if you have set it) 98 | $Poster->get_var('content'); // Returns the content (if you have set it) 99 | $Poster->get_var('category'); // Returns the category as an array (if you have set it) 100 | $Poster->get_var('template'); // Returns the template (if you have set it) 101 | $Poster->get_var('slug'); // Returns the slug (if you have set it) 102 | $Poster->get_var('auth_id'); // Returns the author id (if you have set it) 103 | $Poster->get_var('status'); // Returns the post's status (if you have set it) 104 | 105 | // AFTER YOU HAVE EITHER CREATED A POST OR SUCCESFULLY SEARCHED FOR ONE YOU CAN USE THESE 106 | 107 | $Poster->get_var('current_post'); // Returns the a PHP Object of the post. This can be used to check if the search method was successful 108 | $Poster->get_var('current_post_id'); // Returns the selected post's ID (integer). 109 | $Poster->get_var('current_post_permalink'); // Returns the URL permalink of the selected post 110 |111 | -------------------------------------------------------------------------------- /class.postcontroller.php: -------------------------------------------------------------------------------- 1 | 6 | * @created January, 2014 7 | * @version 1.0.0 8 | * @license Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) 9 | * @license url : http://creativecommons.org/licenses/by-sa/3.0/ 10 | */ 11 | if (!isset($wp_rewrite)) 12 | $wp_rewrite = new WP_Rewrite(); 13 | 14 | class PostController { 15 | 16 | // Variables for Post Data 17 | public $PC_title; 18 | public $PC_type; 19 | public $PC_content; 20 | public $PC_category; 21 | public $PC_template; 22 | public $PC_slug; 23 | public $PC_auth_id; 24 | public $PC_status = "publish"; 25 | 26 | // Variables for Post Updating 27 | public $PC_current_post; 28 | public $PC_current_post_id; 29 | public $PC_current_post_permalink; 30 | 31 | // Error Array 32 | public $PC_errors; 33 | 34 | // Creation functions 35 | public function create() { 36 | if(isset($this->PC_title) ) { 37 | if ($this->PC_type == 'page') 38 | $post = get_page_by_title( $this->PC_title, 'OBJECT', $this->PC_type ); 39 | else 40 | $post = get_page_by_title( $this->PC_title, 'OBJECT', $this->PC_type ); 41 | 42 | $post_data = array( 43 | 'post_title' => wp_strip_all_tags($this->PC_title), 44 | 'post_name' => $this->PC_slug, 45 | 'post_content' => $this->PC_content, 46 | 'post_status' => $this->PC_status, 47 | 'post_type' => $this->PC_type, 48 | 'post_author' => $this->PC_auth_id, 49 | 'post_category' => $this->PC_category, 50 | 'page_template' => $this->PC_template 51 | ); 52 | if(!isset($post)){ 53 | $this->PC_current_post_id = wp_insert_post( $post_data, $error_obj ); 54 | $this->PC_current_post = get_post((integer)$this->PC_current_post_id, 'OBJECT'); 55 | $this->PC_current_post_permalink = get_permalink((integer)$this->PC_current_post_id); 56 | return $error_obj; 57 | } 58 | else { 59 | $this->update(); 60 | $this->errors[] = 'That page already exists. Try updating instead. Control passed to the update() function.'; 61 | return FALSE; 62 | } 63 | } 64 | else { 65 | $this->errors[] = 'Title has not been set.'; 66 | return FALSE; 67 | } 68 | } 69 | 70 | // SET POST'S TITLE 71 | public function set_title($name){ 72 | $this->PC_title = $name; 73 | return $this->PC_title; 74 | } 75 | 76 | // SET POST'S TYPE 77 | public function set_type($type){ 78 | $this->PC_type = $type; 79 | return $this->PC_type; 80 | } 81 | 82 | // SET POST'S CONTENT 83 | public function set_content($content){ 84 | $this->PC_content = $content; 85 | return $this->PC_content; 86 | } 87 | 88 | // SET POST'S AUTHOR ID 89 | public function set_author_id($auth_id){ 90 | $this->PC_auth_id = $auth_id; 91 | return $this->PC_auth_id; 92 | } 93 | 94 | // SET POST'S STATE 95 | public function set_post_state($content){ 96 | $this->PC_status = $content; 97 | return $this->PC_status; 98 | } 99 | 100 | // SET POST SLUG 101 | public function set_post_slug($slug){ 102 | $args = array('name' => $slug); 103 | $posts_query = get_posts( $args ); 104 | if( !get_posts( $args ) && !get_page_by_path( $this->PC_slug ) ) { 105 | $this->PC_slug = $slug; 106 | return $this->PC_slug; 107 | } 108 | else { 109 | $this->errors[] = 'Slug already in use.'; 110 | return FALSE; 111 | } 112 | } 113 | 114 | // SET PAGE TEMPLATE 115 | public function set_page_template($content){ 116 | if ($this->PC_type == "page") { 117 | $this->PC_template = $content; 118 | return $this->PC_template; 119 | } 120 | else { 121 | $this->errors[] = 'You can only use template for pages.'; 122 | return FALSE; 123 | } 124 | } 125 | 126 | // ADD CATEGORY IDs TO THE CATEGORIES ARRAY 127 | public function add_category($IDs){ 128 | if(is_array($IDs)) { 129 | foreach ($IDs as $id) { 130 | if (is_int($id)) { 131 | $this->PC_category[] = $id; 132 | } else { 133 | $this->errors[] = '' .$id . ' is not a valid integer input.'; 134 | return FALSE; 135 | } 136 | } 137 | } else { 138 | $this->errors[] = 'Input specified in not a valid array.'; 139 | return FALSE; 140 | } 141 | } 142 | 143 | // Search for Post function 144 | public function search($by, $data){ 145 | switch ($by) { 146 | case 'id' : 147 | if(is_integer($data) && get_post((integer)$data) !== NULL) { 148 | $this->PC_current_post = get_post((integer)$data, 'OBJECT'); 149 | $this->PC_current_post_id = (integer)$data; 150 | $this->PC_current_post_permalink = get_permalink((integer)$data); 151 | return TRUE; 152 | } else { 153 | $this->errors[] = 'No post found with that ID.'; 154 | return FALSE; 155 | } 156 | break; 157 | 158 | case 'title' : 159 | $post = get_page_by_title($data); 160 | $id = $post->ID; 161 | if(is_integer($id) && get_post((integer)$id) !== NULL) { 162 | $this->PC_current_post = get_post((integer)$id, 'OBJECT'); 163 | $this->PC_current_post_id = (integer)$id; 164 | $this->PC_current_post_permalink = get_permalink((integer)$id); 165 | return TRUE; 166 | } else { 167 | $this->errors[] = 'No post found with that title.'; 168 | return FALSE; 169 | } 170 | break; 171 | 172 | case 'slug' : 173 | $args = array('name' => $data, 'max_num_posts' => 1); 174 | $posts_query = get_posts( $args ); 175 | if( $posts_query ) 176 | $id = $posts_query[0]->ID; 177 | else 178 | $this->errors[] = 'No post found with that slug.'; 179 | if(is_integer($id) && get_post((integer)$id) !== NULL) { 180 | $this->PC_current_post = get_post((integer)$id, 'OBJECT'); 181 | $this->PC_current_post_id = (integer)$id; 182 | $this->PC_current_post_permalink = get_permalink((integer)$id); 183 | return TRUE; 184 | } else { 185 | $this->errors[] = 'No post found with that slug.'; 186 | return FALSE; 187 | } 188 | 189 | break; 190 | 191 | default: 192 | $this->errors[] = 'No post found.'; 193 | return FALSE; 194 | break; 195 | } 196 | } 197 | 198 | // Update Post 199 | public function update(){ 200 | if (isset($this->PC_current_post_id)) { 201 | 202 | // Declare ID of Post to be updated 203 | $PC_post['ID'] = $this->PC_current_post_id; 204 | 205 | // Declare ID of Post to be updated 206 | if (isset($this->PC_title) && $this->PC_title !== $this->PC_current_post->post_title) 207 | $PC_post['post_title'] = $this->PC_title; 208 | 209 | if (isset($this->PC_type) && $this->PC_type !== $this->PC_current_post->post_type) 210 | $PC_post['post_type'] = $this->PC_type; 211 | 212 | if (isset($this->PC_auth_id) && $this->PC_auth_id !== $this->PC_current_post->post_type) 213 | $PC_post['post_author'] = $this->PC_auth_id; 214 | 215 | if (isset($this->PC_status) && $this->PC_status !== $this->PC_current_post->post_status) 216 | $PC_post['post_status'] = $this->PC_status; 217 | 218 | if (isset($this->PC_category) && $this->PC_category !== $this->PC_current_post->post_category) 219 | $PC_post['post_category'] = $this->PC_category; 220 | 221 | 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')) 222 | $PC_post['page_template'] = $this->PC_template; 223 | 224 | if (isset($this->PC_slug) && $this->PC_slug !== $this->PC_current_post->post_name) { 225 | $args = array('name' => $this->PC_slug); 226 | if( !get_posts( $args ) && !get_page_by_path( $this->PC_slug ) ) 227 | $PC_post['post_name'] = $this->PC_slug; 228 | else 229 | $errors[] = 'Slug Defined is Not Unique'; 230 | } 231 | 232 | if (isset($this->PC_content) && $this->PC_content !== $this->PC_status->post_content ) 233 | $PC_post['post_content'] = $this->PC_content; 234 | 235 | wp_update_post( $PC_post ); 236 | } 237 | return($errors); 238 | } 239 | 240 | // General functions 241 | public function get_content(){ 242 | if (isset($this->PC_status->post_content ) ) 243 | return $this->PC_status->post_content; 244 | } 245 | 246 | public function get_var($name){ 247 | $name = 'PC_'.$name; 248 | if (isset($this->$name)) 249 | return $this->$name; 250 | } 251 | 252 | public function unset_all(){ 253 | foreach (get_class_vars(get_class($this)) as $name => $default) 254 | $this->$name = $default; 255 | } 256 | 257 | public function __toString() { 258 | return 'Use the PrettyPrint function to return the contents of this Object. E.g;
$my_post->PrettyPrintAll();'; 259 | } 260 | 261 | public function PrettyPrint($data){ 262 | echo "
"; 263 | print_r($data); 264 | echo ""; 265 | } 266 | 267 | public function PrettyPrintAll(){ 268 | echo "
"; 269 | print_r($this); 270 | echo ""; 271 | } 272 | } 273 | ?> -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | 5 | * @created January, 2014 6 | * @version 1.0.0 7 | * @license Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) 8 | * @license url : http://creativecommons.org/licenses/by-sa/3.0/ 9 | */ 10 | 11 | 12 | /* INCLUDE WRAPPER */ 13 | require_once('class.postcontroller.php'); 14 | 15 | 16 | 17 | /* USAGE */ 18 | 19 | $Poster = new PostController; 20 | 21 | $Poster->set_title( "My Post's Title" ); 22 | $Poster->add_category(array(1,2,8)); 23 | $Poster->set_type( "post" ); 24 | $Poster->set_content( "This my awesome content" ); 25 | $Poster->set_author_id( 1 ); 26 | $Poster->set_post_slug( 'updated_post' ); 27 | //$Poster->set_page_template( "login-infusion-page.php" ); 28 | $Poster->set_post_state( "publish" ); 29 | 30 | $Poster->search('title', 'Old Post'); 31 | $Poster->update(); 32 | 33 | //$Poster->create(); 34 | 35 | //$Poster->PrettyPrintAll(); 36 | 37 | $Poster->get_var('slug'); 38 | 39 | ?> --------------------------------------------------------------------------------