Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/jetpack/extensio.../blocks/subscrip...
File: class-jetpack-subscription-site.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Adds support for Jetpack Subscription Site feature.
[2] Fix | Delete
*
[3] Fix | Delete
* @package automattic/jetpack
[4] Fix | Delete
* @since 13.2
[5] Fix | Delete
*/
[6] Fix | Delete
[7] Fix | Delete
namespace Automattic\Jetpack\Extensions\Subscriptions;
[8] Fix | Delete
[9] Fix | Delete
use Jetpack_Memberships;
[10] Fix | Delete
use WP_Block_Template;
[11] Fix | Delete
use WP_Post;
[12] Fix | Delete
[13] Fix | Delete
/**
[14] Fix | Delete
* Jetpack_Subscription_Site class.
[15] Fix | Delete
*/
[16] Fix | Delete
class Jetpack_Subscription_Site {
[17] Fix | Delete
/**
[18] Fix | Delete
* Jetpack_Subscription_Site singleton instance.
[19] Fix | Delete
*
[20] Fix | Delete
* @var Jetpack_Subscription_Site|null
[21] Fix | Delete
*/
[22] Fix | Delete
private static $instance;
[23] Fix | Delete
[24] Fix | Delete
/**
[25] Fix | Delete
* Jetpack_Subscription_Site instance init.
[26] Fix | Delete
*/
[27] Fix | Delete
public static function init() {
[28] Fix | Delete
if ( self::$instance === null ) {
[29] Fix | Delete
self::$instance = new Jetpack_Subscription_Site();
[30] Fix | Delete
}
[31] Fix | Delete
[32] Fix | Delete
return self::$instance;
[33] Fix | Delete
}
[34] Fix | Delete
[35] Fix | Delete
/**
[36] Fix | Delete
* Handles Subscribe block placements.
[37] Fix | Delete
*
[38] Fix | Delete
* @return void
[39] Fix | Delete
*/
[40] Fix | Delete
public function handle_subscribe_block_placements() {
[41] Fix | Delete
$this->handle_subscribe_block_post_end_placement();
[42] Fix | Delete
$this->handle_subscribe_block_navigation_placement();
[43] Fix | Delete
}
[44] Fix | Delete
[45] Fix | Delete
/**
[46] Fix | Delete
* Returns true if current user can view the post.
[47] Fix | Delete
*
[48] Fix | Delete
* @return bool
[49] Fix | Delete
*/
[50] Fix | Delete
protected function user_can_view_post() {
[51] Fix | Delete
if ( ! class_exists( 'Jetpack_Memberships' ) ) {
[52] Fix | Delete
return true;
[53] Fix | Delete
}
[54] Fix | Delete
[55] Fix | Delete
return Jetpack_Memberships::user_can_view_post();
[56] Fix | Delete
}
[57] Fix | Delete
[58] Fix | Delete
/**
[59] Fix | Delete
* Returns post end placement hooked block attributes.
[60] Fix | Delete
*
[61] Fix | Delete
* @param array $default_attrs Deafult attributes.
[62] Fix | Delete
* @param array $anchor_block The anchor block, in parsed block array format.
[63] Fix | Delete
*
[64] Fix | Delete
* @return array
[65] Fix | Delete
*/
[66] Fix | Delete
protected function get_post_end_placement_block_attributes( $default_attrs, $anchor_block ) {
[67] Fix | Delete
if ( ! empty( $anchor_block['attrs']['layout']['type'] ) ) {
[68] Fix | Delete
return array_merge(
[69] Fix | Delete
$default_attrs,
[70] Fix | Delete
array(
[71] Fix | Delete
'layout' => array(
[72] Fix | Delete
'type' => $anchor_block['attrs']['layout']['type'],
[73] Fix | Delete
),
[74] Fix | Delete
)
[75] Fix | Delete
);
[76] Fix | Delete
}
[77] Fix | Delete
[78] Fix | Delete
if ( ! empty( $anchor_block['attrs']['layout']['inherit'] ) ) {
[79] Fix | Delete
return array_merge(
[80] Fix | Delete
$default_attrs,
[81] Fix | Delete
array(
[82] Fix | Delete
'layout' => array(
[83] Fix | Delete
'inherit' => $anchor_block['attrs']['layout']['inherit'],
[84] Fix | Delete
),
[85] Fix | Delete
)
[86] Fix | Delete
);
[87] Fix | Delete
}
[88] Fix | Delete
[89] Fix | Delete
return $default_attrs;
[90] Fix | Delete
}
[91] Fix | Delete
[92] Fix | Delete
/**
[93] Fix | Delete
* Returns true if context is recognized as a header element.
[94] Fix | Delete
*
[95] Fix | Delete
* @param WP_Block_Template|WP_Post|array $context The block template, template part, or pattern the anchor block belongs to.
[96] Fix | Delete
*
[97] Fix | Delete
* @return bool
[98] Fix | Delete
*/
[99] Fix | Delete
protected function is_header_context( $context ) {
[100] Fix | Delete
if ( $context instanceof WP_Post && $context->post_type === 'wp_navigation' ) {
[101] Fix | Delete
return true;
[102] Fix | Delete
}
[103] Fix | Delete
[104] Fix | Delete
if ( $context instanceof WP_Block_Template && $context->area === 'header' ) {
[105] Fix | Delete
return true;
[106] Fix | Delete
}
[107] Fix | Delete
[108] Fix | Delete
return false;
[109] Fix | Delete
}
[110] Fix | Delete
[111] Fix | Delete
/**
[112] Fix | Delete
* Returns true if context is recognized as a single post element.
[113] Fix | Delete
*
[114] Fix | Delete
* @param WP_Block_Template|WP_Post|array $context The block template, template part, or pattern the anchor block belongs to.
[115] Fix | Delete
*
[116] Fix | Delete
* @return bool
[117] Fix | Delete
*/
[118] Fix | Delete
protected function is_single_post_context( $context ) {
[119] Fix | Delete
/**
[120] Fix | Delete
* Some themes are not returning a WP_Block_Template. In that case, we need to check with is_singular function.
[121] Fix | Delete
*/
[122] Fix | Delete
if ( is_array( $context ) ) {
[123] Fix | Delete
return is_singular( 'post' );
[124] Fix | Delete
}
[125] Fix | Delete
[126] Fix | Delete
return $context instanceof WP_Block_Template && $context->slug === 'single';
[127] Fix | Delete
}
[128] Fix | Delete
[129] Fix | Delete
/**
[130] Fix | Delete
* Handles Subscription block navigation placement.
[131] Fix | Delete
*
[132] Fix | Delete
* @return void
[133] Fix | Delete
*/
[134] Fix | Delete
protected function handle_subscribe_block_navigation_placement() {
[135] Fix | Delete
$is_enabled = get_option( 'jetpack_subscriptions_subscribe_navigation_enabled', false );
[136] Fix | Delete
if ( ! $is_enabled ) {
[137] Fix | Delete
return;
[138] Fix | Delete
}
[139] Fix | Delete
[140] Fix | Delete
if ( ! wp_is_block_theme() ) { // TODO Fallback for classic themes.
[141] Fix | Delete
return;
[142] Fix | Delete
}
[143] Fix | Delete
[144] Fix | Delete
add_filter(
[145] Fix | Delete
'hooked_block_types',
[146] Fix | Delete
function ( $hooked_blocks, $relative_position, $anchor_block, $context ) {
[147] Fix | Delete
if (
[148] Fix | Delete
$anchor_block === 'core/navigation' &&
[149] Fix | Delete
$relative_position === 'last_child' &&
[150] Fix | Delete
self::is_header_context( $context )
[151] Fix | Delete
) {
[152] Fix | Delete
$hooked_blocks[] = 'jetpack/subscriptions';
[153] Fix | Delete
}
[154] Fix | Delete
[155] Fix | Delete
return $hooked_blocks;
[156] Fix | Delete
},
[157] Fix | Delete
10,
[158] Fix | Delete
4
[159] Fix | Delete
);
[160] Fix | Delete
[161] Fix | Delete
add_filter(
[162] Fix | Delete
'hooked_block_jetpack/subscriptions',
[163] Fix | Delete
function ( $hooked_block, $hooked_block_type, $relative_position, $anchor_block ) {
[164] Fix | Delete
$is_navigation_anchor_block = isset( $anchor_block['blockName'] ) && $anchor_block['blockName'] === 'core/navigation';
[165] Fix | Delete
[166] Fix | Delete
if ( $is_navigation_anchor_block ) {
[167] Fix | Delete
$class_name = ( ! empty( $hooked_block['attrs'] ) && ! empty( $hooked_block['attrs']['className'] ) )
[168] Fix | Delete
? $hooked_block['attrs']['className'] . ' is-style-button'
[169] Fix | Delete
: 'is-style-button';
[170] Fix | Delete
[171] Fix | Delete
$hooked_block['attrs']['className'] = $class_name;
[172] Fix | Delete
$hooked_block['attrs']['appSource'] = 'subscribe-block-navigation';
[173] Fix | Delete
}
[174] Fix | Delete
[175] Fix | Delete
return $hooked_block;
[176] Fix | Delete
},
[177] Fix | Delete
10,
[178] Fix | Delete
4
[179] Fix | Delete
);
[180] Fix | Delete
}
[181] Fix | Delete
[182] Fix | Delete
/**
[183] Fix | Delete
* Handles Subscribe block placement at the end of each post.
[184] Fix | Delete
*
[185] Fix | Delete
* @return void
[186] Fix | Delete
*/
[187] Fix | Delete
protected function handle_subscribe_block_post_end_placement() {
[188] Fix | Delete
$subscribe_post_end_enabled = get_option( 'jetpack_subscriptions_subscribe_post_end_enabled', false );
[189] Fix | Delete
if ( ! $subscribe_post_end_enabled ) {
[190] Fix | Delete
return;
[191] Fix | Delete
}
[192] Fix | Delete
[193] Fix | Delete
if ( ! wp_is_block_theme() ) { // Fallback for classic themes.
[194] Fix | Delete
add_filter(
[195] Fix | Delete
'the_content',
[196] Fix | Delete
function ( $content ) {
[197] Fix | Delete
// Check if we're inside the main loop in a single Post.
[198] Fix | Delete
if (
[199] Fix | Delete
is_single() &&
[200] Fix | Delete
in_the_loop() &&
[201] Fix | Delete
is_main_query() &&
[202] Fix | Delete
$this->user_can_view_post()
[203] Fix | Delete
) {
[204] Fix | Delete
// translators: %s is the name of the site.
[205] Fix | Delete
$discover_more_from_text = sprintf( __( 'Discover more from %s', 'jetpack' ), get_bloginfo( 'name' ) );
[206] Fix | Delete
$subscribe_text = __( 'Subscribe to get the latest posts sent to your email.', 'jetpack' );
[207] Fix | Delete
[208] Fix | Delete
return $content . do_blocks(
[209] Fix | Delete
<<<HTML
[210] Fix | Delete
<!-- wp:group {"style":{"spacing":{"padding":{"top":"0px","bottom":"0px","left":"0px","right":"0px"},"margin":{"top":"32px","bottom":"32px"}},"border":{"width":"0px","style":"none"}},"className":"has-border-color","layout":{"type":"default"}} -->
[211] Fix | Delete
<div class="wp-block-group has-border-color" style="border-style:none;border-width:0px;margin-top:32px;margin-bottom:32px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">
[212] Fix | Delete
<!-- wp:separator {"style":{"spacing":{"margin":{"bottom":"24px"}}},"className":"is-style-wide"} -->
[213] Fix | Delete
<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-bottom:24px"/>
[214] Fix | Delete
<!-- /wp:separator -->
[215] Fix | Delete
[216] Fix | Delete
<!-- wp:heading {"textAlign":"center","level":3,"style":{"layout":{"selfStretch":"fit","flexSize":null},"spacing":{"margin":{"top":"4px","bottom":"10px"}}}} -->
[217] Fix | Delete
<h3 class="wp-block-heading has-text-align-center" style="margin-top:4px;margin-bottom:10px">$discover_more_from_text</h3>
[218] Fix | Delete
<!-- /wp:heading -->
[219] Fix | Delete
[220] Fix | Delete
<!-- wp:paragraph {"align":"center","style":{"typography":{"fontSize":"15px"},"spacing":{"margin":{"top":"10px","bottom":"10px"}}}} -->
[221] Fix | Delete
<p class="has-text-align-center" style="margin-top:10px;margin-bottom:10px;font-size:15px">$subscribe_text</p>
[222] Fix | Delete
<!-- /wp:paragraph -->
[223] Fix | Delete
[224] Fix | Delete
<!-- wp:group {"layout":{"type":"constrained","contentSize":"480px"}} -->
[225] Fix | Delete
<div class="wp-block-group">
[226] Fix | Delete
<!-- wp:jetpack/subscriptions {"appSource":"subscribe-block-post-end"} /-->
[227] Fix | Delete
</div>
[228] Fix | Delete
<!-- /wp:group -->
[229] Fix | Delete
</div>
[230] Fix | Delete
<!-- /wp:group -->
[231] Fix | Delete
HTML
[232] Fix | Delete
);
[233] Fix | Delete
}
[234] Fix | Delete
[235] Fix | Delete
return $content;
[236] Fix | Delete
},
[237] Fix | Delete
100, // To insert it after the sharing blocks.
[238] Fix | Delete
1
[239] Fix | Delete
);
[240] Fix | Delete
[241] Fix | Delete
return;
[242] Fix | Delete
}
[243] Fix | Delete
[244] Fix | Delete
add_filter(
[245] Fix | Delete
'hooked_block_types',
[246] Fix | Delete
function ( $hooked_blocks, $relative_position, $anchor_block, $context ) {
[247] Fix | Delete
if (
[248] Fix | Delete
$anchor_block === 'core/post-content' &&
[249] Fix | Delete
$relative_position === 'after' &&
[250] Fix | Delete
self::is_single_post_context( $context ) &&
[251] Fix | Delete
$this->user_can_view_post()
[252] Fix | Delete
) {
[253] Fix | Delete
$hooked_blocks[] = 'jetpack/subscriptions';
[254] Fix | Delete
}
[255] Fix | Delete
[256] Fix | Delete
return $hooked_blocks;
[257] Fix | Delete
},
[258] Fix | Delete
10,
[259] Fix | Delete
4
[260] Fix | Delete
);
[261] Fix | Delete
[262] Fix | Delete
add_filter(
[263] Fix | Delete
'hooked_block_jetpack/subscriptions',
[264] Fix | Delete
function ( $hooked_block, $hooked_block_type, $relative_position, $anchor_block ) {
[265] Fix | Delete
$is_post_content_anchor_block = isset( $anchor_block['blockName'] ) && $anchor_block['blockName'] === 'core/post-content';
[266] Fix | Delete
if ( $is_post_content_anchor_block && ( $relative_position === 'after' || $relative_position === 'before' ) ) {
[267] Fix | Delete
$attrs = $this->get_post_end_placement_block_attributes(
[268] Fix | Delete
array(
[269] Fix | Delete
'style' => array(
[270] Fix | Delete
'spacing' => array(
[271] Fix | Delete
'margin' => array(
[272] Fix | Delete
'top' => '48px',
[273] Fix | Delete
'bottom' => '48px',
[274] Fix | Delete
),
[275] Fix | Delete
'padding' => array(
[276] Fix | Delete
'top' => '5px',
[277] Fix | Delete
'bottom' => '5px',
[278] Fix | Delete
),
[279] Fix | Delete
),
[280] Fix | Delete
),
[281] Fix | Delete
),
[282] Fix | Delete
$anchor_block
[283] Fix | Delete
);
[284] Fix | Delete
[285] Fix | Delete
// translators: %s is the name of the site.
[286] Fix | Delete
$discover_more_from_text = sprintf( __( 'Discover more from %s', 'jetpack' ), get_bloginfo( 'name' ) );
[287] Fix | Delete
$subscribe_text = __( 'Subscribe to get the latest posts sent to your email.', 'jetpack' );
[288] Fix | Delete
$inner_content_begin = <<<HTML
[289] Fix | Delete
<div class="wp-block-group" style="margin-top:48px;margin-bottom:48px;padding-top:5px;padding-bottom:5px">
[290] Fix | Delete
<!-- wp:separator {"style":{"spacing":{"margin":{"bottom":"36px"}}},"className":"is-style-wide"} -->
[291] Fix | Delete
<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-bottom:36px"/>
[292] Fix | Delete
<!-- /wp:separator -->
[293] Fix | Delete
[294] Fix | Delete
<!-- wp:heading {"textAlign":"center","level":3,"style":{"layout":{"selfStretch":"fit","flexSize":null},"spacing":{"margin":{"top":"4px","bottom":"10px"}}}} -->
[295] Fix | Delete
<h3 class="wp-block-heading has-text-align-center" style="margin-top:4px;margin-bottom:10px">$discover_more_from_text</h3>
[296] Fix | Delete
<!-- /wp:heading -->
[297] Fix | Delete
[298] Fix | Delete
<!-- wp:paragraph {"align":"center","style":{"typography":{"fontSize":"15px"},"spacing":{"margin":{"top":"4px","bottom":"0px"}}}} -->
[299] Fix | Delete
<p class="has-text-align-center" style="margin-top:4px;margin-bottom:0px;font-size:15px">$subscribe_text</p>
[300] Fix | Delete
<!-- /wp:paragraph -->
[301] Fix | Delete
[302] Fix | Delete
<!-- wp:group {"layout":{"type":"constrained","contentSize":"480px"}} -->
[303] Fix | Delete
<div class="wp-block-group">
[304] Fix | Delete
HTML;
[305] Fix | Delete
$inner_content_end = <<<HTML
[306] Fix | Delete
</div>
[307] Fix | Delete
<!-- /wp:group -->
[308] Fix | Delete
</div>
[309] Fix | Delete
HTML;
[310] Fix | Delete
[311] Fix | Delete
$hooked_block['attrs']['appSource'] = 'subscribe-block-post-end';
[312] Fix | Delete
[313] Fix | Delete
return array(
[314] Fix | Delete
'blockName' => 'core/group',
[315] Fix | Delete
'attrs' => $attrs,
[316] Fix | Delete
'innerBlocks' => array( $hooked_block ),
[317] Fix | Delete
'innerContent' => array(
[318] Fix | Delete
$inner_content_begin,
[319] Fix | Delete
null,
[320] Fix | Delete
$inner_content_end,
[321] Fix | Delete
),
[322] Fix | Delete
);
[323] Fix | Delete
}
[324] Fix | Delete
[325] Fix | Delete
return $hooked_block;
[326] Fix | Delete
},
[327] Fix | Delete
10,
[328] Fix | Delete
4
[329] Fix | Delete
);
[330] Fix | Delete
}
[331] Fix | Delete
}
[332] Fix | Delete
[333] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function