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

Media Kit page handling #3358

Merged
merged 4 commits into from
Sep 20, 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
8 changes: 0 additions & 8 deletions includes/class-plugin-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,14 +314,6 @@ public static function get_managed_plugins() {
'Download' => 'wporg',
'EditPath' => 'options-general.php?page=adstxt-settings',
],
'publisher-media-kit' => [
'Name' => \esc_html__( 'Publisher Media Kit', 'newspack' ),
'Description' => \esc_html__( 'Quick and easy option for small to medium sized publishers to digitize their media kit.', 'newspack' ),
'Author' => \esc_html__( '10up', 'newspack' ),
'AuthorURI' => \esc_url( 'https://10up.com' ),
'PluginURI' => \esc_url( 'https://wordpress.org/plugins/publisher-media-kit/' ),
'Download' => 'wporg',
],
'broadstreet' => [
'Name' => \esc_html__( 'Broadstreet', 'newspack' ),
'Description' => \esc_html__( 'Integrate Broadstreet’s business directory and ad-serving features into your site.', 'newspack' ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,61 @@ public function is_service_enabled( $service ) {
return get_option( Advertising_Wizard::NEWSPACK_ADVERTISING_SERVICE_PREFIX . $service, false );
}

/**
* Get Media Kit page ID.
*/
public static function get_media_kit_page_id() {
$page_id = false;
try {
$page_id = \Newspack_Ads\Media_Kit::get_existing_page_id();
} catch ( \Throwable $th ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
// Do nothing.
}
return $page_id;
}

/**
* Get Media Kit page.
*/
public function get_media_kit_page_edit_url() {
$raw_url = get_edit_post_link( $this->get_media_kit_page_id(), '' );
if ( ! $raw_url ) {
return false;
}
$parsed_url = wp_parse_url( $raw_url );
return $parsed_url['path'] . '?' . $parsed_url['query'];
}

/**
* Get Media Kit page status.
*/
public function get_media_kit_page_status() {
if ( method_exists( '\Newspack_Ads\Media_Kit', 'get_page_status' ) ) {
return \Newspack_Ads\Media_Kit::get_page_status();
}
return false;
}

/**
* Create the Media Kit page.
*/
public function create_media_kit_page() {
if ( method_exists( '\Newspack_Ads\Media_Kit', 'create_media_kit_page' ) ) {
return \Newspack_Ads\Media_Kit::create_media_kit_page();
}
return false;
}

/**
* Unpublish the Media Kit page.
*/
public function unpublish_media_kit_page() {
if ( method_exists( '\Newspack_Ads\Media_Kit', 'create_media_kit_page' ) ) {
return \Newspack_Ads\Media_Kit::create_media_kit_page();
}
return false;
}

/**
* Configure Newspack Ads for Newspack use.
*
Expand Down
72 changes: 69 additions & 3 deletions includes/wizards/class-advertising-wizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,28 @@ function( $acc, $code ) {
],
]
);

// Create the Media Kit page.
\register_rest_route(
NEWSPACK_API_NAMESPACE,
'/wizard/billboard/media-kit',
[
'methods' => \WP_REST_Server::CREATABLE,
'callback' => [ $this, 'api_create_media_kit_page' ],
'permission_callback' => [ $this, 'api_permissions_check' ],
]
);

// Unpublish the Media Kit page.
\register_rest_route(
NEWSPACK_API_NAMESPACE,
'/wizard/billboard/media-kit',
[
'methods' => \WP_REST_Server::DELETABLE,
'callback' => [ $this, 'api_unpublish_media_kit_page' ],
'permission_callback' => [ $this, 'api_permissions_check' ],
]
);
}

/**
Expand Down Expand Up @@ -397,6 +419,48 @@ public function api_delete_adunit( $request ) {
return \rest_ensure_response( $this->retrieve_data() );
}

/**
* Create the Media Kit page.
*/
public static function api_create_media_kit_page() {
$configuration_manager = Configuration_Managers::configuration_manager_class_for_plugin_slug( 'newspack-ads' );
$edit_url = $configuration_manager->get_media_kit_page_edit_url();
if ( ! $edit_url ) {
$configuration_manager->create_media_kit_page();
}
return \rest_ensure_response(
[
'edit_url' => $configuration_manager->get_media_kit_page_edit_url(),
'page_status' => $configuration_manager->get_media_kit_page_status(),
]
);
}

/**
* Unpublish (revert to draft) the Media Kit page.
*/
public static function api_unpublish_media_kit_page() {
$configuration_manager = Configuration_Managers::configuration_manager_class_for_plugin_slug( 'newspack-ads' );
$page_id = $configuration_manager->get_media_kit_page_id();
if ( $page_id ) {
$update = wp_update_post(
[
'ID' => $page_id,
'post_status' => 'draft',
]
);
if ( $update === 0 || is_wp_error( $update ) ) {
return rest_ensure_response( new WP_Error( 'update_failed', __( 'Failed to update page status.', 'newspack' ) ) );
}
}
return \rest_ensure_response(
[
'edit_url' => $configuration_manager->get_media_kit_page_edit_url(),
'page_status' => $configuration_manager->get_media_kit_page_status(),
]
);
}

/**
* Retrieve all advertising data.
*
Expand Down Expand Up @@ -504,13 +568,15 @@ public function enqueue_scripts_and_styles() {
\wp_style_add_data( 'newspack-advertising-wizard', 'rtl', 'replace' );
\wp_enqueue_style( 'newspack-advertising-wizard' );

$configuration_manager = Configuration_Managers::configuration_manager_class_for_plugin_slug( 'newspack-ads' );
\wp_localize_script(
'newspack-advertising-wizard',
'newspack_ads_wizard',
array(
'iab_sizes' => function_exists( '\Newspack_Ads\get_iab_sizes' ) ? \Newspack_Ads\get_iab_sizes() : [],
'mediakit_edit_url' => get_option( 'pmk-page' ) ? get_edit_post_link( get_option( 'pmk-page' ) ) : '',
'can_connect_google' => OAuth::is_proxy_configured( 'google' ),
'iab_sizes' => function_exists( '\Newspack_Ads\get_iab_sizes' ) ? \Newspack_Ads\get_iab_sizes() : [],
'media_kit_page_edit_url' => $configuration_manager->get_media_kit_page_edit_url(),
'media_kit_page_status' => $configuration_manager->get_media_kit_page_status(),
'can_connect_google' => OAuth::is_proxy_configured( 'google' ),
)
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/components/src/action-card/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class ActionCard extends Component {
hasWhiteHeader,
isPending,
expandable = false,
isButtonEnabled = false
} = this.props;

const { expanded } = this.state;
Expand Down Expand Up @@ -173,7 +174,7 @@ class ActionCard extends Component {
</Handoff>
) : onClick || hasInternalLink ? (
<Button
disabled={ disabled }
disabled={ disabled && ! isButtonEnabled }
isLink
href={ href }
onClick={ onClick }
Expand Down
101 changes: 92 additions & 9 deletions src/wizards/advertising/components/add-ons/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* globals newspack_ads_wizard */

/**
* Ad Add-ons component
*/
Expand All @@ -7,14 +8,100 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import apiFetch from '@wordpress/api-fetch';
import { Notice } from '@wordpress/components';

/**
* Internal dependencies
*/
import { PluginToggle } from '../../../../components/src';
import { PluginToggle, ActionCard } from '../../../../components/src';
import { useState } from 'react';

const MediaKitToggle = () => {
const [ isInFlight, setInFlight ] = useState( false );
const [ editURL, setEditURL ] = useState(
newspack_ads_wizard.media_kit_page_edit_url
);
const [ pageStatus, setPageStatus ] = useState(
newspack_ads_wizard.media_kit_page_status
);

if (
! newspack_ads_wizard.media_kit_page_status &&
! newspack_ads_wizard.media_kit_page_edit_url
) {
return (
<Notice isDismissible={ false } status="error">
{ __(
'Something went wrong, the Media Kit feature is unavailable.',
'newspack-plugin'
) }
</Notice>
);
}

const isPagePublished = pageStatus === 'publish';

const toggleMediaKit = () => {
setInFlight( true );
apiFetch( {
path: '/newspack/v1/wizard/billboard/media-kit',
method: isPagePublished ? 'DELETE' : 'POST',
} )
.then( ( { edit_url, page_status } ) => {
setEditURL( edit_url );
setPageStatus( page_status );
} )
.finally( () => setInFlight( false ) );
};

const props = editURL
? {
href: editURL,
actionText: isPagePublished
? __( 'Edit Media Kit page', 'newspack-plugin' )
: __( 'Review draft page', 'newspack-plugin' ),
}
: {};

let description = __(
'Media kit page is created but unpublished, click the link to review and publish.',
'newspack-plugin'
);
let toggleEnabled = false;
switch ( pageStatus ) {
case 'publish':
description = __(
'Media Kit page is published. Click the link to edit it, or toggle this card to unpublish.',
'newspack-plugin'
);
toggleEnabled = true;
break;
case 'non-existent':
description = __(
'Media Kit page has not been created. Toggle this card to create it.',
'newspack-plugin'
);
toggleEnabled = true;
break;
}

export default function AddOns() {
return (
<ActionCard
disabled={ isInFlight || ! toggleEnabled }
isButtonEnabled={ true }
title={ __( 'Media Kit', 'newspack-plugin' ) }
description={ description }
toggle
toggleChecked={ Boolean( editURL ) && isPagePublished }
toggleOnChange={ toggleMediaKit }
{ ...props }
/>
);
};

export default () => (
<>
<PluginToggle
plugins={ {
'super-cool-ad-inserter': {
Expand All @@ -25,12 +112,8 @@ export default function AddOns() {
actionText: __( 'Configure', 'newspack-plugin' ),
href: '#/settings',
},
'publisher-media-kit': {
shouldRefreshAfterUpdate: true,
actionText: __( 'Edit Media Kit', 'newspack-plugin' ),
href: newspack_ads_wizard.mediakit_edit_url ? newspack_ads_wizard.mediakit_edit_url : '',
},
} }
/>
);
}
<MediaKitToggle />
</>
);