Skip to content

Commit

Permalink
Merge branch 'add/traffic-boost' into add/dashboard-header
Browse files Browse the repository at this point in the history
  • Loading branch information
vaurdan committed Nov 15, 2024
2 parents 9c431f9 + 31a3ab9 commit d440061
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 35 deletions.
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-components', 'wp-dom-ready', 'wp-element', 'wp-primitives'), 'version' => '899988226d2180e0bead');
<?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' => 'df37572f8d1f45c12fc4');
<?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 @@ -106,20 +108,7 @@ public function add_dashboard_page_to_menu(): void {
* @since 3.18.0
*/
public function add_dashboard_page_placeholder(): void {
echo '<div class="wp-parsely-dashboard-container" 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.';
}
echo '<div id="parsely-dashboard-page"></div>';
}

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

// 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',
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'
);
}

/**
* 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();
}

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

$settings = wp_json_encode( $settings );

wp_add_inline_script(
'parsely-dashboard-page',
"window.wpParselyContentHelperSettings = '$settings';",
'before'
);
}
}
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;
}
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';

/**
* 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,3 +1,55 @@
/**
* Internal dependencies
*/
import { SettingsProvider, TrafficBoostSettings } from '../../../common/settings';
import { getContentHelperPermissions } from '../../../common/utils/permissions';
import { VerifyCredentials } from '../../../common/verify-credentials';
import { PageContainer, PageBody } from '../../components';
import { DashboardHeader } from './components/header-component';

/**
* 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;
};

import { PageContainer, PageBody } from '../../components';
import { DashboardHeader } from './components/header-component';

Expand All @@ -8,11 +60,20 @@ import { DashboardHeader } from './components/header-component';
*/
export const DashboardPage = (): React.JSX.Element => {
return (
<PageContainer name="dashboard">
<DashboardHeader />
<PageBody>
<p>This is the dashboard page</p>
</PageBody>
</PageContainer>
<SettingsProvider
endpoint="traffic-boost"
defaultSettings={ getSettingsFromJson( window.wpParselyContentHelperSettings ) }
>
<VerifyCredentials>
<PageContainer name="dashboard">
<DashboardHeader />
<PageBody>
<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>
</PageBody>
</PageContainer>
</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!',
),
);
}
}
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 ),
);

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

0 comments on commit d440061

Please sign in to comment.