Last active
July 10, 2017 04:29
-
-
Save debabratakarfa/90204a6bed91347ad895f17fd6753252 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 | |
if ( ! class_exists( 'AapWidget' ) ) { | |
class AapWidget { | |
public $timestamp; | |
public $publish_category; | |
public $archive_category; | |
public $private_category; | |
public static $instance; | |
private $instanced = false; | |
private $output; | |
private $taxonomy; | |
public | |
function __construct() { | |
self::$instance = $this; | |
$this->publish_category = wpcom_vip_get_category_by_slug( 'publish' ); | |
$this->archive_category = wpcom_vip_get_category_by_slug( 'archive' ); | |
$this->private_category = wpcom_vip_get_category_by_slug( 'private' ); | |
$this->instanced = true; | |
$this->taxonomy = new Taxonomy(); | |
add_action('admin_menu', array( | |
$this, | |
'register_submenu', | |
)); | |
} | |
public static function register_submenu() { | |
add_submenu_page('edit.php', 'Archive and Publish', 'Generate', 'edit_posts', 'archive-and-publish', array( | |
'AapWidget', | |
'page', | |
)); | |
} | |
public static function loada_assets() { | |
wp_register_style( 'bootstrap', plugins_url( 'css/bootstrap.min.css', __FILE__ ) ); | |
wp_register_style( 'datetimepicker', plugins_url( 'css/datetimepicker.css', __FILE__ ) ); | |
wp_register_script( 'bootstrap', plugins_url( 'js/bootstrap.min.js', __FILE__ ) ); | |
wp_register_script( 'datepicker', plugins_url( 'js/jquery.datepicker.min.js', __FILE__ ) ); | |
wp_enqueue_style( 'bootstrap' ); | |
wp_enqueue_style( 'datetimepicker' ); | |
wp_enqueue_script( 'bootstrap' ); | |
wp_enqueue_script( 'datepicker' ); | |
} | |
public static function page() { | |
AapWidget::loada_assets(); | |
date_default_timezone_set( 'America/Chicago' ); | |
$instance = AapWidget::getInstance(); | |
// $categoryCheck = $instance->categoryChecks(); | |
if ( ! empty( $_POST['deleteDate'] ) ) { | |
$instance->doDelete( $_POST['deleteDate'] ); | |
return; | |
} | |
if ( ! empty( $_POST['date'] ) ) { $instance->addSchedule(); | |
} | |
if ( ! isset( $_POST['process'] ) ) { | |
$instance->schedulePage(); | |
return; | |
} | |
$archived_posts = array(); | |
$published_posts = array(); | |
$instance->timestamp = time(); | |
$posts = WP_Query(array( | |
'numberposts' => 40, | |
'order' => 'DESC', | |
)); | |
$should_process = false; | |
foreach ( $posts as $post ) { | |
if ( $instance->taxonomy->isPrivate( $post->ID ) ) { | |
$should_process = true; | |
} | |
} | |
if ( ! $should_process ) { | |
$instance->output .= 'No private posts found. <br />'; | |
$instance->schedulePage(); | |
return; | |
} | |
foreach ( $posts as $post ) { | |
if ( $instance->taxonomy->isPublish( $post->ID ) ) { | |
$instance->archivePost( $post ); | |
$archived_posts[] = $post->post_title; | |
} elseif ( $instance->taxonomy->isPrivate( $post->ID ) ) { | |
$tags = get_the_terms( $post->ID ); | |
if ( empty( $tags ) ) { | |
wp_set_post_tags( $post->ID, $instance->timestamp, true ); | |
$instance->publishPost( $post ); | |
$published_posts[] = $post->post_title; | |
} | |
} | |
} | |
$instance->output = ''; | |
$instance->output .= 'Done.<br />'; | |
$instance->output .= 'Archived ' . sizeof( $archived_posts ) . ' posts:<br />'; | |
foreach ( $archived_posts as $post ) { $instance->output .= " -- $post <br />"; | |
} | |
$instance->output .= 'Published ' . sizeof( $published_posts ) . ' posts:<br />'; | |
foreach ( $published_posts as $post ) { $instance->output .= " -- $post <br />"; | |
} | |
if ( (sizeof( $published_posts ) + sizeof( $archived_posts )) > 0 ) { | |
if ( $instance->createPage() ) { $instance->output .= 'Reference page with date ' . $instance->timestamp . ' created.<br />'; | |
} | |
} | |
if ( ! empty( $_POST['toSchedule'] ) ) { | |
$instance->schedulePage(); | |
} | |
} | |
public function archive_post( $post ) { | |
$this->taxonomy->setTerm( $post->ID, Taxonomy::ARCHIVE_TERM ); | |
} | |
public function publish_post( $post ) { | |
$this->taxonomy->setTerm( $post->ID, Taxonomy::PUBLISH_TERM ); | |
} | |
public function tags_check( $tags ) { | |
foreach ( $tags as $tag ) { | |
if ( preg_match( '/[0-9]{10}/', $tag->name ) ) { return true; | |
} | |
} | |
return false; | |
} | |
} | |
}// End if(). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment