Skip to content

Commit

Permalink
Social: Implement the fix for the title-less post (#35462)
Browse files Browse the repository at this point in the history
* Social: Fix titless posts showing up as auto-draft and flush rewrite rules.

* changelog

* Delete the flush rewrite option on plugin deactivation.

* Remove plugin_deactivation activities.

* Use options to check if the feature is enabled and add method to toggle the status of the feature.

* Update slug for social note.

* Fix typo and make methods non-static.
  • Loading branch information
spsiddarthan authored Feb 8, 2024
1 parent cd15410 commit 0848bea
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Implemented titless permalink fixes.
2 changes: 1 addition & 1 deletion projects/plugins/social/src/class-jetpack-social.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function () {
My_Jetpack_Initializer::init();
}
);
add_action( 'init', array( new Automattic\Jetpack\Social\Note(), 'register' ) );
add_action( 'init', array( new Automattic\Jetpack\Social\Note(), 'init' ) );

$this->manager = $connection_manager ? $connection_manager : new Connection_Manager();

Expand Down
66 changes: 61 additions & 5 deletions projects/plugins/social/src/class-note.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,44 @@
* Register the Jetpack Social Note custom post type.
*/
class Note {
const JETPACK_SOCIAL_NOTE_CPT = 'jetpack-social-note';
const FLUSH_REWRITE_RULES_FLUSHED = 'jetpack_social_rewrite_rules_flushed';

/**
* Register the Jetpack Social Note custom post type.
* Check if the feature is enabled.
*/
public static function register() {
if ( ! defined( 'JETPACK_SOCIAL_NOTES_ENABLED' ) || ! constant( 'JETPACK_SOCIAL_NOTES_ENABLED' ) ) {
public function enabled() {
return get_option( self::JETPACK_SOCIAL_NOTE_CPT );
}

/**
* Initialize the Jetpack Social Note custom post type.
*/
public function init() {
if ( ! self::enabled() ) {
return;
}
self::register_cpt();
add_action( 'wp_insert_post_data', array( new Note(), 'set_empty_title' ), 10, 2 );
}

/**
* Set the title to empty string.
*
* @param array $data The Post Data.
* @param array $post The Post.
*/
public function set_empty_title( $data, $post ) {
if ( self::JETPACK_SOCIAL_NOTE_CPT === $post['post_type'] && 'auto-draft' === $post['post_status'] ) {
$data['post_title'] = '';
}
return $data;
}

/**
* Register the Jetpack Social Note custom post type.
*/
public function register_cpt() {
$args = array(
'public' => true,
'labels' => array(
Expand Down Expand Up @@ -46,8 +76,34 @@ public static function register() {
'show_in_rest' => true,
'supports' => array( 'editor', 'thumbnail', 'publicize', 'activitypub' ),
'menu_icon' => 'dashicons-welcome-write-blog',

'rewrite' => array( 'slug' => 'sn' ),
);
register_post_type( 'jetpack-social-note', $args );
register_post_type( self::JETPACK_SOCIAL_NOTE_CPT, $args );
self::maybe_flush_rewrite_rules();
}

/**
* Flush rewrite rules so the post permalink works correctly for the Social Note CPT. Flushing is an expensive operation, so do only when necessary.
*
* @param boolean $force Force flush the rewrite rules.
*/
public function maybe_flush_rewrite_rules( $force = false ) {
if ( empty( get_option( self::FLUSH_REWRITE_RULES_FLUSHED ) ) || $force ) {
flush_rewrite_rules( false );
update_option( self::FLUSH_REWRITE_RULES_FLUSHED, true );
}
}

/**
* Toggle whether or not the Notes feature is enabled.
*/
public function toggle_enabled_status() {
if ( ! self::enabled() ) {
update_option( self::JETPACK_SOCIAL_NOTE_CPT, true );
} else {
delete_option( self::JETPACK_SOCIAL_NOTE_CPT );
}
// Delete this option, so the rules get flushe in maybe_flush_rewrite_rules when the CPT is registered.
delete_option( self::FLUSH_REWRITE_RULES_FLUSHED );
}
}
22 changes: 21 additions & 1 deletion projects/plugins/social/tests/php/test-class-jetpack-social.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

use Automattic\Jetpack\Connection\Manager as Connection_Manager;
use Automattic\Jetpack\Social\Note;
use WorDBless\BaseTestCase;

/**
Expand Down Expand Up @@ -56,7 +57,7 @@ public function test_publicize_module_is_activated_on_plugin_activation() {
}

/**
* Testh that the Publicize package isn't ensured without a user connection
* Test that the Publicize package isn't ensured without a user connection
*/
public function test_publicize_not_configured() {
$connection_manager = $this->createMock( Connection_Manager::class );
Expand All @@ -71,4 +72,23 @@ public function test_publicize_not_configured() {

$this->assertSame( 0, did_action( 'jetpack_feature_publicize_enabled' ) );
}

/**
* Test the social notes feature.
*/
public function test_social_notes() {
$note = new Note();
$note->init();
$this->assertEmpty( get_option( Note::FLUSH_REWRITE_RULES_FLUSHED ) );
update_option( Note::JETPACK_SOCIAL_NOTE_CPT, true );
$note->init();
$this->assertTrue( get_option( Note::FLUSH_REWRITE_RULES_FLUSHED ) );
$note->toggle_enabled_status();
$this->assertFalse( $note->enabled() );
$note->init();
$this->assertEmpty( get_option( Note::FLUSH_REWRITE_RULES_FLUSHED ) );
$note->toggle_enabled_status();
$note->init();
$this->assertTrue( get_option( Note::FLUSH_REWRITE_RULES_FLUSHED ) );
}
}

0 comments on commit 0848bea

Please sign in to comment.