Skip to content

Commit

Permalink
PLANET-7648: Added opt-in archive notice on old posts (#2445)
Browse files Browse the repository at this point in the history
- Add new feature flag
- Add new default content
- Pass content to the twig template
- Add styles and clean code
- Show or hide notice based on the date
- Improve code logic
- Add migration script
- Fix lint issues
- Fix php test
  • Loading branch information
mardelnet authored Nov 27, 2024
1 parent b0b91b3 commit ae64ea1
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 0 deletions.
27 changes: 27 additions & 0 deletions assets/src/scss/pages/post/_post.scss
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,30 @@
.post-tags {
margin-bottom: $sp-2;
}

.single-post-old-posts-archive-notice {
background-color: var(--beige-100);
padding: $sp-3;
margin-top: $sp-3;

@include medium-and-up {
padding: $sp-3 $sp-8;
}

a {
margin: 0 auto;
display: block;
width: fit-content;
}

h4 {
text-align: center;
margin: 0;
}

p {
font-size: var(--font-size-m--font-family-secondary);
line-height: var(--line-height-m--font-family-secondary);
margin: $sp-3 0;
}
}
1 change: 1 addition & 0 deletions single.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
$context['post_categories'] = implode(', ', $post->categories());
// We need the explode because we want to remove "+00:00" at the end of the string.
$context['page_date'] = explode('+', get_the_date('c', $post->ID))[0];
$context['old_posts_archive_notice'] = $post->get_old_posts_archive_notice();

Context::set_og_meta_fields($context, $post);
Context::set_campaign_datalayer($context, $page_meta_data);
Expand Down
47 changes: 47 additions & 0 deletions src/Features/OldPostsArchiveNotice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace P4\MasterTheme\Features;

use P4\MasterTheme\Feature;

/**
* @see description().
*/
class OldPostsArchiveNotice extends Feature
{
/**
* @inheritDoc
*/
public static function id(): string
{
return 'old_posts_archive_notice';
}

/**
* @inheritDoc
*/
protected static function name(): string
{
return __('Old Posts Archive notice', 'planet4-master-theme-backend');
}

/**
* @inheritDoc
*/
protected static function description(): string
{
return __(
// phpcs:ignore Generic.Files.LineLength.MaxExceeded
'Displays a notice on top of old Posts.<br>Adjust its behavior and text through <a href="/wp-admin/admin.php?page=planet4_settings_defaults_content" href="_self">Defaults content settings.</a>',
'planet4-master-theme-backend'
);
}

/**
* @inheritDoc
*/
public static function show_toggle_production(): bool
{
return true;
}
}
49 changes: 49 additions & 0 deletions src/Migrations/M034PrePopulateOldPostsArchiveNotice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace P4\MasterTheme\Migrations;

use P4\MasterTheme\MigrationRecord;
use P4\MasterTheme\MigrationScript;

/**
* Remove the WordPress template editor option from Planet 4 Features.
*/
class M034PrePopulateOldPostsArchiveNotice extends MigrationScript
{
/**
* Perform the actual migration.
*
* @param MigrationRecord $record Information on the execution, can be used to add logs.
* phpcs:disable SlevomatCodingStandard.Functions.UnusedParameter -- interface implementation
*/
protected static function execute(MigrationRecord $record): void
{
$cutoff = '10';
$title = 'Oldies But Goodies Alert!';
$description = "Hey there! Just a quick note: the stuff you're browsing through is mostly for nostalgia and archival kicks. So, before you go basing any big decisions on what you find here, maybe double-check with some fresher content."; // phpcs:ignore Generic.Files.LineLength.MaxExceeded
$button = 'Read the latest from ' . get_bloginfo('name');
$prefix = 'old_posts_archive_notice_';

$options = get_option('planet4_options');

if (!$options) {
echo "Error with migration M034PrePopulateOldPostsArchiveNotice. P4 options not found.\n"; // phpcs:ignore
return;
}

$options[ $prefix . 'cutoff' ] = $cutoff;
$options[ $prefix . 'title' ] = $title;
$options[ $prefix . 'description' ] = $description;
$options[ $prefix . 'button' ] = $button;

$result = update_option('planet4_options', $options);

if ($result) {
echo "Migration M034PrePopulateOldPostsArchiveNotice successful\n"; // phpcs:ignore
} else {
echo "Error with migration M034PrePopulateOldPostsArchiveNotice. P4 options could not be updated.\n"; // phpcs:ignore
}
}
}
2 changes: 2 additions & 0 deletions src/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use P4\MasterTheme\Migrations\M031MigrateMediaBlockToAudioVideoBlock;
use P4\MasterTheme\Migrations\M032MigrateSplit2ColumnBlock;
use P4\MasterTheme\Migrations\M033MigrateSocialMediaTwitterBlockToEmbedBlock;
use P4\MasterTheme\Migrations\M034PrePopulateOldPostsArchiveNotice;

/**
* Run any new migration scripts and record results in the log.
Expand Down Expand Up @@ -87,6 +88,7 @@ public static function migrate(): void
M031MigrateMediaBlockToAudioVideoBlock::class,
M032MigrateSplit2ColumnBlock::class,
M033MigrateSocialMediaTwitterBlockToEmbedBlock::class,
M034PrePopulateOldPostsArchiveNotice::class,
];

// Loop migrations and run those that haven't run yet.
Expand Down
33 changes: 33 additions & 0 deletions src/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace P4\MasterTheme;

use Timber\Post as TimberPost;
use P4\MasterTheme\Features\OldPostsArchiveNotice;
use WP_Block;
use WP_Error;

Expand Down Expand Up @@ -518,4 +519,36 @@ public function get_locale(): string

return $post_locale ?? $site_locale;
}

/**
* Get the content for the old posts archive notice.
*
*/
public function get_old_posts_archive_notice(): array
{
$prefix = 'old_posts_archive_notice_';
$post_date = get_post_field('post_date', $this->id);
$options = get_option('planet4_options');
$notice_cutoff = isset($options[$prefix . 'cutoff']) ? $options[$prefix . 'cutoff'] : null;
$activate = OldPostsArchiveNotice::is_active();

if ($options && $post_date && $notice_cutoff && $activate) {
$post_publish_year = (int) date('Y', strtotime($post_date));
$current_year = (int) date('Y');

return array(
"show_notice" => ($current_year - $post_publish_year) >= (int) $notice_cutoff,
"title" => $options[$prefix . 'title'] ?? '',
"description" => $options[$prefix . 'description'] ?? '',
"button" => $options[$prefix . 'button'] ?? '',
);
}

return array(
"show_notice" => false,
"title" => '',
"description" => '',
"button" => '',
);
}
}
25 changes: 25 additions & 0 deletions src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,31 @@ public function __construct()
'media_buttons' => false,
],
],

[
'name' => __('Old Posts Archive notice cutoff years', 'planet4-master-theme-backend'),
'id' => 'old_posts_archive_notice_cutoff',
'type' => 'text',
'desc' => __('Condition to determine when to show the notice, in years.<br>Any Post older than that will have the notice displayed on the top.', 'planet4-master-theme-backend'),
],

[
'name' => __('Old Posts Archive notice title', 'planet4-master-theme-backend'),
'id' => 'old_posts_archive_notice_title',
'type' => 'text',
],

[
'name' => __('Old Posts Archive notice description', 'planet4-master-theme-backend'),
'id' => 'old_posts_archive_notice_description',
'type' => 'textarea',
],

[
'name' => __('Old Posts Archive button text', 'planet4-master-theme-backend'),
'id' => 'old_posts_archive_notice_button',
'type' => 'text',
],
],
],
'planet4_settings_cookies_text' => [
Expand Down
2 changes: 2 additions & 0 deletions src/Settings/Features.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use P4\MasterTheme\Features\LazyYoutubePlayer;
use P4\MasterTheme\Features\RedirectRedirectPages;
use P4\MasterTheme\Features\Planet4Blocks;
use P4\MasterTheme\Features\OldPostsArchiveNotice;
use P4\MasterTheme\Loader;
use P4\MasterTheme\Settings;
use CMB2;
Expand Down Expand Up @@ -99,6 +100,7 @@ public static function all_features(): array
LazyYoutubePlayer::class,
RedirectRedirectPages::class,
Planet4Blocks::class,
OldPostsArchiveNotice::class,

// Dev only.
DisableDataSync::class,
Expand Down
16 changes: 16 additions & 0 deletions templates/single.twig
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,22 @@
</div>
</div>
{% include "blocks/share_buttons.twig" with {social:post.share_meta, utm_medium: 'share'} %}

{% if old_posts_archive_notice.show_notice %}
<div class="single-post-old-posts-archive-notice">
{% if ( old_posts_archive_notice.title ) %}
<h4>{{ old_posts_archive_notice.title|e('wp_kses_post')|raw }}</h4>
{% endif %}
{% if ( old_posts_archive_notice.description ) %}
<p>{{ old_posts_archive_notice.description|e('wp_kses_post')|raw }}</p>
{% endif %}
{% if ( old_posts_archive_notice.button ) %}
<a class="btn btn-secondary" href="{{ fn('get_term_link', page_term_id) }}">
{{ old_posts_archive_notice.button|e('wp_kses_post')|raw }}
</a>
{% endif %}
</div>
{% endif %}
</header>
</div>

Expand Down

0 comments on commit ae64ea1

Please sign in to comment.