Skip to content

Commit

Permalink
Merge pull request #3731 from Automattic/trunk
Browse files Browse the repository at this point in the history
Alpha release Feb 07
  • Loading branch information
chickenn00dle authored Feb 7, 2025
2 parents edc1531 + 5fdcab6 commit c75b7c7
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 28 deletions.
13 changes: 7 additions & 6 deletions includes/corrections/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ define( 'NEWSPACK_CORRECTIONS_ENABLED', true );

Corrections are stored as `newspack_correction` custom post type. A correction consists of the following fields:

| Name | Type | Stored As | Description |
| ----------------------------- | -------- | -------------- | --------------------------------------------------------------- |
| `title` | `string` | `post_title` | The correction title. Defaults to 'Correction for [post title]' |
| `content` | `string` | `post_content` | The correction text. |
| `date` | `string` | `post_date` | The date assigned to the correction. |
| `newspack_correction-post-id` | `int` | `post_meta` | The ID of the post to which the correction is associated. |
| Name | Type | Stored As | Description |
| ----------------------------- | -------- | -------------- | ------------------------------------------------------------------------------ |
| `title` | `string` | `post_title` | The correction title. Defaults to 'Correction for [post title]' |
| `content` | `string` | `post_content` | The correction text. |
| `date` | `string` | `post_date` | The date assigned to the correction. |
| `newspack_correction-post-id` | `int` | `post_meta` | The ID of the post to which the correction is associated. |
| `newspack_corrections_type` | `string` | `post_meta` | Whether it's a correction or a clarification (`correction` or `clarification`) |

In addition, some correction data is stored in the associated post as post meta:

Expand Down
74 changes: 65 additions & 9 deletions includes/corrections/class-corrections.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ class Corrections {
*/
const CORRECTIONS_LOCATION_META = 'newspack_corrections_location';

/**
* Meta key for post corrections type meta.
*/
const CORRECTIONS_TYPE_META = 'newspack_corrections_type';

/**
* Supported post types.
*/
const SUPPORTED_POST_TYPES = [ 'article_legacy', 'content_type_blog', 'post', 'press_release' ];

/**
* Initializes the class.
*/
Expand Down Expand Up @@ -134,14 +144,20 @@ public static function register_post_type() {
'public' => true,
'public_queryable' => true,
'query_var' => true,
'rewrite' => [ 'slug' => 'correction' ],
'rewrite' => [ 'slug' => 'corrections' ],
'show_ui' => false,
'show_in_rest' => true,
'supports' => $supports,
'taxonomies' => [],
'menu_icon' => 'dashicons-edit',
);
\register_post_type( self::POST_TYPE, $args );

$rewrite_rules_updated_option_name = 'newspack_corrections_rewrite_rules_updated';
if ( get_option( $rewrite_rules_updated_option_name ) !== true ) {
flush_rewrite_rules(); //phpcs:ignore
update_option( $rewrite_rules_updated_option_name, true );
}
}

/**
Expand All @@ -161,6 +177,7 @@ public static function add_corrections( $post_id, $corrections ) {
'post_status' => 'publish',
'meta_input' => [
self::CORRECTION_POST_ID_META => $post_id,
self::CORRECTIONS_TYPE_META => $correction['type'],
],
]
);
Expand Down Expand Up @@ -202,6 +219,9 @@ public static function update_correction( $correction_id, $correction ) {
'ID' => $correction_id,
'post_content' => sanitize_textarea_field( $correction['content'] ),
'post_date' => sanitize_text_field( $correction['date'] ),
'meta_input' => [
self::CORRECTIONS_TYPE_META => $correction['type'],
],
]
);
}
Expand All @@ -225,6 +245,32 @@ public static function add_corrections_shortcode() {
add_shortcode( 'corrections', [ __CLASS__, 'handle_corrections_shortcode' ] );
}

/**
* Gets the Correction type label for a given post. Defaults to the current global post if none is provided.
*
* @param int $post_id The correction id.
* @return string The correction type label.
*/
public static function get_correction_type( $post_id = null ) {
if ( ! $post_id ) {
$post_id = get_the_ID();
}
return self::get_correction_type_label( get_post_meta( $post_id, self::CORRECTIONS_TYPE_META, true ) );
}

/**
* Gets the correction type label.
*
* @param string $type the correction type.
* @return string the correction type label.
*/
private static function get_correction_type_label( $type ) {
if ( 'clarification' === $type ) {
return __( 'Clarification', 'newspack-plugin' );
}
return __( 'Correction', 'newspack-plugin' );
}

/**
* Handles the corrections shortcode.
*
Expand Down Expand Up @@ -288,8 +334,7 @@ public static function handle_corrections_shortcode() {
* @param string $post_type the post type.
*/
public static function add_corrections_metabox( $post_type ) {
$valid_post_types = [ 'article_legacy', 'content_type_blog', 'post', 'press_release' ];
if ( in_array( $post_type, $valid_post_types, true ) ) {
if ( in_array( $post_type, self::SUPPORTED_POST_TYPES, true ) ) {
add_meta_box(
'corrections',
'Corrections',
Expand Down Expand Up @@ -330,6 +375,7 @@ public static function render_corrections_metabox( $post ) {
foreach ( $corrections as $correction ) :
$correction_content = $correction->post_content;
$correction_date = \get_the_date( 'Y-m-d', $correction->ID );
$correction_type = get_post_meta( $correction->ID, self::CORRECTIONS_TYPE_META, true );
?>
<fieldset name="existing-corrections[<?php echo esc_attr( $correction->ID ); ?>]" class="correction">
<p><?php echo esc_html( __( 'Article Correction', 'newspack-plugin' ) ); ?></p>
Expand All @@ -339,6 +385,13 @@ public static function render_corrections_metabox( $post ) {
<?php echo esc_html( __( 'Date:', 'newspack_plugin' ) ); ?>
<input name="existing-corrections[<?php echo esc_attr( $correction->ID ); ?>][date]" type="date" value="<?php echo esc_attr( sanitize_text_field( $correction_date ) ); ?>">
</p>
<p>
<?php echo esc_html( __( 'Type:', 'newspack_plugin' ) ); ?>
<select name="existing-corrections[<?php echo esc_attr( $correction->ID ); ?>][type]" />
<option value="correction" <?php selected( $correction_type, 'correction' ); ?>><?php echo esc_html( self::get_correction_type_label( 'correction' ) ); ?></option>
<option value="clarification" <?php selected( $correction_type, 'clarification' ); ?>><?php echo esc_html( self::get_correction_type_label( 'clarification' ) ); ?></option>
</select>
</p>
<button class="delete-correction">X</button>
</fieldset>
<?php endforeach; ?>
Expand All @@ -357,8 +410,8 @@ public static function render_corrections_metabox( $post ) {
* @param int $post_id the post id.
*/
public static function save_corrections_metabox( $post_id ) {
// return early if we are saving a correction.
if ( self::POST_TYPE === get_post_type( $post_id ) ) {
// return early if we are saving an unsupported post type.
if ( ! in_array( get_post_type( $post_id ), self::SUPPORTED_POST_TYPES, true ) ) {
return;
}

Expand Down Expand Up @@ -414,6 +467,7 @@ public static function save_corrections_metabox( $post_id ) {
$corrections[] = [
'content' => sanitize_textarea_field( $correction['content'] ),
'date' => ! empty( $correction['date'] ) ? sanitize_text_field( $correction['date'] ) : gmdate( 'Y-m-d' ),
'type' => sanitize_text_field( $correction['type'] ),
];
}
self::add_corrections( $post_id, $corrections );
Expand Down Expand Up @@ -454,11 +508,13 @@ public static function output_corrections_on_post( $content ) {
<?php
foreach ( $corrections as $correction ) :
$correction_content = $correction->post_content;
$correction_date = \get_the_date( 'M j, Y', $correction->ID );
$correction_date = \get_the_date( get_option( 'date_format' ), $correction->ID );
$correction_time = \get_the_time( get_option( 'time_format' ), $correction->ID );
$correction_heading = sprintf(
// translators: %s: correction date.
__( 'Correction on %s', 'newspack-plugin' ),
$correction_date
'%s, %s %s',
self::get_correction_type_label( get_post_meta( $correction->ID, self::CORRECTIONS_TYPE_META, true ) ),
$correction_date,
$correction_time
);
?>
<!-- wp:paragraph {"fontSize":"small"} -->
Expand Down
14 changes: 14 additions & 0 deletions includes/plugins/class-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class WooCommerce {
public static function init() {
add_action( 'wp_loaded', [ __CLASS__, 'disable_wc_author_archive_override' ] );
add_filter( 'woocommerce_rest_prepare_shop_order_object', [ __CLASS__, 'modify_shop_order_wc_rest_api_payload' ] );
add_filter( 'woocommerce_create_pages', [ __CLASS__, 'use_shortcodes_for_cart_checkout' ] );
}

/**
Expand Down Expand Up @@ -52,5 +53,18 @@ public static function modify_shop_order_wc_rest_api_payload( $response ) {
$response->set_data( $data );
return $response;
}

/**
* Override the default page contents when creating the WooCommerce Cart and Checkout pages.
*
* @param array $woocommerce_pages Defaults for WooCommerce pages created on install.
* @return array
*/
public static function use_shortcodes_for_cart_checkout( $woocommerce_pages ) {
$woocommerce_pages['cart']['content'] = '<!-- wp:shortcode -->[woocommerce_cart]<!-- /wp:shortcode -->';
$woocommerce_pages['checkout']['content'] = '<!-- wp:shortcode -->[woocommerce_checkout]<!-- /wp:shortcode -->';

return $woocommerce_pages;
}
}
WooCommerce::init();
2 changes: 1 addition & 1 deletion includes/plugins/wc-memberships/class-memberships.php
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ public static function render_js() {
if ( ev.detail.authenticated && ! window?.newspackReaderActivation?.getPendingCheckout() ) {
if ( ras.overlays.get().length ) {
ras.on( 'overlay', function( ev ) {
if ( ! ev.detail.overlays.length ) {
if ( ! ev.detail.overlays.length && ! window?.newspackReaderActivation?.openNewslettersSignupModal ) {
window.location.reload();
}
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static function woocommerce_subscriptions_integration_init() {
* @return bool
*/
public static function is_active() {
return class_exists( 'WC_Subscriptions' );
return function_exists( 'WC' ) && class_exists( 'WC_Subscriptions' );
}

/**
Expand Down
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"@testing-library/react": "^12.1.4",
"@types/qs": "^6.9.17",
"@types/react": "^17.0.75",
"@wordpress/browserslist-config": "^6.15.0",
"@wordpress/browserslist-config": "^6.17.0",
"eslint": "^8.57.0",
"lint-staged": "^15.4.1",
"newspack-scripts": "^5.5.2",
Expand Down
18 changes: 15 additions & 3 deletions src/other-scripts/corrections/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,23 @@ domReady( () => {
const newCorrection = document.createElement( 'div' );
newCorrection.classList.add( 'correction' );
newCorrection.innerHTML = `
<fieldset name="new-corrections[${newCorrectionsCount}]">
<fieldset name="new-corrections[${ newCorrectionsCount }]">
<p>${ __( 'Article Correction', 'newspack-plugin' ) }</p>
<textarea name="new-corrections[${newCorrectionsCount}][content]" rows="3" cols="60"></textarea>
<textarea name="new-corrections[${ newCorrectionsCount }][content]" rows="3" cols="60"></textarea>
<br/>
<p>${ __( 'Date:', 'newspack-plugin' ) } <input type="date" name="new-corrections[${newCorrectionsCount}][date]"></p>
<p>${ __(
'Date:',
'newspack-plugin'
) } <input type="date" name="new-corrections[${ newCorrectionsCount }][date]"></p>
<p>${ __( 'Type:', 'newspack-plugin' ) }
<select name="new-corrections[${ newCorrectionsCount }][type]">
<option value="correction">${ __( 'Correction', 'newspack-plugin' ) }</option>
<option value="clarification">${ __(
'Clarification',
'newspack-plugin'
) }</option>
</select>
</p>
<button class="delete-correction">X</button>
</fieldset>
`;
Expand Down
2 changes: 1 addition & 1 deletion src/reader-activation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export function openNewslettersSignupModal( config = {} ) {
config = {
...{
onSuccess: null,
onDissmiss: null,
onDismiss: null,
onError: null,
initialState: null,
skipSuccess: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ class Newspack_Test_WooCommerce_Subscriptions extends WP_UnitTestCase {
*/
public function test_is_active() {
$is_active = WooCommerce_Subscriptions::is_active();
$this->assertTrue( $is_active, 'WooCommerce Subscriptions integration should be active when WC_Subscriptions class exists.' );
$this->assertFalse( $is_active, 'WooCommerce Subscriptions integration should not be active if the main WooCommerce plugin is not available.' );
}

/**
* Test WooCommerce_Subscriptions::is_enabled.
*/
public function test_is_enabled() {
$is_enabled = WooCommerce_Subscriptions::is_enabled();
$this->assertTrue( $is_enabled, 'WooCommerce Subscriptions integration should be enabled when RAS is enabled.' );
$this->assertFalse( $is_enabled, 'WooCommerce Subscriptions integration should not be active if the main WooCommerce plugin is not available.' );
}
}

0 comments on commit c75b7c7

Please sign in to comment.