Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Portfolio / Testimonials: on block themes remove settings and menu items when not in use #41714

Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Custom post types: do not display testimonials and portfolios on block themes where they are not in use.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* @package automattic/jetpack-classic-theme-helper
*/

use Automattic\Jetpack\Classic_Theme_Helper\Jetpack_Portfolio;
use Automattic\Jetpack\Classic_Theme_Helper\Jetpack_Testimonial;
use Automattic\Jetpack\Redirect;
use Automattic\Jetpack\Status\Host;

Expand Down Expand Up @@ -47,8 +49,10 @@ function jetpack_custom_post_types_loaded() {
$initial_state = 'var CUSTOM_CONTENT_TYPE__INITIAL_STATE; typeof CUSTOM_CONTENT_TYPE__INITIAL_STATE === "object" || (CUSTOM_CONTENT_TYPE__INITIAL_STATE = JSON.parse(decodeURIComponent("' . rawurlencode(
wp_json_encode(
array(
'active' => true,
'over_ride' => false,
'active' => classic_theme_helper_cpt_should_be_active(),
'over_ride' => false,
'should_show_testimonials' => Jetpack_Testimonial::site_should_display_testimonials() ? true : false,
'should_show_portfolios' => Jetpack_Portfolio::site_should_display_portfolios() ? true : false,
)
)
) . '")));';
Expand Down Expand Up @@ -90,7 +94,7 @@ function register_rest_route_custom_content_types() {
*/
function get_custom_content_type_details() {

$active = true;
$active = classic_theme_helper_cpt_should_be_active();
$over_ride = false;
$name = 'Custom Content Types';
$description = 'Display different types of content on your site with custom content types.';
Expand Down Expand Up @@ -126,11 +130,27 @@ function custom_content_require_admin_privilege_callback() {
);
}

/**
* Check if the custom content types should be active.
*
* @return bool
*/
function classic_theme_helper_cpt_should_be_active() {
if ( ! Jetpack_Testimonial::site_should_display_testimonials() && ! Jetpack_Portfolio::site_should_display_portfolios() ) {
return false;
}
return true;
}

if ( ! function_exists( 'jetpack_cpt_settings_api_init' ) ) {
/**
* Add Settings Section for CPT
*/
function jetpack_cpt_settings_api_init() {
if ( ! classic_theme_helper_cpt_should_be_active() ) {
return;
}

add_settings_section(
'jetpack_cpt_section',
'<span id="cpt-options">' . __( 'Your Custom Content Types', 'jetpack-classic-theme-helper' ) . '</span>',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Automattic\Jetpack\Classic_Theme_Helper;

use Automattic\Jetpack\Blocks;
use Automattic\Jetpack\Status\Host;
use Jetpack_Options;
use WP_Customize_Image_Control;
Expand Down Expand Up @@ -148,13 +149,40 @@ public function maybe_register_cpt() {
}
}

/**
* Check if a site should display portfolios - it should not if:
* - the theme is a block theme without portfolios enabled.
*
* @return bool
*/
public static function site_should_display_portfolios() {
$should_display = true;
if ( Blocks::is_fse_theme() ) {
if ( ! get_option( self::OPTION_NAME, '0' ) ) {
$should_display = false;
}
}

/**
* Filter whether the site should display portfolios.
*
* @since $$next-version$$
*
* @param bool $should_display Whether portfolios should be displayed.
*/
return apply_filters( 'classic_theme_helper_should_display_portfolios', $should_display );
}

/**
* Add a checkbox field in 'Settings' > 'Writing'
* for enabling CPT functionality.
*
* @return void
*/
public function settings_api_init() {
if ( ! self::site_should_display_portfolios() ) {
return;
}
add_settings_field(
self::OPTION_NAME,
'<span class="cpt-options">' . __( 'Portfolio Projects', 'jetpack-classic-theme-helper' ) . '</span>',
Expand Down Expand Up @@ -187,42 +215,42 @@ public function settings_api_init() {
public function setting_html() {
if ( current_theme_supports( self::CUSTOM_POST_TYPE ) ) : ?>
<p>
<?php
echo wp_kses(
sprintf(
<?php
echo wp_kses(
sprintf(
/* translators: %s is the name of a custom post type such as "jetpack-portfolio" */
__( 'Your theme supports <strong>%s</strong>', 'jetpack-classic-theme-helper' ),
esc_attr( self::CUSTOM_POST_TYPE )
),
array(
'strong' => array(),
)
);
?>
__( 'Your theme supports <strong>%s</strong>', 'jetpack-classic-theme-helper' ),
esc_attr( self::CUSTOM_POST_TYPE )
),
array(
'strong' => array(),
)
);
?>
</p>
<?php else : ?>
<?php else : ?>
<label for="<?php echo esc_attr( self::OPTION_NAME ); ?>">
<input name="<?php echo esc_attr( self::OPTION_NAME ); ?>" id="<?php echo esc_attr( self::OPTION_NAME ); ?>" <?php echo checked( get_option( self::OPTION_NAME, '0' ), true, false ); ?> type="checkbox" value="1" />
<?php esc_html_e( 'Enable Portfolio Projects for this site.', 'jetpack-classic-theme-helper' ); ?>
<a target="_blank" href="https://en.support.wordpress.com/portfolios/"><?php esc_html_e( 'Learn More', 'jetpack-classic-theme-helper' ); ?></a>
</label>
<?php
endif;
if ( get_option( self::OPTION_NAME, '0' ) || current_theme_supports( self::CUSTOM_POST_TYPE ) ) :
printf(
'<p><label for="%1$s">%2$s</label></p>',
esc_attr( self::OPTION_READING_SETTING ),
sprintf(
/* translators: %1$s is replaced with an input field for numbers */
__( 'Portfolio pages display at most %1$s projects', 'jetpack-classic-theme-helper' ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- the placeholder contains HTML.
<?php
endif;
if ( get_option( self::OPTION_NAME, '0' ) || current_theme_supports( self::CUSTOM_POST_TYPE ) ) :
printf(
'<p><label for="%1$s">%2$s</label></p>',
esc_attr( self::OPTION_READING_SETTING ),
sprintf(
'<input name="%1$s" id="%1$s" type="number" step="1" min="1" value="%2$s" class="small-text" />',
esc_attr( self::OPTION_READING_SETTING ),
esc_attr( get_option( self::OPTION_READING_SETTING, '10' ) )
/* translators: %1$s is replaced with an input field for numbers */
__( 'Portfolio pages display at most %1$s projects', 'jetpack-classic-theme-helper' ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- the placeholder contains HTML.
sprintf(
'<input name="%1$s" id="%1$s" type="number" step="1" min="1" value="%2$s" class="small-text" />',
esc_attr( self::OPTION_READING_SETTING ),
esc_attr( get_option( self::OPTION_READING_SETTING, '10' ) )
)
)
)
);
endif;
);
endif;
}

/**
Expand Down Expand Up @@ -347,6 +375,10 @@ public function register_post_types() {
return;
}

if ( ! self::site_should_display_portfolios() ) {
return;
}

register_post_type(
self::CUSTOM_POST_TYPE,
array(
Expand Down Expand Up @@ -561,9 +593,9 @@ public function enqueue_admin_styles( $hook ) {
$screen = get_current_screen();

if (
'edit.php' === $hook
&& self::CUSTOM_POST_TYPE === $screen->post_type
&& current_theme_supports( 'post-thumbnails' )
'edit.php' === $hook
&& self::CUSTOM_POST_TYPE === $screen->post_type
&& current_theme_supports( 'post-thumbnails' )
) {
wp_add_inline_style( 'wp-admin', '.manage-column.column-thumbnail { width: 50px; } @media screen and (max-width: 360px) { .column-thumbnail{ display:none; } }' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Automattic\Jetpack\Classic_Theme_Helper;

use Automattic\Jetpack\Blocks;
use Automattic\Jetpack\Status\Host;
use Jetpack_Options;
use WP_Customize_Image_Control;
Expand Down Expand Up @@ -146,13 +147,42 @@ public function maybe_register_cpt() {
}
}

/**
* Check if a site should display testimonials - it should not if:
* - the theme is a block theme without testimonials enabled.
*
* @return bool
*/
public static function site_should_display_testimonials() {
$should_display = true;
if ( Blocks::is_fse_theme() ) {
if ( ! get_option( self::OPTION_NAME, '0' ) ) {
$should_display = false;
}
}

/**
* Filter whether the site should display testimonials.
*
* @since $$next-version$$
*
* @param bool $should_display Whether testimonials should be displayed.
*/
return apply_filters( 'classic_theme_helper_should_display_testimonials', $should_display );
}

/**
* Add a checkbox field in 'Settings' > 'Writing'
* for enabling CPT functionality.
*
* @return void
*/
public function settings_api_init() {

if ( ! self::site_should_display_testimonials() ) {
return;
}

add_settings_field(
self::OPTION_NAME,
'<span class="cpt-options">' . __( 'Testimonials', 'jetpack-classic-theme-helper' ) . '</span>',
Expand Down Expand Up @@ -338,6 +368,9 @@ public function register_post_types() {
if ( post_type_exists( self::CUSTOM_POST_TYPE ) ) {
return;
}
if ( ! self::site_should_display_testimonials() ) {
return;
}

register_post_type(
self::CUSTOM_POST_TYPE,
Expand Down
Loading
Loading