Skip to content

Commit

Permalink
Added post transitions methods for Post List & Design
Browse files Browse the repository at this point in the history
Added Action Hooks for handling post transitions to auto-draft/draft,
trash, and delete.

Additional Database Functionality for Advanced-Post-List#9 Advanced-Post-List#79
  • Loading branch information
EkoJR committed Jul 29, 2017
1 parent d3dd638 commit 98b49b9
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 37 deletions.
139 changes: 136 additions & 3 deletions admin/class-apl-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,18 @@ private function __construct() {
add_action( 'add_meta_boxes', array( $this, 'post_list_meta_boxes' ) );

// Post Data
add_action( 'draft_apl_post_list', array( $this, 'save_post_list' ), 10, 2 );
add_action( 'draft_apl_post_list', array( $this, 'draft_post_list' ), 10, 2 );

add_action( 'private_apl_post_list', array( $this, 'save_post_list' ), 10, 2 );
add_action( 'publish_apl_post_list', array( $this, 'save_post_list' ), 10, 2 );
add_action( 'pending_apl_post_list', array( $this, 'save_post_list' ), 10, 2 );
add_action( 'future_apl_post_list', array( $this, 'save_post_list' ), 10, 2 );
//add_action( 'trash_apl_post_list', array( $this, 'save_post_list' ), 10, 3 );

//add_action( 'trash_apl_post_list', array( $this, 'trash_post_list' ), 10, 3 );
add_action( 'wp_trash_post', array( $this, 'action_wp_trash_post_apl_post_list' ) );
add_action( 'untrash_post', array( $this, 'action_untrash_post_apl_post_list' ) );
add_action( 'before_delete_post', array( $this, 'action_before_delete_post_apl_post_list' ) );

/*
// Early Hook.
add_action( 'plugins_loaded', array( $this, 'hook_action_plugins_loaded' ) );
Expand Down Expand Up @@ -483,6 +488,34 @@ public function post_list_meta_box_display( $post, $metabox ) {
include( APL_DIR . 'admin/meta-box-design.php' );
}

public function draft_post_list( $post_id, $post ) {
if ( isset( $_REQUEST['action'] ) ) {
if ( 'untrash' === $_REQUEST['action'] ) {
return;
}
}
if ( empty( $post->post_name ) ) {
if ( empty( $post->post_title ) ) {
$post->post_title = 'APL-' . $post->ID;
}

remove_action('draft_apl_post_list', array( $this, 'draft_post_list' ) );
$post->post_name = sanitize_title_with_dashes( $post->post_title );

$postarr = array(
'ID' => $post->ID,
'post_title' => $post->post_title,
'post_name' => $post->post_name,
//'post_status' => $post->post_status,
);
wp_update_post( $postarr );

add_action( 'draft_apl_post_list', array( $this, 'draft_post_list' ), 10, 2 );
}

$this->post_list_process( $post_id, $post );
}

/**
* Save Post List.
*
Expand All @@ -504,10 +537,110 @@ public function save_post_list( $post_id, $post ) {
// Doesn't work if there is no action ( Add New )
//check_admin_referer( 'update-post_' . $post_id );

//add_action( 'private_apl_post_list', array( $this, 'save_post_list' ), 10, 2 );
//add_action( 'publish_apl_post_list', array( $this, 'save_post_list' ), 10, 2 );
//add_action( 'pending_apl_post_list', array( $this, 'save_post_list' ), 10, 2 );
//add_action( 'future_apl_post_list', array( $this, 'save_post_list' ), 10, 2 );

$this->post_list_process( $post_id, $post );

}

// MOVE TO APL_POST_LIST???
// https://codex.wordpress.org/Plugin_API/Action_Reference/before_delete_post
public function action_wp_trash_post_apl_post_list( $post_id ) {
$args = array(
'post__in' => array( $post_id ),
'post_type' => 'apl_post_list',
//'post_status' => 'trash',
);
$post_lists = new WP_Query( $args );
if ( 1 > $post_lists->post_count ) {
return false;
}
$post_list = $post_lists->post;

if ( 'apl_post_list' !== $post_list->post_type ) {
return;
}

$apl_post_list = new APL_Post_List( $post_list->post_name );

$apl_design = new APL_Design( $apl_post_list->pl_apl_design );

$new_post_list_slug = $post_list->post_name . '__trashed';
$new_design_slug = '';
if ( !empty( $post_list->post_name ) ) {
$slug_suffix = apply_filters( 'apl_design_slug_suffix', '-design' );
$design_slug = apply_filters( 'apl_design_trash_slug', $new_post_list_slug );
$new_design_slug = $design_slug . $slug_suffix;
}
$apl_post_list->pl_apl_design = $new_design_slug;
$apl_design->slug = $new_design_slug;

$apl_design->save_design();
}


public function action_untrash_post_apl_post_list( $post_id ) {
$args = array(
'post__in' => array( $post_id ),
'post_type' => 'apl_post_list',
'post_status' => 'trash',
);
$post_lists = new WP_Query( $args );
if ( 1 > $post_lists->post_count ) {
return false;
}
$post_list = $post_lists->post;

if ( 'apl_post_list' !== $post_list->post_type ) {
return;
}

$apl_post_list = new APL_Post_List( $post_list->post_name );

$apl_design = new APL_Design( $apl_post_list->pl_apl_design );

$new_post_list_slug = str_replace( '__trashed', '', $post_list->post_name );
$new_design_slug = '';
if ( !empty( $post_list->post_name ) ) {
$slug_suffix = apply_filters( 'apl_design_slug_suffix', '-design' );
$design_slug = apply_filters( 'apl_design_trash_slug', $new_post_list_slug );
$new_design_slug = $design_slug . $slug_suffix;
}
$apl_post_list->pl_apl_design = $new_design_slug;
$apl_design->slug = $new_design_slug;

$apl_design->save_design();
}

// MOVE TO APL_POST_LIST???
// before delete post hook https://codex.wordpress.org/Plugin_API/Action_Reference/before_delete_post
public function action_before_delete_post_apl_post_list( $post_id ) {


$args = array(
'post__in' => array( $post_id ),
'post_type' => 'apl_post_list',
'post_status' => 'trash',
);
$post_lists = new WP_Query( $args );
if ( 1 > $post_lists->post_count ) {
return false;
}
$post_list = $post_lists->post;

if ( 'apl_post_list' !== $post_list->post_type ) {
return;
}

$apl_post_list = new APL_Post_List( $post_list->post_name );
$apl_design = new APL_Design( $apl_post_list->pl_apl_design );

$apl_design->delete_design();
}

/**
* Process Post List Form.
*
Expand Down Expand Up @@ -817,7 +950,7 @@ private function post_list_process_apl_design( $apl_design_slug, $new_design_slu

// EMPTY MESSAGE.
$tmp_apl_design_empty = '';
if ( isset( $_POST['apl_empty_message'] ) ) {
if ( isset( $_POST['apl_empty_enable'] ) && isset( $_POST['apl_empty_message'] ) ) {
$tmp_apl_design_empty = filter_input( INPUT_POST, 'apl_empty_message', FILTER_UNSAFE_RAW );
}
$apl_design->empty = $tmp_apl_design_empty;
Expand Down
2 changes: 1 addition & 1 deletion admin/meta-box-design.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
</div>
<div>
<div style="margin: 3px 0px 3px 6px;">
<input type="checkbox" id="apl_empty_message_enable" class="apl-empty-message-enable" <?php echo !empty( $apl_design->empty ) ? 'checked="checked"' : ''; ?> />
<input type="checkbox" id="apl_empty_message_enable" class="apl-empty-message-enable" name="apl_empty_enable" <?php echo !empty( $apl_design->empty ) ? 'checked="checked"' : ''; ?> />
<label for="apl_empty_message_enable"><?php esc_html_e( 'Enable (Overwrites Default)', 'advanced-post-list' ) ?></label>
</div>
<textarea id="apl_textarea_empty_message" class="apl-textarea-empty-message large-text" name="apl_empty_message" rows="9" style="<?php echo empty( $apl_design->empty ) ? 'display: none;' : ''; ?>" ><?php echo $apl_design->empty; ?></textarea>
Expand Down
9 changes: 4 additions & 5 deletions admin/meta-box-filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,12 +381,11 @@ function apl_selected_post_status( $post_status, $apl_post_status ) {
}
}
}

if ( $first_tax && empty( $apl_post_list->tax_query[ $k_pt_slug ] ) ) {
$tax_display = 'display: block;';
}
$first_tax = false;
}
if ( $first_tax && empty( $apl_post_list->tax_query[ $k_pt_slug ] ) ) {
$tax_display = 'display: block;';
}
$first_tax = false;
?>
<li id="apl-t-li-<?php echo esc_attr( $k_pt_slug ); ?>-<?php echo esc_attr( $tax_slug ); ?>" style="<?php echo $tax_display; ?>"><a href="#apl-t-div-<?php echo esc_attr( $k_pt_slug ); ?>-<?php echo esc_attr( $tax_slug ); ?>"><?php echo esc_html( $apl_taxonomy_objs[ $tax_slug ]->labels->singular_name ); ?></a></li>
<?php endforeach; ?>
Expand Down
15 changes: 8 additions & 7 deletions includes/class/class-apl-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public function __construct( $file ) {
* @access private
*
* @see Function/method/class relied on
* @link https://hitchhackerguide.com/2011/02/12/get_plugin_data/
*
* @param string $plugin_file Main plugin file.
*/
Expand Down Expand Up @@ -262,7 +263,7 @@ public function hook_action_register_post_type_post_list() {
'attributes' => __( 'Post List Attributes', 'advanced-post-list' ),
'insert_into_item' => __( 'Insert into Post List', 'advanced-post-list' ),
'uploaded_to_this_item' => __( 'Upload to this Post List', 'advanced-post-list' ),
'menu_name' => __( 'APL Post List', 'advanced-post-list' ),
'menu_name' => __( 'Adv. Post List', 'advanced-post-list' ),
),
'description' => __( 'APL Preset Post Lists.', 'advanced-post-list' ),
'public' => true,
Expand All @@ -276,9 +277,9 @@ public function hook_action_register_post_type_post_list() {
'hierarchical' => true,
'supports' => array(
'title',
'author',
'thumbnail',
'revisions',
//'author',
//'thumbnail',
//'revisions',
),
'has_archive' => false,
'rewrite' => array(
Expand Down Expand Up @@ -339,9 +340,9 @@ public function hook_action_register_post_type_design() {
'hierarchical' => true,
'supports' => array(
'title',
'thumbnail',
'excerpt',
'revisions',
//'thumbnail',
//'excerpt',
//'revisions',
),
'has_archive' => false,
'rewrite' => array(
Expand Down
41 changes: 27 additions & 14 deletions includes/class/class-apl-design.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function __construct( $design_name ) {
// Draft/Init Hook.

// Save Design Meta Data Hook.
add_action( 'save_post_apl_design', array( &$this, 'hook_action_save_post_apl_design' ) );
add_action( 'save_post_apl_design', array( &$this, 'hook_action_save_post_apl_design' ), 10, 3 );
// Delete Design Hook.
// Import / Export Hook.
}
Expand All @@ -128,7 +128,14 @@ private function get_data( $args = array() ) {
$defaults = array(
'name' => '',
'post_type' => 'apl_design',
//'post_status' => 'publish',
'post_status' => array(
'draft',
'pending',
'publish',
'future',
'private',
'trash',
),
'posts_per_page' => 1,
);
$args = wp_parse_args( $args, $defaults );
Expand Down Expand Up @@ -171,27 +178,34 @@ public function save_design() {
return;
}
$get_args = array(
'ID' => $this->id,
//'name' => $this->slug,
'post_type' => 'apl_design',
'post__in' => array( $this->id ),
//'name' => $this->slug,
'post_type' => 'apl_design',
);
$designs = new WP_Query( $get_args );

$save_args = array(
$save_postarr = array(
'ID' => $this->id,
'post_title' => $this->title,
'post_name' => $this->slug,
'post_status' => 'publish',
'post_type' => 'apl_design',
);

if ( 0 < $designs->post_count ) {
$this->insert_design_post( $save_args );
if ( 1 > $designs->post_count || 0 === $this->id ) {
$this->insert_design_post( $save_postarr );
} else {
$this->update_design_post( $save_args );
$this->update_design_post( $save_postarr );
}
}

// https://codex.wordpress.org/Function_Reference/wp_delete_post
public function delete_design() {

wp_delete_post( $this->id, true );

}

/**
* Insert Design post data.
*
Expand Down Expand Up @@ -285,11 +299,10 @@ private function default_postarr() {
* @param int $post_id Post ID that is past by WP when saving post.
* @return void
*/
public function hook_action_save_post_apl_design( $post_id ) {
$args = array(
'ID' => $post_id,
);
$design_post = new WP_Query( $args );
public function hook_action_save_post_apl_design( $post_id, $post_obj, $update ) {
$this->id = $post_id;
$this->title = $post_obj->post_title;
$this->slug = $post_obj->post_name;

$old_before = get_post_meta( $this->id, 'apl_before', true );
$old_content = get_post_meta( $this->id, 'apl_content', true );
Expand Down
25 changes: 18 additions & 7 deletions includes/class/class-apl-post-list.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ class APL_Post_List {
* @return void
*/
public function __construct( $post_list_name ) {
$this->title = $post_list_name;
//$this->title = $post_list_name;
$this->slug = (string) sanitize_title_with_dashes( $post_list_name );
$args = array(
'name' => $this->slug,
Expand Down Expand Up @@ -294,9 +294,16 @@ public function __construct( $post_list_name ) {
*/
private function get_data( $args = array() ) {
$defaults = array(
'name' => '',
'post__in' => array(),
'post_type' => 'apl_post_list',
//'post_status' => 'publish',
'post_status' => array(
'draft',
'pending',
'publish',
'future',
'private',
'trash',
),
'posts_per_page' => 1,
);
$args = wp_parse_args( $args, $defaults );
Expand Down Expand Up @@ -361,22 +368,22 @@ public function save_post_list() {
return;
}
$get_args = array(
'ID' => $this->id,
'post__in' => array( $this->id ),
'post_type' => 'apl_post_list',
);
$post_lists = new WP_Query( $get_args );

$save_args = array(
$save_postarr = array(
'ID' => $this->id,
'post_title' => $this->title,
'post_name' => $this->slug,
'post_status' => 'publish',
'post_type' => 'apl_post_list',
);
if ( 0 < $post_lists->post_count ) {
$this->insert_post_list_post( $save_args );
$this->insert_post_list_post( $save_postarr );
} else {
$this->update_post_list_post( $save_args );
$this->update_post_list_post( $save_postarr );
}
}

Expand Down Expand Up @@ -473,6 +480,10 @@ private function update_post_list_post( $args = array() ) {
* @return void
*/
public function hook_action_save_post_apl_post_list( $post_id, $post_obj, $update ) {
$this->id = $post_id;
$this->title = $post_obj->post_title;
$this->slug = $post_obj->post_name;

$old_post_type = get_post_meta( $this->id, 'apl_post_type', true );
$old_tax_query = get_post_meta( $this->id, 'apl_tax_query', true );
$old_post_parent__in = get_post_meta( $this->id, 'apl_post_parent__in', true );
Expand Down

0 comments on commit 98b49b9

Please sign in to comment.