Skip to content

Commit

Permalink
Merge pull request #323 from Automattic/master
Browse files Browse the repository at this point in the history
Release Sep 01
  • Loading branch information
adekbadek authored Sep 1, 2020
2 parents 0dce7a7 + 01a72fb commit e2abf00
Show file tree
Hide file tree
Showing 16 changed files with 218 additions and 454 deletions.
73 changes: 27 additions & 46 deletions includes/class-newspack-newsletters-ads.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ final class Newspack_Newsletters_Ads {
*/
const NEWSPACK_NEWSLETTERS_ADS_CPT = 'newspack_nl_ads_cpt';

/**
* Ads admin page handler.
*/
const NEWSPACK_NEWSLETTERS_ADS_PAGE = 'newspack-newsletters-ads-admin';

/**
* The single instance of the class.
*
Expand Down Expand Up @@ -49,7 +44,6 @@ public function __construct() {
add_action( 'init', [ __CLASS__, 'register_meta' ] );
add_action( 'save_post_' . self::NEWSPACK_NEWSLETTERS_ADS_CPT, [ __CLASS__, 'ad_default_fields' ], 10, 3 );
add_action( 'admin_menu', [ __CLASS__, 'add_ads_page' ] );
add_action( 'admin_enqueue_scripts', [ __CLASS__, 'admin_enqueue_scripts' ] );
}

/**
Expand Down Expand Up @@ -89,47 +83,10 @@ public static function add_ads_page() {
__( 'Newsletters Ads', 'newspack-newsletters' ),
__( 'Ads', 'newspack-newsletters' ),
'manage_options',
self::NEWSPACK_NEWSLETTERS_ADS_PAGE,
[ __CLASS__, 'create_admin_page' ]
);
}

/**
* Ads page callback.
*/
public static function create_admin_page() {
?>
<div class="wrap">
<div id="newspack-newsletters-ads-admin"></div>
</div>
<?php
}

/**
* Enqueue ads admin scripts.
*
* @param string $handler Page handler.
*/
public static function admin_enqueue_scripts( $handler ) {
if ( Newspack_Newsletters::NEWSPACK_NEWSLETTERS_CPT . '_page_' . self::NEWSPACK_NEWSLETTERS_ADS_PAGE !== $handler ) {
return;
};

\wp_enqueue_script(
self::NEWSPACK_NEWSLETTERS_ADS_PAGE,
plugins_url( '../dist/adsAdmin.js', __FILE__ ),
[ 'wp-components', 'wp-api-fetch', 'wp-date' ],
filemtime( NEWSPACK_NEWSLETTERS_PLUGIN_FILE . 'dist/adsAdmin.js' ),
true
);
wp_register_style(
self::NEWSPACK_NEWSLETTERS_ADS_PAGE,
plugins_url( '../dist/adsAdmin.css', __FILE__ ),
[ 'wp-components' ],
filemtime( NEWSPACK_NEWSLETTERS_PLUGIN_FILE . 'dist/adsAdmin.css' )
'/edit.php?post_type=' . self::NEWSPACK_NEWSLETTERS_ADS_CPT,
null,
2
);
wp_style_add_data( self::NEWSPACK_NEWSLETTERS_ADS_PAGE, 'rtl', 'replace' );
wp_enqueue_style( self::NEWSPACK_NEWSLETTERS_ADS_PAGE );
}

/**
Expand All @@ -140,8 +97,32 @@ public static function register_ads_cpt() {
return;
}

$labels = [
'name' => _x( 'Newsletter Ads', 'post type general name', 'newspack-newsletters' ),
'singular_name' => _x( 'Newsletter Ad', 'post type singular name', 'newspack-newsletters' ),
'menu_name' => _x( 'Newsletter Ads', 'admin menu', 'newspack-newsletters' ),
'name_admin_bar' => _x( 'Newsletter Ad', 'add new on admin bar', 'newspack-newsletters' ),
'add_new' => _x( 'Add New', 'popup', 'newspack-newsletters' ),
'add_new_item' => __( 'Add New Newsletter Ad', 'newspack-newsletters' ),
'new_item' => __( 'New Newsletter Ad', 'newspack-newsletters' ),
'edit_item' => __( 'Edit Newsletter Ad', 'newspack-newsletters' ),
'view_item' => __( 'View Newsletter Ad', 'newspack-newsletters' ),
'all_items' => __( 'All Newsletter Ads', 'newspack-newsletters' ),
'search_items' => __( 'Search Newsletter Ads', 'newspack-newsletters' ),
'parent_item_colon' => __( 'Parent Newsletter Ads:', 'newspack-newsletters' ),
'not_found' => __( 'No Newsletter Ads found.', 'newspack-newsletters' ),
'not_found_in_trash' => __( 'No Newsletter Ads found in Trash.', 'newspack-newsletters' ),
'items_list' => __( 'Newsletter Ads list', 'newspack-newsletters' ),
'item_published' => __( 'Newsletter Ad published', 'newspack-newsletters' ),
'item_published_privately' => __( 'Newsletter Ad published privately', 'newspack-newsletters' ),
'item_reverted_to_draft' => __( 'Newsletter Ad reverted to draft', 'newspack-newsletters' ),
'item_scheduled' => __( 'Newsletter Ad scheduled', 'newspack-newsletters' ),
'item_updated' => __( 'Newsletter Ad updated', 'newspack-newsletters' ),
];

$cpt_args = [
'public' => false,
'labels' => $labels,
'show_ui' => true,
'show_in_menu' => false,
'show_in_rest' => true,
Expand Down
8 changes: 4 additions & 4 deletions includes/class-newspack-newsletters-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,20 @@ public static function enqueue_block_editor_assets() {

if ( self::is_editing_newsletter_ad() ) {
\wp_enqueue_script(
Newspack_Newsletters_Ads::NEWSPACK_NEWSLETTERS_ADS_PAGE,
'newspack-newsletters-ads-page',
plugins_url( '../dist/adsEditor.js', __FILE__ ),
[ 'wp-components', 'wp-api-fetch' ],
filemtime( NEWSPACK_NEWSLETTERS_PLUGIN_FILE . 'dist/adsEditor.js' ),
true
);
wp_register_style(
Newspack_Newsletters_Ads::NEWSPACK_NEWSLETTERS_ADS_PAGE,
'newspack-newsletters-ads-page',
plugins_url( '../dist/adsEditor.css', __FILE__ ),
[],
filemtime( NEWSPACK_NEWSLETTERS_PLUGIN_FILE . 'dist/adsEditor.css' )
);
wp_style_add_data( Newspack_Newsletters_Ads::NEWSPACK_NEWSLETTERS_ADS_PAGE, 'rtl', 'replace' );
wp_enqueue_style( Newspack_Newsletters_Ads::NEWSPACK_NEWSLETTERS_ADS_PAGE );
wp_style_add_data( 'newspack-newsletters-ads-page', 'rtl', 'replace' );
wp_enqueue_style( 'newspack-newsletters-ads-page' );
}

if ( ! self::is_editing_newsletter() ) {
Expand Down
20 changes: 10 additions & 10 deletions includes/class-newspack-newsletters-renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -627,16 +627,6 @@ private static function insert_ads( $markup, $current_position ) {
* @return string MJML markup to be injected into the template.
*/
private static function post_to_mjml_components( $post, $include_ads ) {
self::$color_palette = json_decode( get_option( 'newspack_newsletters_color_palette', false ), true );
self::$font_header = get_post_meta( $post->ID, 'font_header', true );
self::$font_body = get_post_meta( $post->ID, 'font_body', true );
if ( ! in_array( self::$font_header, Newspack_Newsletters::$supported_fonts ) ) {
self::$font_header = 'Arial';
}
if ( ! in_array( self::$font_body, Newspack_Newsletters::$supported_fonts ) ) {
self::$font_body = 'Georgia';
}

$body = '';
$valid_blocks = array_filter(
parse_blocks( $post->post_content ),
Expand Down Expand Up @@ -720,6 +710,16 @@ function ( $block ) {
* @return string MJML markup.
*/
private static function render_mjml( $post ) {
self::$color_palette = json_decode( get_option( 'newspack_newsletters_color_palette', false ), true );
self::$font_header = get_post_meta( $post->ID, 'font_header', true );
self::$font_body = get_post_meta( $post->ID, 'font_body', true );
if ( ! in_array( self::$font_header, Newspack_Newsletters::$supported_fonts ) ) {
self::$font_header = 'Arial';
}
if ( ! in_array( self::$font_body, Newspack_Newsletters::$supported_fonts ) ) {
self::$font_body = 'Georgia';
}

$title = $post->post_title; // phpcs:ignore WordPressVIPMinimum.Variables.VariableAnalysis.UnusedVariable
$body = self::post_to_mjml_components( $post, true ); // phpcs:ignore WordPressVIPMinimum.Variables.VariableAnalysis.UnusedVariable
$background_color = get_post_meta( $post->ID, 'background_color', true );
Expand Down
28 changes: 26 additions & 2 deletions includes/class-newspack-newsletters-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Newspack_Newsletters_Settings {
public static function init() {
add_action( 'admin_menu', [ __CLASS__, 'add_plugin_page' ] );
add_action( 'admin_init', [ __CLASS__, 'page_init' ] );
add_action( 'update_option_newspack_newsletters_public_posts_slug', [ __CLASS__, 'update_option_newspack_newsletters_public_posts_slug' ], 10, 2 );
}

/**
Expand Down Expand Up @@ -70,6 +71,13 @@ public static function get_settings_list() {
'key' => 'newspack_newsletters_mjml_api_secret',
'type' => 'text',
),
array(
'default' => 'newsletter',
'description' => __( 'Public Newsletter Posts Slug', 'newspack-newsletters' ),
'key' => 'newspack_newsletters_public_posts_slug',
'sanitize_callback' => 'sanitize_title',
'type' => 'text',
),
);

if ( class_exists( 'Jetpack' ) && \Jetpack::is_module_active( 'related-posts' ) ) {
Expand Down Expand Up @@ -126,9 +134,13 @@ public static function page_init() {
'newspack-newsletters-settings-admin'
);
foreach ( self::get_settings_list() as $setting ) {
$args = [
'sanitize_callback' => ! empty( $setting['sanitize_callback'] ) ? $setting['sanitize_callback'] : 'sanitize_text_field',
];
register_setting(
'newspack_newsletters_options_group',
$setting['key']
$setting['key'],
$args
);
add_settings_field(
$setting['key'],
Expand All @@ -150,7 +162,8 @@ public static function newspack_newsletters_settings_callback( $setting ) {
$key = $setting['key'];
$type = $setting['type'];
$description = $setting['description'];
$value = get_option( $key, false );
$default = ! empty( $setting['default'] ) ? $setting['default'] : false;
$value = get_option( $key, $default );

if ( 'select' === $type ) {
$options = $setting['options'];
Expand Down Expand Up @@ -194,6 +207,17 @@ public static function newspack_newsletters_settings_callback( $setting ) {
);
}
}

/**
* Hook into public posts slug option after save, to flush permalinks.
*
* @param string $old_value The old value.
* @param string $new_value The new value.
*/
public static function update_option_newspack_newsletters_public_posts_slug( $old_value, $new_value ) {
Newspack_Newsletters::register_cpt();
flush_rewrite_rules(); // phpcs:ignore
}
}

if ( is_admin() ) {
Expand Down
81 changes: 75 additions & 6 deletions includes/class-newspack-newsletters.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,15 @@ public function __construct() {
add_action( 'init', [ __CLASS__, 'register_blocks' ] );
add_action( 'rest_api_init', [ __CLASS__, 'rest_api_init' ] );
add_action( 'default_title', [ __CLASS__, 'default_title' ], 10, 2 );
add_action( 'wp_head', [ __CLASS__, 'public_newsletter_custom_style' ], 10, 2 );
add_filter( 'display_post_states', [ __CLASS__, 'display_post_states' ], 10, 2 );
add_action( 'pre_get_posts', [ __CLASS__, 'maybe_display_public_archive_posts' ] );
add_action( 'template_redirect', [ __CLASS__, 'maybe_display_public_post' ] );
add_filter( 'post_row_actions', [ __CLASS__, 'display_view_or_preview_link_in_admin' ] );
add_filter( 'newspack_newsletters_assess_has_disabled_popups', [ __CLASS__, 'disable_campaigns_for_newsletters' ], 11 );
add_filter( 'jetpack_relatedposts_filter_options', [ __CLASS__, 'disable_jetpack_related_posts' ] );
add_action( 'save_post_' . self::NEWSPACK_NEWSLETTERS_CPT, [ $this, 'save' ], 10, 3 );
add_action( 'save_post_' . self::NEWSPACK_NEWSLETTERS_CPT, [ __CLASS__, 'save' ], 10, 3 );
add_action( 'admin_enqueue_scripts', [ __CLASS__, 'branding_scripts' ] );

self::set_service_provider( self::service_provider() );

Expand Down Expand Up @@ -228,7 +230,7 @@ public static function register_meta() {
* @param WP_Post $post The complete post object.
* @param boolean $update Whether this is an existing post being updated or not.
*/
public function save( $post_id, $post, $update ) {
public static function save( $post_id, $post, $update ) {
if ( ! $update ) {
update_post_meta( $post_id, 'template_id', -1 );
}
Expand All @@ -238,6 +240,8 @@ public function save( $post_id, $post, $update ) {
* Register the custom post type.
*/
public static function register_cpt() {
$public_slug = get_option( 'newspack_newsletters_public_posts_slug', 'newsletter' );

$labels = [
'name' => _x( 'Newsletters', 'post type general name', 'newspack-newsletters' ),
'singular_name' => _x( 'Newsletter', 'post type singular name', 'newspack-newsletters' ),
Expand All @@ -256,12 +260,12 @@ public static function register_cpt() {
];

$cpt_args = [
'has_archive' => true,
'has_archive' => $public_slug,
'labels' => $labels,
'public' => true,
'public_queryable' => true,
'query_var' => true,
'rewrite' => [ 'slug' => 'newsletter' ],
'rewrite' => [ 'slug' => $public_slug ],
'show_ui' => true,
'show_in_rest' => true,
'supports' => [ 'editor', 'title', 'custom-fields' ],
Expand Down Expand Up @@ -738,7 +742,7 @@ public static function api_administration_permissions_check( $request ) {
if ( ! current_user_can( 'manage_options' ) ) {
return new \WP_Error(
'newspack_rest_forbidden',
esc_html__( 'You cannot use this resource.', 'newspack' ),
esc_html__( 'You cannot use this resource.', 'newspack-newsletters' ),
[
'status' => 403,
]
Expand All @@ -757,7 +761,7 @@ public static function api_authoring_permissions_check( $request ) {
if ( ! current_user_can( 'edit_others_posts' ) ) {
return new \WP_Error(
'newspack_rest_forbidden',
esc_html__( 'You cannot use this resource.', 'newspack' ),
esc_html__( 'You cannot use this resource.', 'newspack-newsletters' ),
[
'status' => 403,
]
Expand All @@ -780,6 +784,42 @@ public static function default_title( $post_title, $post ) {
return $post_title;
}

/**
* Handle custom Newsletter styling when viewing the newsletter as a public post.
*/
public static function public_newsletter_custom_style() {
if ( ! is_single() ) {
return;
}
$post = get_post();
if ( $post && self::NEWSPACK_NEWSLETTERS_CPT === $post->post_type ) {
$font_header = get_post_meta( $post->ID, 'font_header', true );
$font_body = get_post_meta( $post->ID, 'font_body', true );
$background_color = get_post_meta( $post->ID, 'background_color', true );
?>
<style>
.main-content {
background-color: <?php echo esc_attr( $background_color ); ?>;
font-family: <?php echo esc_attr( $font_body ); ?>;
}
.main-content h1,
.main-content h2,
.main-content h3,
.main-content h4,
.main-content h5,
.main-content h6 {
font-family: <?php echo esc_attr( $font_header ); ?>;
}
<?php if ( $background_color ) : ?>
.entry-content {
padding: 0 32px;;
}
<?php endif; ?>
</style>
<?php
}
}

/**
* Activation Nag
*/
Expand Down Expand Up @@ -810,6 +850,35 @@ public static function activation_nag() {
<?php
}

/**
* Enqueue style to handle Newspack branding.
*/
public static function branding_scripts() {
$screen = get_current_screen();
if (
self::NEWSPACK_NEWSLETTERS_CPT !== $screen->post_type &&
Newspack_Newsletters_Ads::NEWSPACK_NEWSLETTERS_ADS_CPT !== $screen->post_type
) {
return;
}

$script = 'newspack-newsletters-branding_scripts';
wp_enqueue_script(
$script,
plugins_url( '../dist/branding.js', __FILE__ ),
[ 'jquery' ],
'1.0',
false
);
wp_enqueue_style(
$script,
plugins_url( '../dist/branding.css', __FILE__ ),
[],
'1.0',
'screen'
);
}

/**
* Enqueue script to handle activation nag dismissal.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function api_authoring_permissions_check( $request ) {
if ( ! current_user_can( 'edit_others_posts' ) ) {
return new \WP_Error(
'newspack_rest_forbidden',
esc_html__( 'You cannot use this resource.', 'newspack' ),
esc_html__( 'You cannot use this resource.', 'newspack-newsletters' ),
[
'status' => 403,
]
Expand Down
Loading

0 comments on commit e2abf00

Please sign in to comment.