Skip to content

Commit

Permalink
PLANET-7047: Migrate Donate button setting to a menu (#2076)
Browse files Browse the repository at this point in the history
* Ref: https://jira.greenpeace.org/browse/PLANET-7047

Migrate Donate button settings

- Create a new term if not exists
- Remove donate button settings page
- Unset donate settings from planet4_options
- Remove default donate settings within the context
- Update desktop and mobile templates

---------

Co-authored-by: Dan Tovbein <[email protected]>
  • Loading branch information
GP-Dan-Tovbein and dantovbein authored Aug 2, 2023
1 parent 889c572 commit 0547c5e
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 38 deletions.
15 changes: 1 addition & 14 deletions src/MasterSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -587,27 +587,16 @@ function ($item) {
}
);

$donate_menu_default[] = [
'link' => planet4_get_option('donate_button', '#'),
'title' => planet4_get_option('donate_text', __('Donate', 'planet4-master-theme')),
];

// Donate button menu dropdown.
// If no Donate menu is defined, we use the old settings from Planet 4 > Donate.
$donate_menu_items = $donate_menu_default;

// Check if the menu has been created.
if (has_nav_menu('donate-menu')) {
$donate_menu = new TimberMenu('donate-menu');

// Check if it has at least 1 item added into the menu.
if (! empty($donate_menu->get_items())) {
$donate_menu_items = $donate_menu->get_items();
$context['donate_menu_items'] = $donate_menu->get_items();
}
}

$context['donate_menu_default'] = $donate_menu_default;
$context['donate_menu_items'] = $donate_menu_items;

$languages = function_exists('icl_get_languages') ? icl_get_languages() : [];
$context['site_languages'] = $languages;
Expand Down Expand Up @@ -651,8 +640,6 @@ function ($item) {
$context['p4_visitor_type'] = 'guest';
}

$context['donatelink'] = $options['donate_button'] ?? '#';
$context['donatetext'] = $options['donate_text'] ?? __('Donate', 'planet4-master-theme');
$context['website_navbar_title'] = $options['website_navigation_title']
?? __('International (English)', 'planet4-master-theme');

Expand Down
69 changes: 69 additions & 0 deletions src/Migrations/M018MigrateDonateButtonSetting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace P4\MasterTheme\Migrations;

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

/**
* Migrate Donate button setting to a menu.
*/
class M018MigrateDonateButtonSetting 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
{
global $wpdb;

$donate_menu_slug = 'donate-menu';
$option_key = 'planet4_options';

$sql = 'SELECT t.term_id FROM wp_terms AS t WHERE t.slug="%s"';
$prepared_sql = $wpdb->prepare($sql, array($donate_menu_slug));
$results = $wpdb->get_results($prepared_sql);

if (count($results)) {
return;
}

$term = wp_insert_term(
'Donate Menu',
'nav_menu',
[
'slug' => $donate_menu_slug,
],
);

if (is_wp_error($term) || !isset($term['term_id'])) {
return;
}

$term_id = $term['term_id'];

$options = get_option($option_key);

wp_update_nav_menu_item(
$term_id,
0,
[
'menu-item-title' => $options['donate_text'],
'menu-item-url' => $options['donate_button'],
'menu-item-status' => 'publish',
'menu-item-type' => 'custom',
],
);

$nav_menu_locations = get_theme_mod('nav_menu_locations');
$nav_menu_locations[$donate_menu_slug] = (int) $term['term_id'];
set_theme_mod('nav_menu_locations', $nav_menu_locations);

unset($options['donate_text']);
unset($options['donate_button']);
update_option($option_key, $options);
}
}
2 changes: 2 additions & 0 deletions src/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use P4\MasterTheme\Migrations\M015RemoveListingPagesBackgroundImage;
use P4\MasterTheme\Migrations\M016CreateDefaultActionType;
use P4\MasterTheme\Migrations\M017NewIAToggle;
use P4\MasterTheme\Migrations\M018MigrateDonateButtonSetting;

/**
* Run any new migration scripts and record results in the log.
Expand Down Expand Up @@ -55,6 +56,7 @@ public static function migrate(): void
M015RemoveListingPagesBackgroundImage::class,
M016CreateDefaultActionType::class,
M017NewIAToggle::class,
M018MigrateDonateButtonSetting::class,
];

// Loop migrations and run those that haven't run yet.
Expand Down
22 changes: 0 additions & 22 deletions src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,28 +99,6 @@ public function __construct()
],
],
],
'planet4_settings_donate_button' => [
'title' => 'Donate button',
'fields' => [
[
'name' => __('Donate button link', 'planet4-master-theme-backend'),
'id' => 'donate_button',
'type' => 'text',
'attributes' => [
'type' => 'text',
],
],

[
'name' => __('Donate button text', 'planet4-master-theme-backend'),
'id' => 'donate_text',
'type' => 'text',
'attributes' => [
'type' => 'text',
],
],
],
],
'planet4_settings_defaults_content' => [
'title' => 'Defaults content',
'fields' => [
Expand Down
4 changes: 2 additions & 2 deletions templates/burger-menu.twig
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@
<div class="burger-menu-footer">
<a
class="btn btn-donate"
href="{{ donate_menu_default[0].link }}"
href="{{ donate_menu_items[0].link }}"
data-ga-category="Menu Navigation"
data-ga-action="Donate"
data-ga-label="{{ page_category }}">
{{ donate_menu_default[0].title }}
{{ donate_menu_items[0].title }}
</a>
</div>
</div>
Expand Down

0 comments on commit 0547c5e

Please sign in to comment.