Skip to content

Commit

Permalink
Introduce pressforward_draft_post_type() and use throughout.
Browse files Browse the repository at this point in the history
See #1160.
  • Loading branch information
boonebgorges committed Apr 17, 2023
1 parent 4265902 commit 80de570
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Controllers/PF_Advancement.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function last_step_state() {
* @return string
*/
public function last_step_post_type() {
return get_option( PF_SLUG . '_draft_post_type', 'post' );
return pressforward_draft_post_type();
}

/**
Expand Down Expand Up @@ -277,7 +277,7 @@ public function pf_get_posts_by_id_for_check( $post_type = false, $item_id = nul
'meta_key' => $this->metas->get_key( 'item_id' ),
'meta_value' => $item_id,
// phpcs:enable WordPress.DB.SlowDBQuery
'post_type' => array( 'post', pf_feed_item_post_type() ),
'post_type' => array( pressforward_draft_post_type(), pf_feed_item_post_type() ),
);

if ( $ids_only ) {
Expand Down
2 changes: 1 addition & 1 deletion Controllers/PF_to_WP_Posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function insert_post( $post, $error = false, $item_id = false ) {
if ( isset( $post['post_date'] ) && ! isset( $post['post_date_gmt'] ) ) {
$post['post_date_gmt'] = get_gmt_from_date( $post['post_date'] );
}
if ( ( false !== $item_id ) && in_array( $post['post_type'], array( 'post', 'pf_feed_item', 'nomination' ), true ) ) {
if ( ( false !== $item_id ) && in_array( $post['post_type'], array( pressforward_draft_post_type(), 'pf_feed_item', 'nomination' ), true ) ) {
$check = $this->check_not_existing( $item_id, $post['post_type'] );
if ( true !== $check ) {
return $check;
Expand Down
2 changes: 1 addition & 1 deletion Controllers/Stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ private function post_status_query_args() {
* @return string
*/
private function post_type_for_query() {
return get_option( PF_SLUG . '_draft_post_type', 'post' );
return pressforward_draft_post_type();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Core/Admin/EditPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class EditPost implements HasActions {
* @return void
*/
public function __construct() {
$this->post_type = 'post';
$this->post_type = pressforward_draft_post_type();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Core/Admin/Nominated.php
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ public function get_first_nomination( $item_id, $post_type ) {
*/
public function is_nominated( $item_id, $post_type = '', $update = false ) {
if ( ! $post_type ) {
$post_type = array( 'post', 'nomination' );
$post_type = array( pressforward_draft_post_type(), 'nomination' );
}
$attempt = $this->get_first_nomination( $item_id, $post_type );
if ( ! empty( $attempt ) ) {
Expand Down
2 changes: 1 addition & 1 deletion Core/Bookmarklet/NominateThisCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function submit_meta_box() {

$create_nom_post_cap_test = current_user_can( get_post_type_object( pressforward( 'schema.nominations' )->post_type )->cap->create_posts );

$pf_draft_post_type_value = get_option( PF_SLUG . '_draft_post_type', 'post' );
$pf_draft_post_type_value = pressforward_draft_post_type();

if ( 'draft' === $publish_type ) {
$cap = 'edit_posts';
Expand Down
6 changes: 3 additions & 3 deletions Core/Utility/Forward_Tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ public function item_to_nomination( $item_id, $item_post_id ) {
* @return int|bool
*/
public function nomination_to_last_step( $item_id = '', $nomination_id = 0, $make_readable = true ) {
$post_check = $this->is_a_pf_type( $item_id, 'post' );
$post_check = $this->is_a_pf_type( $item_id, pressforward_draft_post_type() );

// Assign user status as well here.
if ( ! $post_check ) {
Expand Down Expand Up @@ -777,7 +777,7 @@ public function bookmarklet_to_nomination( $item_id = '', $post = [] ) {

// Update the existing post with values from the bookmarklet, which is assumed more accurate.
$post['ID'] = $nom_and_post_check;
$post_check = $this->is_a_pf_type( $item_id, 'post' );
$post_check = $this->is_a_pf_type( $item_id, pressforward_draft_post_type() );

// If this is a nomination but has not yet been published, assume bookmarklet has best version of content.
if ( false !== $post_check ) {
Expand Down Expand Up @@ -835,7 +835,7 @@ public function bookmarklet_to_last_step( $item_id = '', $post = null ) {
*/
public function is_a_pf_type( $item_id, $post_type = '', $update = false ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
if ( ! $post_type ) {
$post_type = array( 'post', pressforward( 'schema.nominations' )->post_type );
$post_type = array( pressforward_draft_post_type(), pressforward( 'schema.nominations' )->post_type );
}

$attempt = $this->advance_interface->get_pf_type_by_id( $item_id, $post_type );
Expand Down
26 changes: 22 additions & 4 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,24 @@ function pf_nomthis_bookmarklet() {
return apply_filters( 'pf_nomthis_bookmarklet', $link );
}

/**
* Get the draft post type name.
*
* @since 5.5.0
*
* @return string The name of the draft post_type for PressForward.
*/
function pressforward_draft_post_type() {
$post_type = get_option( PF_SLUG . '_draft_post_type', 'post' );

/**
* Filters the 'draft' post type.
*
* @param string Defaults to 'post'.
*/
return apply_filters( 'pressforward_draft_post_type', $post_type );
}

/**
* Get the feed item post type name.
*
Expand Down Expand Up @@ -394,7 +412,7 @@ function pf_get_posts_by_id_for_check( $post_type = null, $item_id = null, $ids_
$r = array(
'meta_key' => pressforward( 'controller.metas' )->get_key( 'item_id' ),
'meta_value' => $item_id,
'post_type' => array( 'post', pf_feed_item_post_type() ),
'post_type' => array( pressforward_draft_post_type(), pf_feed_item_post_type() ),
);

if ( $ids_only ) {
Expand Down Expand Up @@ -955,7 +973,7 @@ function pf_is_drafted( $item_id ) {
'fields' => 'ids',
'meta_key' => pressforward( 'controller.metas' )->get_key( 'item_id' ),
'meta_value' => $item_id,
'post_type' => get_option( PF_SLUG . '_draft_post_type', 'post' ),
'post_type' => pressforward_draft_post_type(),
);
$q = new WP_Query( $a );

Expand All @@ -976,7 +994,7 @@ function pf_is_drafted( $item_id ) {
function pf_get_drafted_items( $post_type = 'pf_feed_item' ) {
$draft_query_args = [
'no_found_rows' => true,
'post_type' => get_option( PF_SLUG . '_draft_post_type', 'post' ),
'post_type' => pressforward_draft_post_type(),
'post_status' => 'any',
'meta_query' => array(
array(
Expand Down Expand Up @@ -1415,7 +1433,7 @@ function pf_exclude_queued_items_from_queries( $query ) {
}

$type = $query->get( 'post_type' );
if ( ( empty( $type ) ) || ( 'post' !== $type ) ) {
if ( ( empty( $type ) ) || ( pressforward_draft_post_type() !== $type ) ) {
if ( 300 <= count( $queued ) ) {
$queued_chunk = array_chunk( $queued, 100 );
$queued = $queued_chunk[0];
Expand Down
2 changes: 1 addition & 1 deletion parts/settings/tab-site.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
<select name="<?php echo esc_attr( PF_SLUG ); ?>_draft_post_type" id="<?php echo esc_attr( PF_SLUG ); ?>_draft_post_type">
<?php
$the_post_types = get_post_types( array( 'public' => true ), 'objects' );
$pf_draft_post_type_value = get_option( PF_SLUG . '_draft_post_type', 'post' );
$pf_draft_post_type_value = pressforward_draft_post_type();
?>

<?php foreach ( $the_post_types as $the_post_type ) : ?>
Expand Down
8 changes: 4 additions & 4 deletions tests/test-nominate-this.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ public function query_check( $item_id, $post_type = false, $ids_only = false ){
// $theDate = getdate();
// $w = date('W');
$r = array(
'meta_key' => pressforward('controller.metas')->get_key('item_id'),
'meta_value' => $item_id,
'post_type' => array( 'post', pf_feed_item_post_type() ),
);
'meta_key' => pressforward('controller.metas')->get_key('item_id'),
'meta_value' => $item_id,
'post_type' => array( pressforward_draft_post_type(), pf_feed_item_post_type() ),
);

if ( $ids_only ) {
$r['fields'] = 'ids';
Expand Down
12 changes: 6 additions & 6 deletions tests/test-nomination-process.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ public function test_sequential_bookmarklet_to_nomination(){
$user_id_2 = $this->factory->user->create( array( 'role' => 'administrator', 'user_login' => 'bookmarklet_to_nomination2' ) );
wp_set_current_user( $user_id_2 );

$post_type = array( 'post', pressforward( 'schema.nominations' )->post_type );
$post_type = array( pressforward_draft_post_type(), pressforward( 'schema.nominations' )->post_type );

// Check by item_id.
$nom_and_post_check = pressforward('utility.forward_tools')->is_a_pf_type( $item_id, pressforward( 'schema.nominations' )->post_type );
Expand Down Expand Up @@ -570,7 +570,7 @@ public function test_sequential_bookmarklet_to_nomination_followed_by_feed_item(
$user_id_2 = $this->factory->user->create( array( 'role' => 'administrator', 'user_login' => 'bookmarklet_to_nomination2' ) );
wp_set_current_user( $user_id_2 );

$post_type = array( 'post', pressforward( 'schema.nominations' )->post_type );
$post_type = array( pressforward_draft_post_type(), pressforward( 'schema.nominations' )->post_type );

// Check by item_id.
$nom_and_post_check = pressforward('utility.forward_tools')->is_a_pf_type( $item_id, pressforward( 'schema.nominations' )->post_type );
Expand Down Expand Up @@ -691,7 +691,7 @@ public function test_sequential_feed_item_to_nomination_followed_by_bookmarklet(
$user_id_2 = $this->factory->user->create( array( 'role' => 'administrator', 'user_login' => 'bookmarklet_to_nomination2' ) );
wp_set_current_user( $user_id_2 );

$post_type = array( 'post', pressforward( 'schema.nominations' )->post_type );
$post_type = array( pressforward_draft_post_type(), pressforward( 'schema.nominations' )->post_type );

// Check by item_id.
$nom_and_post_check = pressforward('utility.forward_tools')->is_a_pf_type( $item_id, pressforward( 'schema.nominations' )->post_type );
Expand Down Expand Up @@ -869,7 +869,7 @@ public function test_feed_item_to_last_step_followed_by_nomination() {
$this->check_standard_metrics($feed_item_id, $final_id, $title);

$final_post = get_post($final_id);
$this->assertEquals($final_post->post_type, 'post');
$this->assertEquals($final_post->post_type, pressforward_draft_post_type());

$nominate = pressforward('controller.metas')->get_post_pf_meta($final_id, 'nom_id');

Expand Down Expand Up @@ -902,9 +902,9 @@ public function test_feed_item_to_last_step_followed_by_nomination() {

$nomination_and_post_check = pressforward('utility.forward_tools')->is_a_pf_type( $item_id, pressforward( 'schema.nominations' )->post_type );
$this->assertEquals($nomination_and_post_check, $nomination_two_id);
$this->assertEquals( get_option( PF_SLUG . '_draft_post_type', 'post' ), 'post' );
$this->assertEquals( pressforward_draft_post_type(), 'post' );
$final_post = get_post($final_id);
$this->assertEquals( get_option( PF_SLUG . '_draft_post_type', 'post' ), $final_post->post_type );
$this->assertEquals( pressforward_draft_post_type(), $final_post->post_type );
$this->assertEquals( pressforward('controller.metas')->get_post_pf_meta( $final_id, 'item_id', true ), $item_id );

// $nominators = pressforward('utility.forward_tools')->apply_nomination_data($final_id. false, true);
Expand Down

0 comments on commit 80de570

Please sign in to comment.