Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Traffic Boost settings infrastructure #2942

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/content-helper/dashboard-page.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'react-dom', 'wp-dom-ready', 'wp-element'), 'version' => '18278c930ac085311870');
<?php return array('dependencies' => array('react', 'react-dom', 'wp-api-fetch', 'wp-data', 'wp-dom-ready', 'wp-element', 'wp-i18n'), 'version' => '801d6f5bef2f27842da3');
2 changes: 1 addition & 1 deletion build/content-helper/dashboard-page.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/content-helper/editor-sidebar.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-core-data', 'wp-data', 'wp-dom-ready', 'wp-editor', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins', 'wp-primitives', 'wp-url', 'wp-wordcount'), 'version' => 'd7059d961c9fe0a71c34');
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-core-data', 'wp-data', 'wp-dom-ready', 'wp-editor', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins', 'wp-primitives', 'wp-url', 'wp-wordcount'), 'version' => '91832340c46f7a9be8e6');
2 changes: 1 addition & 1 deletion build/content-helper/editor-sidebar.js

Large diffs are not rendered by default.

68 changes: 54 additions & 14 deletions src/UI/class-dashboard-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Parsely\Parsely;
use Parsely\Permissions;
use Parsely\Utils\Utils;
use WP_REST_Request;

use const Parsely\PARSELY_FILE;

Expand All @@ -25,6 +26,7 @@ final class Dashboard_Page {
/**
* Instance of Parsely class.
*
* @since 3.18.0
* @var Parsely
*/
private $parsely;
Expand Down Expand Up @@ -107,19 +109,6 @@ public function add_dashboard_page_to_menu(): void {
*/
public function add_dashboard_page_placeholder(): void {
echo '<div id="parsely-dashboard-page"></div>';

// TODO: The codeblock below is for demonstration purposes only and
// will be removed in the future.
if (
Permissions::current_user_can_use_pch_feature(
'traffic_boost',
$this->parsely->get_options()['content_helper']
)
) {
echo 'Traffic Boost is enabled.';
} else {
echo 'Traffic Boost is disabled.';
}
}

/**
Expand Down Expand Up @@ -168,11 +157,62 @@ public function enqueue_dashboard_page_scripts( ?string $hook_suffix ): void {
true
);

// Inline scripts must be injected after enqueueing the main script.
$this->inject_content_helper_permissions();
$this->inject_traffic_boost_settings();

wp_enqueue_style(
'parsely-dashboard-page',
$built_assets_url . 'dashboard-page.css',
$asset_info['dependencies'],
array(),
$asset_info['version']
);
}

/**
* Injects Content Helper permissions into the dashboard page.
*
* @since 3.18.0
*/
protected function inject_content_helper_permissions(): void {
$permissions_json = Permissions::get_pch_permissions_json(
$this->parsely->get_options()['content_helper']
);

wp_add_inline_script(
'parsely-dashboard-page',
"window.wpParselyContentHelperPermissions = '$permissions_json';",
'before'
);
}
acicovic marked this conversation as resolved.
Show resolved Hide resolved

/**
* Injects Traffic Boost settings into the dashboard page.
*
* @since 3.18.0
*/
protected function inject_traffic_boost_settings(): void {
$settings = '';

if ( ! defined( 'INTEGRATION_TESTS_RUNNING' ) ) {
$settings = rest_do_request(
new WP_REST_Request(
'GET',
'/wp-parsely/v2/settings/traffic-boost'
)
)->get_data();
}
acicovic marked this conversation as resolved.
Show resolved Hide resolved

if ( ! is_array( $settings ) ) {
$settings = array();
}

$settings = wp_json_encode( $settings );

wp_add_inline_script(
'parsely-dashboard-page',
"window.wpParselyContentHelperSettings = '$settings';",
'before'
);
acicovic marked this conversation as resolved.
Show resolved Hide resolved
}
}
4 changes: 3 additions & 1 deletion src/content-helper/common/settings/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
TitleSuggestionsSettings,
} from './sidebar-settings';
import type { TopPostsSettings } from './top-posts-settings';
import type { TrafficBoostSettings } from './traffic-boost-settings';

/**
* Export the settings types.
Expand All @@ -22,7 +23,8 @@ export type {
SmartLinkingSettings, // Part of SidebarSettings type.
TitleSuggestionsSettings, // Part of SidebarSettings type.
TopPostsSettings,
TrafficBoostSettings,
};

// Generic type for settings.
export type Settings = SidebarSettings | TopPostsSettings;
export type Settings = SidebarSettings | TopPostsSettings | TrafficBoostSettings;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Defines the Traffic Boost settings structure.
*
* @since 3.18.0
*/
export interface TrafficBoostSettings {
Setting1: string;
}
acicovic marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 4 additions & 2 deletions src/content-helper/common/utils/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
* @since 3.16.0
*/
export interface ContentHelperPermissions {
ExcerptSuggestions: boolean;
SmartLinking: boolean;
TitleSuggestions: boolean;
ExcerptSuggestions: boolean;
TrafficBoost: boolean;
}

/**
Expand All @@ -18,9 +19,10 @@ export interface ContentHelperPermissions {
*/
export function getContentHelperPermissions(): ContentHelperPermissions {
const defaultPermissions: ContentHelperPermissions = {
ExcerptSuggestions: false,
SmartLinking: false,
TitleSuggestions: false,
ExcerptSuggestions: false,
TrafficBoost: false,
};

try {
Expand Down
27 changes: 19 additions & 8 deletions src/content-helper/dashboard-page/dashboard-page.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
/**
* WordPress dependencies
* External dependencies
*/
import domReady from '@wordpress/dom-ready';
import { createRoot, useEffect } from '@wordpress/element';
import {
Route,
HashRouter as Router,
Routes,
useLocation,
} from 'react-router-dom';

/**
* External dependencies
* WordPress dependencies
*/
import { HashRouter as Router, Route, Routes, useLocation } from 'react-router-dom';
import domReady from '@wordpress/dom-ready';
import { createRoot, useEffect } from '@wordpress/element';
acicovic marked this conversation as resolved.
Show resolved Hide resolved

/**
* Internal dependencies
Expand Down Expand Up @@ -47,9 +52,13 @@ const ParselyDashboard = () => {
* @since 3.18.0
*/
useEffect( () => {
const firstLink = document.querySelector( '#toplevel_page_parsely-dashboard-page .wp-submenu li a.wp-first-item' );
const firstLink = document.querySelector(
'#toplevel_page_parsely-dashboard-page .wp-submenu li a.wp-first-item'
);
if ( firstLink ) {
firstLink.setAttribute( 'href', window.location.pathname + window.location.search + '#/' );
firstLink.setAttribute(
'href', window.location.pathname + window.location.search + '#/'
);
}
}, [] );

Expand All @@ -59,7 +68,9 @@ const ParselyDashboard = () => {
* @since 3.18.0
*/
useEffect( () => {
const submenuItems = document.querySelectorAll( '#toplevel_page_parsely-dashboard-page .wp-submenu li' );
const submenuItems = document.querySelectorAll(
'#toplevel_page_parsely-dashboard-page .wp-submenu li'
);

submenuItems.forEach( ( item ) => {
const link = item.querySelector( 'a' );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,72 @@
/**
* Internal dependencies
*/
import { SettingsProvider, TrafficBoostSettings } from '../../../common/settings';
import { getContentHelperPermissions } from '../../../common/utils/permissions';
import { VerifyCredentials } from '../../../common/verify-credentials';

/**
* Gets the settings from the passed JSON.
*
* If missing settings or invalid values are detected, they get set to their
* defaults.
*
* @since 3.18.0
*
* @param {string} settingsJson The JSON containing the settings.
*
* @return {TrafficBoostSettings} The resulting settings object.
*/
const getSettingsFromJson = ( settingsJson: string ): TrafficBoostSettings => {
// Default settings object.
const defaultSettings: TrafficBoostSettings = {
Setting1: 'Hello World!',
};

// If the settings are empty, try to get them from the global variable.
if ( '' === settingsJson ) {
settingsJson = window.wpParselyContentHelperSettings;
}

let parsedSettings: TrafficBoostSettings;

try {
parsedSettings = JSON.parse( settingsJson );
} catch ( e ) {
// Return defaults when parsing failed or the string is empty.
return defaultSettings;
}

// Merge parsed settings with default settings.
const mergedSettings = { ...defaultSettings, ...parsedSettings };

// Fix invalid values if any are found.
if ( typeof mergedSettings.Setting1 !== 'string' ) {
mergedSettings.Setting1 = defaultSettings.Setting1;
}

return mergedSettings;
};

/**
* The main dashboard page component.
*
* @since 3.18.0
*/
export const DashboardPage = () => {
return (
<>
<h1>Parse.ly Dashboard</h1>
<p>Welcome to the main Parse.ly dashboard page.</p>
</>
<SettingsProvider
endpoint="traffic-boost"
defaultSettings={ getSettingsFromJson( window.wpParselyContentHelperSettings ) }
>
<VerifyCredentials>
<>
<h1>Parse.ly</h1>
<p>Welcome to the Parse.ly Dashboard page!</p>
<p>Content Helper Permissions: { JSON.stringify( getContentHelperPermissions() ) }</p>
<p>Traffic Boost Settings: { JSON.stringify( getSettingsFromJson( window.wpParselyContentHelperSettings ) ) }</p>
</>
acicovic marked this conversation as resolved.
Show resolved Hide resolved
</VerifyCredentials>
</SettingsProvider>
);
};
58 changes: 58 additions & 0 deletions src/rest-api/settings/class-endpoint-traffic-boost-settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* API Endpoint: Traffic Boost Settings
*
* @package Parsely
* @since 3.18.0
*/

declare(strict_types=1);

namespace Parsely\REST_API\Settings;

/**
* Endpoint for saving and retrieving Content Helper Traffic Boost settings.
*
* @since 3.18.0
*
* @phpstan-import-type Subvalue_Spec from Base_Settings_Endpoint
*/
class Endpoint_Traffic_Boost_Settings extends Base_Settings_Endpoint {
/**
* Returns the endpoint's name.
*
* @since 3.18.0
*
* @return string
*/
public static function get_endpoint_name(): string {
return 'traffic-boost';
}

/**
* Returns the meta entry's key.
*
* @since 3.18.0
*
* @return string The meta entry's key.
*/
protected function get_meta_key(): string {
return 'parsely_content_helper_settings_traffic_boost';
}

/**
* Returns the endpoint's subvalues specifications.
*
* @since 3.18.0
*
* @return array<string, Subvalue_Spec>
*/
protected function get_subvalues_specs(): array {
return array(
'Setting1' => array(
'values' => array(),
'default' => 'Hello World!',
),
);
}
acicovic marked this conversation as resolved.
Show resolved Hide resolved
}
1 change: 1 addition & 0 deletions src/rest-api/settings/class-settings-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function init(): void {
$endpoints = array(
new Endpoint_Dashboard_Widget_Settings( $this ),
new Endpoint_Editor_Sidebar_Settings( $this ),
new Endpoint_Traffic_Boost_Settings( $this ),
acicovic marked this conversation as resolved.
Show resolved Hide resolved
);

$this->register_endpoints( $endpoints );
Expand Down
Loading