Skip to content

Commit 79ad4b6

Browse files
authored
Merge branch 'release/v2.5.0' into fix/GH-867/linked-image-align
2 parents f230b40 + a89c789 commit 79ad4b6

File tree

3 files changed

+86
-13
lines changed

3 files changed

+86
-13
lines changed

apple-news.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,3 @@ function apple_news_is_classic_editor_plugin_active() {
200200

201201
return false;
202202
}
203-
204-
/**
205-
* Given a user ID, a post ID, and an action, determines whether a user can
206-
* perform the action or not.
207-
*
208-
* @param int $post_id The ID of the post to check.
209-
* @param string $action The action to check. One of 'publish', 'update', 'delete'.
210-
* @param int $user_id The user ID to check.
211-
*
212-
* @return bool True if the user can perform the action, false otherwise.
213-
*/

includes/apple-exporter/components/class-body.php

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use Apple_Exporter\Theme;
1212
use DOMElement;
13+
use WP_HTML_Tag_Processor;
1314

1415
/**
1516
* A paragraph component.
@@ -35,6 +36,13 @@ class Body extends Component {
3536
*/
3637
protected $can_be_anchor_target = true;
3738

39+
/**
40+
* Set default value for parent text alignment property.
41+
*
42+
* @var string
43+
*/
44+
protected $text_alignment = 'left';
45+
3846
/**
3947
* Look for node matches for this component.
4048
*
@@ -146,6 +154,21 @@ public function register_specs(): void {
146154
$default_spec
147155
);
148156

157+
$this->register_spec(
158+
'default-body-center',
159+
__( 'Centered Text', 'apple-news' ),
160+
[
161+
'textAlignment' => 'center',
162+
],
163+
);
164+
$this->register_spec(
165+
'default-body-right',
166+
__( 'Right-Aligned Text', 'apple-news' ),
167+
[
168+
'textAlignment' => 'right',
169+
],
170+
);
171+
149172
$dropcap_color_dark = $theme->get_value( 'dropcap_color_dark' );
150173
$dropcap_background_color_dark = $theme->get_value( 'dropcap_background_color_dark' );
151174

@@ -290,6 +313,7 @@ private static function split_unsupported_elements( $html, $tag, $open, $close )
290313
* @access protected
291314
*/
292315
protected function build( $html ) {
316+
$origin = $html;
293317

294318
// If there is no text for this element, bail.
295319
$html = $this->parser->parse( $html );
@@ -298,6 +322,22 @@ protected function build( $html ) {
298322
return;
299323
}
300324

325+
// Determine the text alignment from the first tag in the original HTML, which still has the alignment attributes.
326+
$proc = new WP_HTML_Tag_Processor( $origin );
327+
if ( false !== $proc->next_tag() ) {
328+
// `has_class()` is available in 6.4+.
329+
$class = (string) $proc->get_attribute( 'class' );
330+
$style = (string) $proc->get_attribute( 'style' );
331+
332+
if ( preg_match( '/\bhas-text-align-center\b/i', $class ) || preg_match( '/\btext-align:\s*center\b/i', $style ) ) {
333+
$this->text_alignment = 'center';
334+
}
335+
336+
if ( preg_match( '/\bhas-text-align-right\b/i', $class ) || preg_match( '/\btext-align:\s*right\b/i', $style ) ) {
337+
$this->text_alignment = 'right';
338+
}
339+
}
340+
301341
// Add the JSON for this component.
302342
$this->register_json(
303343
'json',
@@ -477,7 +517,7 @@ private function get_default_text_styles() {
477517
'fontSize' => '#cite_size#',
478518
'tracking' => '#cite_tracking#',
479519
'lineHeight' => '#cite_line_height#',
480-
'textColor' => '#cite_color',
520+
'textColor' => '#cite_color#',
481521
],
482522
$conditionals['cite'] ?? []
483523
),
@@ -487,7 +527,7 @@ private function get_default_text_styles() {
487527
'fontSize' => '#monospaced_size#',
488528
'tracking' => '#monospaced_tracking#',
489529
'lineHeight' => '#monospaced_line_height#',
490-
'textColor' => '#monospaced_color',
530+
'textColor' => '#monospaced_color#',
491531
],
492532
$conditionals['monospaced'] ?? []
493533
),
@@ -523,12 +563,32 @@ private function get_default_text_styles() {
523563
* @access public
524564
*/
525565
public function set_default_style() {
566+
// Always register the default style.
526567
$this->register_style(
527568
'default-body',
528569
'default-body',
529570
$this->get_default_style_values(),
530571
'textStyle'
531572
);
573+
574+
// If necessary, register the styles that are expected to override the 'textAlignment' property.
575+
if ( 'center' === $this->text_alignment ) {
576+
$this->register_style(
577+
'default-body-center',
578+
'default-body-center',
579+
[],
580+
'textStyle'
581+
);
582+
}
583+
584+
if ( 'right' === $this->text_alignment ) {
585+
$this->register_style(
586+
'default-body-right',
587+
'default-body-right',
588+
[],
589+
'textStyle'
590+
);
591+
}
532592
}
533593

534594
/**

tests/apple-exporter/components/test-class-body.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,4 +596,28 @@ public function test_without_dropcap() {
596596
$this->assertEquals( '<p>Paragraph 2.</p>', $json['components'][3]['text'] );
597597
$this->assertEquals( 'default-body', $json['components'][3]['textStyle'] );
598598
}
599+
600+
/**
601+
* Tests alignment textStyles.
602+
*/
603+
public function test_alignment_textstyles() {
604+
$content = <<<HTML
605+
<!-- wp:paragraph {"align":"center"} -->
606+
<p class="has-text-align-center">Paragraph 1.</p>
607+
<!-- /wp:paragraph -->
608+
609+
<!-- wp:paragraph {"align":"right"} -->
610+
<p class="has-text-align-right">Paragraph 2.</p>
611+
<!-- /wp:paragraph -->
612+
HTML;
613+
614+
$post_id = self::factory()->post->create( [ 'post_content' => $content ] );
615+
$json = $this->get_json_for_post( $post_id );
616+
617+
$center = array_filter( $json['components'], fn ( $c ) => '<p>Paragraph 1.</p>' === $c['text'] );
618+
$right = array_filter( $json['components'], fn ( $c ) => '<p>Paragraph 2.</p>' === $c['text'] );
619+
620+
$this->assertSame( 'default-body-center', array_pop( $center )['textStyle'] );
621+
$this->assertSame( 'default-body-right', array_pop( $right )['textStyle'] );
622+
}
599623
}

0 commit comments

Comments
 (0)