Skip to content

Commit

Permalink
PLANET-7681 Make existing social share buttons configurable (#2515)
Browse files Browse the repository at this point in the history
This is the first step to offering more customisation for our share buttons
  • Loading branch information
mleray authored Feb 6, 2025
1 parent e21100f commit c31f666
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 50 deletions.
8 changes: 8 additions & 0 deletions admin/css/options.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
display: none;
}

.social-share-checkbox {
margin-inline-end: 16px;
}

.social-share-checkbox input {
vertical-align: text-bottom;
}

@media (min-width: 768px) {
.planet4_options .cmb-row:not(.hidden) {
display: grid;
Expand Down
48 changes: 8 additions & 40 deletions src/GravityFormsExtensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,34 +54,6 @@ class GravityFormsExtensions
],
];

public const P4_SHARE_BUTTONS = [
[
'label' => 'WhatsApp',
'name' => 'whatsapp',
'default_value' => 1,
],
[
'label' => 'Facebook',
'name' => 'facebook',
'default_value' => 1,
],
[
'label' => 'Twitter',
'name' => 'twitter',
'default_value' => 1,
],
[
'label' => 'Email',
'name' => 'email',
'default_value' => 1,
],
[
'label' => 'Native share (mobile only)',
'name' => 'native',
'default_value' => 1,
],
];

public array $current_entry = [];

public string $is_payment_successful = '';
Expand Down Expand Up @@ -369,13 +341,6 @@ public function p4_gf_confirmation_settings(array $fields): array
$fields = array_merge($fields, $share_buttons);
}

$fields['p4_share_buttons']['fields'][] = [
'type' => 'checkbox',
'name' => 'p4_gf_share_platforms',
'label' => __('Show share buttons below message', 'planet4-master-theme-backend'),
'choices' => self::P4_SHARE_BUTTONS,
];

$fields['p4_share_buttons']['fields'][] = [
'type' => 'text',
'name' => 'p4_gf_share_text_override',
Expand Down Expand Up @@ -499,14 +464,17 @@ public function p4_gf_custom_confirmation($confirmation, $form, $entry)
</script>";
}

$social_share_options = planet4_get_option('social_share_options', []);

$confirmation_fields = [
'confirmation' => $confirmation,
'share_platforms' => [
'facebook' => $current_confirmation['facebook'] ?? true,
'twitter' => $current_confirmation['twitter'] ?? true,
'whatsapp' => $current_confirmation['whatsapp'] ?? true,
'native' => $current_confirmation['native'] ?? true,
'email' => $current_confirmation['email'] ?? true,
'facebook' => in_array('facebook', $social_share_options),
'twitter' => in_array('twitter', $social_share_options),
'whatsapp' => in_array('whatsapp', $social_share_options),
'email' => in_array('email', $social_share_options),
// We might add a setting for this one in the future, but for now we always enable it in GF.
'native' => true,
],
'post' => $post,
'social_accounts' => $post->get_social_accounts($context['footer_social_menu'] ?: []),
Expand Down
27 changes: 27 additions & 0 deletions src/Migrations/M039EnableNewSocialSharePlatforms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace P4\MasterTheme\Migrations;

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

/**
* Enable all social share options via the new P4 setting.
*/
class M039EnableNewSocialSharePlatforms 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
{
$options = get_option('planet4_options');
$options['social_share_options'] = array_keys(Settings::SOCIAL_SHARE_OPTIONS);
update_option('planet4_options', $options);
}
// phpcs:enable SlevomatCodingStandard.Functions.UnusedParameter
}
2 changes: 2 additions & 0 deletions src/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use P4\MasterTheme\Migrations\M036RemoveEnFormOptions;
use P4\MasterTheme\Migrations\M037MigrateCoversContentBlockToPostsListBlock;
use P4\MasterTheme\Migrations\M038RemoveCustomSiteIcon;
use P4\MasterTheme\Migrations\M039EnableNewSocialSharePlatforms;

/**
* Run any new migration scripts and record results in the log.
Expand Down Expand Up @@ -97,6 +98,7 @@ public static function migrate(): void
M036RemoveEnFormOptions::class,
M037MigrateCoversContentBlockToPostsListBlock::class,
M038RemoveCustomSiteIcon::class,
M039EnableNewSocialSharePlatforms::class,
];

// Loop migrations and run those that haven't run yet.
Expand Down
17 changes: 17 additions & 0 deletions src/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,23 @@ public function share_meta(): array
];
}

/**
* Get values for share buttons content.
*/
public function social_share_platforms(): array
{
$social_share_options = planet4_get_option('social_share_options', []);

return [
'facebook' => in_array('facebook', $social_share_options),
'twitter' => in_array('twitter', $social_share_options),
'whatsapp' => in_array('whatsapp', $social_share_options),
'email' => in_array('email', $social_share_options),
// We might add a setting for this one in the future, but for now we always disable it in Posts.
'native' => false,
];
}

/**
* Get post's author override status.
*
Expand Down
44 changes: 43 additions & 1 deletion src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,21 @@ class Settings
*/
protected array $subpages = [];

/**
* Social share options
*/
public const SOCIAL_SHARE_OPTIONS = [
'facebook' => 'Facebook',
'whatsapp' => 'WhatsApp',
'twitter' => 'X',
'email' => 'Email',
];

/**
* Constructor
*/
public function __construct()
{

// Set our title.
$this->title = __('Planet 4', 'planet4-master-theme-backend');

Expand Down Expand Up @@ -397,6 +406,12 @@ public function __construct()
'type' => 'text',
],
],
[
'name' => __('Choose social sharing options', 'planet4-master-theme-backend'),
'id' => 'social_share_options',
'type' => 'social_share_checkboxes',
'default' => [],
],
],
],
'planet4_settings_404_page' => [
Expand Down Expand Up @@ -577,6 +592,7 @@ public function hooks(): void
add_filter('cmb2_render_get_informed_page_dropdown', [$this, 'p4_render_page_dropdown'], 10, 2);
add_filter('cmb2_render_take_action_page_dropdown', [$this, 'p4_render_page_dropdown'], 10, 2);
add_filter('cmb2_render_about_us_page_dropdown', [$this, 'p4_render_page_dropdown'], 10, 2);
add_filter('cmb2_render_social_share_checkboxes', [$this, 'p4_render_social_share_checkboxes'], 10, 2);
add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_assets']);

// Make settings multilingual if wpml plugin is installed and activated.
Expand Down Expand Up @@ -682,6 +698,32 @@ public function p4_render_category_dropdown(CMB2_Field $field_args, $value): voi
}
// phpcs:enable SlevomatCodingStandard.Functions.UnusedParameter

/**
* Render social share checkboxes.
*
* @param CMB2_Field $field_args Field arguments.
* @param array $value Value.
*/
public function p4_render_social_share_checkboxes(CMB2_Field $field_args, array $value): void
{
echo '<fieldset>';
foreach (self::SOCIAL_SHARE_OPTIONS as $key => $label) {
$selected = in_array($key, $value);

printf(
'<label class="social-share-checkbox">
<input type="checkbox" id="%1$s" name="%2$s" value="%3$s" %4$s />%5$s
</label>',
$field_args->id(),
$field_args->id() . '[' . $key . ']',
$key,
$selected ? 'checked' : '',
$label,
);
}
echo '</fieldset>';
}

/**
* Admin page markup. Mostly handled by CMB2.
*
Expand Down
5 changes: 4 additions & 1 deletion templates/blocks/header.twig
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@
<h1 class="page-header-title">{{ header_title|e('wp_kses_post')|raw }}</h1>
{% endif %}
{% if not hide_page_title and ( post.is_take_action_page ) %}
{% include "blocks/share_buttons.twig" with {social:post.share_meta} %}
{% include "blocks/share_buttons.twig" with {
social: post.share_meta,
share_platforms: post.social_share_platforms,
} %}
{% endif %}
{% if ( header_subtitle ) %}
<h3 class="page-header-subtitle">{{ header_subtitle }}</h3>
Expand Down
11 changes: 5 additions & 6 deletions templates/blocks/share_buttons.twig
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{% do action('enqueue_share_buttons_script', social) %}
{% set socialLink = (share_url ?? social.link) ~ '?utm_medium=' ~ utm_medium ~ utm_content_param ~ utm_campaign_param %}
{% set all_platforms = share_platforms is not defined %}
<div class="share-buttons">
<!-- Whatsapp -->
{% if all_platforms or share_platforms.whatsapp %}
{% if share_platforms.whatsapp %}
<a href="https://wa.me/?text={% if share_text or social.description %}{{ (share_text ?? social.description)|striptags|url_encode }} {% endif %}{{ (socialLink ~ '&utm_source=whatsapp')|url_encode }}"
onclick="window.dataLayerPush('Whatsapp');"
target="_blank" class="share-btn whatsapp">
Expand All @@ -12,7 +11,7 @@
</a>
{% endif %}
<!-- Facebook -->
{% if all_platforms or share_platforms.facebook %}
{% if share_platforms.facebook %}
<a href="https://www.facebook.com/sharer/sharer.php?u={{ (socialLink ~ '&utm_source=facebook')|url_encode }}"
onclick="window.dataLayerPush('Facebook');"
target="_blank" class="share-btn facebook">
Expand All @@ -21,7 +20,7 @@
</a>
{% endif %}
<!-- Twitter -->
{% if all_platforms or share_platforms.twitter %}
{% if share_platforms.twitter %}
<a href="https://x.com/intent/post?related={{ social_accounts.twitter }}&text={{ social.title|url_encode }}{% if share_text or social.description %} - {{ (share_text ?? social.description)|striptags|url_encode }}{% endif %}, via @{{ social_accounts.twitter }}&url={{ (socialLink ~ '&utm_source=twitter')|url_encode }}"
onclick="window.dataLayerPush('Twitter');"
target="_blank" class="share-btn twitter">
Expand All @@ -30,7 +29,7 @@
</a>
{% endif %}
<!-- Email -->
{% if all_platforms or share_platforms.email %}
{% if share_platforms.email %}
<a href="mailto:?subject={{ social.title|url_encode }}&body={% if share_text or social.description %}{{ (share_text ?? social.description)|striptags|url_encode }} {% endif %}{{ (socialLink ~ '&utm_source=email')|url_encode }}"
onclick="window.dataLayerPush('Email');"
target="_blank" class="share-btn email">
Expand All @@ -39,7 +38,7 @@
</a>
{% endif %}
<!-- Native (Gravity Forms only, for devices that support it) -->
{% if not all_platforms and share_platforms.native %}
{% if share_platforms.native %}
<a
onclick="window.nativeShare();"
target="_blank"
Expand Down
5 changes: 4 additions & 1 deletion templates/single-attachment.twig
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
<span class="single-post-meta-bullet" aria-hidden="true">&#8226;</span>
<time class="single-post-time" pubdate>{{ post.post_date|date }}</time>
</div>
{% include "blocks/share_buttons.twig" with {social:post.share_meta} %}
{% include "blocks/share_buttons.twig" with {
social:post.share_meta,
share_platforms: post.social_share_platforms,
} %}
</header>
</div>

Expand Down
6 changes: 5 additions & 1 deletion templates/single.twig
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@
{% endif %}
</div>
</div>
{% include "blocks/share_buttons.twig" with {social:post.share_meta, utm_medium: 'share'} %}
{% include "blocks/share_buttons.twig" with {
social: post.share_meta,
utm_medium: 'share',
share_platforms: post.social_share_platforms,
} %}

{% if old_posts_archive_notice.show_notice %}
<div class="single-post-old-posts-archive-notice">
Expand Down

0 comments on commit c31f666

Please sign in to comment.