Skip to content
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/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' => '0052a54c44cf19a09041');
<?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' => '277a0eb866200775564f');
2 changes: 1 addition & 1 deletion build/content-helper/editor-sidebar.js

Large diffs are not rendered by default.

77 changes: 62 additions & 15 deletions src/Models/class-smart-link.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ class Smart_Link extends Base_Model {
*/
public $source_post_id = 0;

/**
* The context of the smart link.
*
* For example, 'traffic_boost' or 'smart_linking'.
*
* @since 3.18.0
* @var string|null The context of the smart link.
*/
protected $context = null;

/**
* The source post object.
*
Expand Down Expand Up @@ -255,6 +265,11 @@ private function load(): bool {

$this->status = $this->get_status();

// Load the context of the smart link, if it exists.
if ( isset( $this->smart_link_post_meta['_smart_link_context'] ) ) {
$this->context = $this->get_string_meta( '_smart_link_context' );
}

// Load the source post ID.
$source_terms = wp_get_post_terms( $this->smart_link_id, 'smart_link_source' );
if ( ! is_wp_error( $source_terms ) && count( $source_terms ) > 0 ) {
Expand Down Expand Up @@ -354,6 +369,10 @@ public function save(): bool {
'_smart_link_offset' => $this->offset,
);

if ( null !== $this->context ) {
$meta['_smart_link_context'] = $this->context;
}

foreach ( $meta as $key => $value ) {
update_post_meta( $this->smart_link_id, $key, $value );
}
Expand Down Expand Up @@ -455,14 +474,30 @@ public function get_link_href( $skip_utm_params = false ): string {
return $this->href;
}

return Utils::append_itm_params(
$this->href,
array(
'campaign' => 'parsely-pch',
'source' => 'smart-link',
'term' => $this->uid,
)
$params = array(
'campaign' => 'wp-parsely',
'medium' => 'smart-link',
'term' => $this->uid,
);

// If the context is set, add it to the params as the source.
if ( null !== $this->get_context() ) {
// Replace underscores with hyphens, for consistency with the ITM parameters.
$params['source'] = str_replace( '_', '-', $this->get_context() );
}

return Utils::append_itm_params( $this->href, $params );
}

/**
* Returns the context of the smart link.
*
* @since 3.18.0
*
* @return string|null The context of the smart link.
*/
public function get_context() {
return $this->context;
}

/**
Expand Down Expand Up @@ -704,6 +739,17 @@ public function set_href( string $href ): void {
}
}

/**
* Sets the context of the smart link.
*
* @since 3.18.0
*
* @param string $context The context of the smart link.
*/
public function set_context( string $context ): void {
$this->context = $context;
}

/**
* Generates a unique ID for the suggested link.
*
Expand Down Expand Up @@ -736,6 +782,7 @@ public function to_array(): array {
'title' => $this->title,
'text' => $this->text,
'offset' => $this->offset,
'context' => $this->context,
'status' => $this->status,
'applied' => $this->is_applied(),
'source' => array(
Expand Down Expand Up @@ -921,16 +968,16 @@ public static function get_smart_links( int $post_id, string $type, string $stat
}

if ( is_callable( $process_smart_link_callback ) ) {
/**
/**
* The processed smart link after it has been processed by the callback.
*
* This callback is used to modify the smart link before it is added to the array,
* or false if the smart link should be skipped.
*
* @since 3.18.0
*
* @var Smart_Link|Inbound_Smart_Link|false|null $smart_link
* */
*
* @var Smart_Link|Inbound_Smart_Link|false|null $smart_link
*/
$smart_link = $process_smart_link_callback( $smart_link );
}

Expand Down Expand Up @@ -969,7 +1016,7 @@ public static function get_outbound_smart_links( int $post_id, string $status =
array(
'orderby' => 'date',
'order' => 'ASC',
)
)
);
}

Expand All @@ -988,9 +1035,9 @@ public static function get_outbound_smart_links( int $post_id, string $status =
public static function get_inbound_smart_links( int $post_id, string $status = Smart_Link_Status::ALL ): array {
/** @var array<Inbound_Smart_Link> */
return self::get_smart_links(
$post_id,
'inbound',
$status,
$post_id,
'inbound',
$status,
array(
'orderby' => 'date modified',
'order' => 'ASC',
Expand Down
5 changes: 3 additions & 2 deletions src/content-helper/editor-sidebar/smart-linking/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -763,8 +763,9 @@ export function getAllSmartLinksURLs( smartLinks: SmartLink[] ): string[] {
*/
export function addSmartLinkITMParamsToURL( url: string, smartLinkUid: string ): string {
return addITMParamsToURL( url, {
campaign: 'parsely-pch',
source: 'smart-link',
campaign: 'wp-parsely',
medium: 'smart-link',
source: 'smart-linking',
term: smartLinkUid,
} );
}
Expand Down
13 changes: 13 additions & 0 deletions src/rest-api/content-helper/class-endpoint-smart-linking.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ public function generate_smart_links( WP_REST_Request $request ) {

$smart_links = array_map(
function ( Smart_Link $link ) {
// Set the context to Smart Linking.
$link->set_context( $this->get_pch_feature_name() );

return $link->to_array();
},
$response
Expand Down Expand Up @@ -321,6 +324,11 @@ public function add_smart_link( WP_REST_Request $request ): WP_REST_Response {
// Mark as applied.
$smart_link->set_status( Smart_Link_Status::APPLIED );

// If the context is not set, set it to Smart Linking.
if ( null === $smart_link->get_context() ) {
$smart_link->set_context( $this->get_pch_feature_name() );
}

// The smart link properties are set in the validate callback.
$saved = $smart_link->save();
if ( ! $saved ) {
Expand Down Expand Up @@ -594,6 +602,11 @@ public function validate_smart_link_params( array $params, WP_REST_Request $requ
$smart_link->set_source_post_id( intval( $post_id ) );
}

// If the context is not set, set it to Smart Linking.
if ( null === $smart_link->get_context() ) {
$smart_link->set_context( $this->get_pch_feature_name() );
}

// Set the smart link attribute in the request.
$request->set_param( 'smart_link', $smart_link );

Expand Down
11 changes: 11 additions & 0 deletions src/rest-api/content-helper/class-endpoint-traffic-boost.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,9 @@ public function generate_link_suggestions( WP_REST_Request $request ) {
function ( Inbound_Smart_Link $link ) use ( $save ) {
$link->set_status( Smart_Link_Status::PENDING );

// Set the context to Traffic Boost.
$link->set_context( $this->get_pch_feature_name() );

// If the save flag is set, save the smart link.
if ( $save ) {
$link->save();
Expand Down Expand Up @@ -516,6 +519,9 @@ public function generate_placement_suggestions( WP_REST_Request $request ) {
);
}

// Set the context to Traffic Boost.
$valid_suggestion->set_context( $this->get_pch_feature_name() );

// Check if there's already a smart link with the same source and destination posts.
$existing_smart_link = Inbound_Smart_Link::get_smart_link_by_source_and_destination( $source_post_id, $destination_post->ID );

Expand Down Expand Up @@ -850,6 +856,11 @@ public function validate_smart_link_id( int $smart_link_id, WP_REST_Request $req
);
}

// If the context is not set, set it to Traffic Boost.
if ( null === $inbound_link->get_context() ) {
$inbound_link->set_context( $this->get_pch_feature_name() );
}

// Set the inbound link in the request.
$request->set_param( 'inbound_link', $inbound_link );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,9 @@ public function test_add_smart_link_returns_valid_response(): void {
self::assertObjectHasProperty( 'itm', $data['data']->href );

// Assert that ITM parameters are added to the href.
self::assertStringContainsString( 'itm_campaign=parsely-pch', $data['data']->href->itm );
self::assertStringContainsString( 'itm_source=smart-link', $data['data']->href->itm );
self::assertStringContainsString( 'itm_campaign=wp-parsely', $data['data']->href->itm );
self::assertStringContainsString( 'itm_medium=smart-link', $data['data']->href->itm );
self::assertStringContainsString( 'itm_source=smart-linking', $data['data']->href->itm );
self::assertStringContainsString( 'itm_term=' . $smart_link_data['uid'], $data['data']->href->itm );
}

Expand Down Expand Up @@ -443,8 +444,9 @@ public function test_add_multiple_smart_links_returns_valid_response(): void {
self::assertArrayHasKey( 'itm', $smart_link['href'] );

// Assert that ITM parameters are added to the href.
self::assertStringContainsString( 'itm_campaign=parsely-pch', $smart_link['href']['itm'] );
self::assertStringContainsString( 'itm_source=smart-link', $smart_link['href']['itm'] );
self::assertStringContainsString( 'itm_campaign=wp-parsely', $smart_link['href']['itm'] );
self::assertStringContainsString( 'itm_medium=smart-link', $smart_link['href']['itm'] );
self::assertStringContainsString( 'itm_source=smart-linking', $smart_link['href']['itm'] );
self::assertStringContainsString( 'itm_term=' . $smart_link['uid'], $smart_link['href']['itm'] );
}
}
Expand Down
Loading