Created
December 23, 2019 10:51
-
-
Save ilicfilip/0c7cb1a457b12382ea8777ef86bf3ecb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Dummy object class. | |
* | |
* @author ThemeFusion | |
* @copyright (c) Copyright by ThemeFusion | |
* @link https://theme-fusion.com | |
* @package Fusion-Library | |
* @since 2.2 | |
*/ | |
/** | |
* Get & set setting values. | |
*/ | |
class Fusion_Dummy_Post { | |
/** | |
* The one, true instance of this object. | |
* | |
* @static | |
* @access private | |
* @since 6.2 | |
* @var object | |
*/ | |
private static $instance; | |
/** | |
* The post type. | |
* | |
* @access protected | |
* @since 2.2 | |
* @var int | |
*/ | |
protected $post_type = 'fusion_dummy_post'; | |
/** | |
* Constructor. | |
* | |
* @access private | |
* @since 2.2 | |
* @return void | |
*/ | |
private function __construct() { | |
} | |
/** | |
* Creates or returns an instance of this class. | |
* | |
* @static | |
* @access public | |
* @since 6.2 | |
*/ | |
public static function get_instance() { | |
// If an instance hasn't been created and set to $instance create an instance and set it to $instance. | |
if ( null === self::$instance ) { | |
self::$instance = new Fusion_Dummy_Post(); | |
} | |
return self::$instance; | |
} | |
/** | |
* Inserts dummy post into database. | |
* | |
* @access public | |
* @since 6.2 | |
* | |
* @param array $postarr An array of elements that make up a post to update or insert. | |
* @return object WP_Error | |
*/ | |
public function insert_object( $postarr ) { | |
// Create post object. | |
$postarr = wp_parse_args( | |
$postarr, | |
[ | |
'post_title' => 'Test Title', | |
'post_content' => 'Test Content', | |
'post_status' => 'publish', | |
'post_author' => get_current_user_id(), | |
'post_type' => $this->post_type, | |
] | |
); | |
// Insert the post into the database. | |
return wp_insert_post( $postarr, true ); | |
} | |
/** | |
* Updates dummy post. | |
* | |
* @access public | |
* @since 6.2 | |
* | |
* @param array $postarr An array of elements that make up a post to update or insert. | |
* @return object WP_Error | |
*/ | |
public function update_object( $postarr ) { | |
// Insert the post into the database. | |
return wp_update_post( $postarr, true ); | |
} | |
/** | |
* Deletes dummy post from database. | |
* | |
* @access public | |
* @since 6.2 | |
* | |
* @param int $post_id ID of the post that should be deleted. | |
* @return mixed object|bool The post object (if it was deleted or moved to the trash successfully) or false (failure). | |
*/ | |
public function delete_object( $post_id ) { | |
return wp_delete_post( $post_id, true ); | |
} | |
} | |
Fusion_Dummy_Post::get_instance(); | |
/* Omit closing PHP tag to avoid "Headers already sent" issues. */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment