Skip to content

Commit

Permalink
Consolidate scripts for General Settings overrides (#41295)
Browse files Browse the repository at this point in the history
* Consolidate scripts for General Settings overrides

* changelog

* Remove wpcom-fiverr/*

* Revert "Remove wpcom-fiverr/*"

This reverts commit 4c65d3b.

* Revert "Consolidate scripts for General Settings overrides"

This reverts commit a4dac17.

* Don't remove the original files

* Fix no DomainSettingsLinks on Atomic

* Fix no DomainSettingsLinks on Atomic: use getElementById
  • Loading branch information
okmttdhr authored Jan 30, 2025
1 parent 0818fd2 commit d1095dd
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Consolidate scripts for General Settings overrides
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,12 @@ public static function load_wpcom_user_features() {
require_once __DIR__ . '/features/wpcom-dashboard-widgets/wpcom-dashboard-widgets.php';
require_once __DIR__ . '/features/wpcom-locale/sync-locale-from-calypso-to-atomic.php';
require_once __DIR__ . '/features/wpcom-media/wpcom-external-media-import.php';
require_once __DIR__ . '/features/wpcom-options-general/options-general.php';
require_once __DIR__ . '/features/wpcom-plugins/wpcom-plugins.php';
require_once __DIR__ . '/features/wpcom-profile-settings/profile-settings-link-to-wpcom.php';
require_once __DIR__ . '/features/wpcom-profile-settings/profile-settings-notices.php';
require_once __DIR__ . '/features/wpcom-sidebar-notice/wpcom-sidebar-notice.php';
require_once __DIR__ . '/features/wpcom-siteurl/siteurl.php';
require_once __DIR__ . '/features/wpcom-themes/wpcom-themes.php';
require_once __DIR__ . '/features/wpcom-fiverr/wpcom-fiverr.php';

// Only load the Calypsoify and Masterbar features on WoA sites.
if ( class_exists( '\Automattic\Jetpack\Status\Host' ) && ( new \Automattic\Jetpack\Status\Host() )->is_woa_site() ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* Enqueue assets needed by the fiverr cta.
*/
function wpcom_fiverr_enqueue_scripts() {
function _wpcom_fiverr_enqueue_scripts() {
$asset_file = include Jetpack_Mu_Wpcom::BASE_DIR . 'build/wpcom-fiverr/wpcom-fiverr.asset.php';

wp_enqueue_style(
Expand All @@ -31,20 +31,20 @@ function wpcom_fiverr_enqueue_scripts() {
)
);
}
add_action( 'admin_enqueue_scripts', 'wpcom_fiverr_enqueue_scripts' );
add_action( 'admin_enqueue_scripts', '_wpcom_fiverr_enqueue_scripts' );

/**
* Add the fiverr cta to the general settings page.
*/
function wpcom_fiverr() {
add_settings_field( 'wpcom_fiverr_cta', '', 'wpcom_fiverr_cta', 'general', 'default' );
function _wpcom_fiverr() {
add_settings_field( 'wpcom_fiverr_cta', '', '_wpcom_fiverr_cta', 'general', 'default' );
}
add_action( 'admin_init', 'wpcom_fiverr' );
add_action( 'admin_init', '_wpcom_fiverr' );

/**
* Display the fiverr cta on the general settings page.
*/
function wpcom_fiverr_cta() {
function _wpcom_fiverr_cta() {
?>
<tr class="wpcom-fiverr-cta">
<th>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* Override /wp-admin/options-general.php
*
* @package automattic/jetpack-mu-wpcom
*/

use Automattic\Jetpack\Jetpack_Mu_Wpcom;

/**
* The fiverr cta's DOM.
*/
function wpcom_fiverr_cta() {
?>
<tr class="wpcom-fiverr-cta">
<th>
<?php esc_html_e( 'Site Logo', 'jetpack-mu-wpcom' ); ?>
</th>
<td>
<p><b><?php esc_html_e( 'Make an incredible logo in minutes', 'jetpack-mu-wpcom' ); ?></b></p>
<p><?php esc_html_e( 'Pre-designed by top talent. Just add your touch.', 'jetpack-mu-wpcom' ); ?></p>
<button class="wpcom-fiverr-cta-button button">
<svg width="20" height="20" viewBox="0 0 500 500" fill="none" xmlns="http://www.w3.org/2000/svg">
<circle cx="250" cy="250" r="177" fill="white"/>
<path d="M500 250C500 111.93 388.07 0 250 0C111.93 0 0 111.93 0 250C0 388.07 111.93 500 250 500C388.07 500 500 388.07 500 250ZM360.42 382.5H294.77V237.2H231.94V382.5H165.9V237.2H128.45V183.45H165.9V167.13C165.9 124.54 198.12 95.48 246.05 95.48H294.78V149.22H256.93C241.62 149.22 231.95 157.58 231.95 171.12V183.45H360.43V382.5H360.42Z" fill="#1DBF73"/>
</svg>
<?php esc_html_e( 'Try Fiverr Logo Maker', 'jetpack-mu-wpcom' ); ?>
</button>
</td>
</tr>
<?php
}

/**
* Add the fiverr cta to the general settings page.
*/
function wpcom_fiverr() {
add_settings_field( 'wpcom_fiverr_cta', '', 'wpcom_fiverr_cta', 'general', 'default' );
}
add_action( 'load-options-general.php', 'wpcom_fiverr' );

/**
* Display the site URL in General Settings on Simple Classic sites.
*/
function wpcom_enqueue_options_general_assets() {
$asset_file = include Jetpack_Mu_Wpcom::BASE_DIR . 'build/wpcom-options-general/wpcom-options-general.asset.php';

wp_enqueue_style(
'wpcom-options-general',
plugins_url( 'build/wpcom-options-general/wpcom-options-general.css', Jetpack_Mu_Wpcom::BASE_FILE ),
array(),
$asset_file['version'] ?? filemtime( Jetpack_Mu_Wpcom::BASE_DIR . 'build/wpcom-options-general/wpcom-options-general.css' )
);

wp_enqueue_script(
'wpcom-options-general',
plugins_url( 'build/wpcom-options-general/wpcom-options-general.js', Jetpack_Mu_Wpcom::BASE_FILE ),
$asset_file['dependencies'] ?? array(),
$asset_file['version'] ?? filemtime( Jetpack_Mu_Wpcom::BASE_DIR . 'build/wpcom-options-general/wpcom-options-general.js' ),
true
);

$site_slug = wpcom_get_site_slug();
$options_general_url = admin_url( 'options-general.php' );
wp_add_inline_script(
'wpcom-options-general',
'window.wpcomSiteUrl = ' . wp_json_encode(
array(
'siteUrl' => get_option( 'siteurl' ),
'homeUrl' => get_option( 'home' ),
'siteSlug' => $site_slug,
'optionsGeneralUrl' => $options_general_url,
)
) . ';',
'before'
);
}
add_action( 'admin_enqueue_scripts', 'wpcom_enqueue_options_general_assets' );
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.wpcom-fiverr-cta {
.wpcom-fiverr-cta-button {
margin-top: 1em;

svg {
vertical-align: middle;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { __, sprintf } from '@wordpress/i18n';
import { wpcomTrackEvent } from '../../common/tracks';

/**
* Build the Fiverr CTA.
* @param fragment - The document fragment for options general.
*/
const _wpcomBuildFiverrCta = ( fragment: DocumentFragment ) => {
const fiverrCtaRow = document.querySelector( '.wpcom-fiverr-cta' );
if ( fiverrCtaRow ) {
fragment.appendChild( fiverrCtaRow );
}
};

/**
* Set up the Fiverr CTA.
*/
function wpcomInitializeFiverrCta() {
const fiverCta = document.querySelector( '.wpcom-fiverr-cta-button' );
if ( fiverCta ) {
fiverCta.addEventListener( 'click', e => {
e.preventDefault();

wpcomTrackEvent( 'wp_admin_site_icon_fiverr_logo_maker_cta_click', {
cta_name: 'wp_admin_site_icon_fiverr_logo_maker',
} );

window.open( 'https://wp.me/logo-maker/?utm_campaign=general_settings', '_blank' );
} );
}
}

/**
* Build the Site Address (URL) and WordPress Address (URL) input fields.
* @param fragment - The document fragment for options general.
*/
const _wpcomBuildSiteUrl = ( fragment: DocumentFragment ) => {
if ( typeof window.wpcomSiteUrl === 'undefined' ) {
return;
}

// Create the WordPress Address (URL) row
if ( ! document.getElementById( 'siteurl' ) ) {
const siteUrlLabel = __( 'WordPress Address (URL)', 'jetpack-mu-wpcom' );
const siteUrlRow = document.createElement( 'tr' );
siteUrlRow.innerHTML = `
<th scope="row"><label for="siteurl">${ siteUrlLabel }</label></th>
<td><input type="url" id="siteurl" value="${ window.wpcomSiteUrl.siteUrl }" class="regular-text code disabled" disabled="disabled" /></td>
`;
fragment.appendChild( siteUrlRow );
}

// Create the Site Address (URL) row
if ( ! document.getElementById( 'home' ) ) {
const homeUrlLabel = __( 'Site Address (URL)', 'jetpack-mu-wpcom' );
const homeUrlRow = document.createElement( 'tr' );
homeUrlRow.innerHTML = `
<th scope="row"><label for="home">${ homeUrlLabel }</label></th>
<td><input type="url" id="home" value="${ window.wpcomSiteUrl.homeUrl }" class="regular-text code disabled" disabled="disabled" /></td>
`;
fragment.appendChild( homeUrlRow );
}
};

/**
* Adds domain settings links under the site URL input fields both on Simple and Atomic sites.
* @param fragment - The document fragment for options general.
*/
function _wpcomBuildDomainSettingsLinks( fragment: DocumentFragment ) {
if ( typeof window.wpcomSiteUrl === 'undefined' ) {
return;
}

// Either WordPress Address (URL) or Site Address (URL), whichever comes last.
const previousSibling =
( fragment.getElementById( 'home' ) as HTMLInputElement ) ||
( fragment.getElementById( 'siteurl' ) as HTMLInputElement ) ||
document.getElementById( 'home' ) ||
document.getElementById( 'siteurl' );
if ( ! previousSibling ) {
return;
}

const domainSettingsLink = document.createElement( 'p' );
domainSettingsLink.className = 'description';
domainSettingsLink.innerHTML = sprintf(
// translators: %1$s is the site slug, %2$s is the URL to the General Settings page.
__(
'Buy a <a href="https://wordpress.com/domains/add/%1$s?redirect_to=%2$s">custom domain</a>, ' +
'<a href="https://wordpress.com/domains/add/mapping/%1$s?redirect_to=%2$s">map</a> a domain you already own, ' +
'or <a href="https://wordpress.com/domains/add/site-redirect/%1$s?redirect_to=%2$s">redirect</a> this site.' +
' <a href="https://wordpress.com/domains/manage/%1$s/edit/%1$s">You can change your site address in Domain Settings</a>.',
'jetpack-mu-wpcom'
),
window.wpcomSiteUrl.siteSlug,
window.wpcomSiteUrl.optionsGeneralUrl
);

previousSibling.parentElement?.appendChild( domainSettingsLink );
}

/**
* Add the options general fragment.
*/
const wpcomAddOptionsGeneralFragment = () => {
const fragment = document.createDocumentFragment();

_wpcomBuildFiverrCta( fragment );
_wpcomBuildSiteUrl( fragment );
_wpcomBuildDomainSettingsLinks( fragment );

// Insert the fragment after the site icon section.
const siteIconSectionRow = document.querySelector( '.site-icon-section' );
if ( siteIconSectionRow ) {
// Insert the UI elements in a single operation to minimize reflow.
siteIconSectionRow.parentNode?.insertBefore( fragment, siteIconSectionRow.nextSibling );
}
};

document.addEventListener( 'DOMContentLoaded', function () {
wpcomAddOptionsGeneralFragment();
wpcomInitializeFiverrCta();
} );
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
declare global {
interface Window {
wpcomSiteUrl: {
siteUrl: string;
homeUrl: string;
siteSlug: string;
optionsGeneralUrl: string;
};
}
}

export {};
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* Enqueues the site URL script and localizes script data for General Settings.
*/
function wpcom_add_siteurl_to_general_settings() {
function _wpcom_add_siteurl_to_general_settings() {
if ( ! class_exists( 'Automattic\Jetpack\Status\Host' ) ) {
return;
}
Expand Down Expand Up @@ -39,4 +39,4 @@ function wpcom_add_siteurl_to_general_settings() {
'before'
);
}
add_action( 'load-options-general.php', 'wpcom_add_siteurl_to_general_settings' );
add_action( 'load-options-general.php', '_wpcom_add_siteurl_to_general_settings' );
9 changes: 4 additions & 5 deletions projects/packages/jetpack-mu-wpcom/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,18 @@ module.exports = [
'./src/features/wpcom-documentation-links/wpcom-documentation-links.ts',
'wpcom-external-media-import-page':
'./src/features/wpcom-media/wpcom-external-media-import.js',
'wpcom-options-general': [
'./src/features/wpcom-options-general/options-general.js',
'./src/features/wpcom-options-general/options-general.scss',
],
'wpcom-plugins-banner': './src/features/wpcom-plugins/js/banner.js',
'wpcom-plugins-banner-style': './src/features/wpcom-plugins/css/banner.css',
'wpcom-profile-settings-link-to-wpcom':
'./src/features/wpcom-profile-settings/profile-settings-link-to-wpcom.ts',
'wpcom-sidebar-notice': './src/features/wpcom-sidebar-notice/wpcom-sidebar-notice.js',
'wpcom-siteurl': './src/features/wpcom-siteurl/siteurl.js',
'starter-page-templates': './src/features/starter-page-templates/index.tsx',
'removed-calypso-screen-notice':
'./src/features/wpcom-admin-interface/removed-calypso-screen-notice.tsx',
'wpcom-fiverr': [
'./src/features/wpcom-fiverr/wpcom-fiverr.ts',
'./src/features/wpcom-fiverr/wpcom-fiverr.scss',
],
},
mode: jetpackWebpackConfig.mode,
devtool: jetpackWebpackConfig.devtool,
Expand Down

0 comments on commit d1095dd

Please sign in to comment.