| 1 |
<?php |
| 2 |
/** |
| 3 |
* UI: Row actions class. |
| 4 |
* |
| 5 |
* @package Parsely |
| 6 |
* @since 2.6.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare(strict_types=1); |
| 10 |
|
| 11 |
namespace Parsely\UI; |
| 12 |
|
| 13 |
use Parsely\Parsely; |
| 14 |
use Parsely\Dashboard_Link; |
| 15 |
use Parsely\Permissions; |
| 16 |
use WP_Post; |
| 17 |
|
| 18 |
/** |
| 19 |
* Handles the post/page row actions. |
| 20 |
* |
| 21 |
* @since 2.6.0 |
| 22 |
* @since 3.19.0 Added Engagement Boost link. |
| 23 |
*/ |
| 24 |
final class Row_Actions { |
| 25 |
/** |
| 26 |
* Instance of Parsely class. |
| 27 |
* |
| 28 |
* @var Parsely |
| 29 |
*/ |
| 30 |
private $parsely; |
| 31 |
|
| 32 |
/** |
| 33 |
* Constructor. |
| 34 |
* |
| 35 |
* @param Parsely $parsely Instance of Parsely class. |
| 36 |
*/ |
| 37 |
public function __construct( Parsely $parsely ) { |
| 38 |
$this->parsely = $parsely; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Registers action and filter hook callbacks. |
| 43 |
* |
| 44 |
* @since 2.6.0 |
| 45 |
*/ |
| 46 |
public function run(): void { |
| 47 |
/** |
| 48 |
* Filter whether row action links are enabled or not. |
| 49 |
* |
| 50 |
* @since 2.6.0 |
| 51 |
* |
| 52 |
* @param bool $enabled True if enabled, false if not. |
| 53 |
*/ |
| 54 |
if ( apply_filters( 'wp_parsely_enable_row_action_links', true ) ) { |
| 55 |
add_filter( 'post_row_actions', array( $this, 'row_actions_add_parsely_link' ), 10, 2 ); |
| 56 |
add_filter( 'page_row_actions', array( $this, 'row_actions_add_parsely_link' ), 10, 2 ); |
| 57 |
add_filter( 'post_row_actions', array( $this, 'row_actions_add_engagement_boost_link' ), 10, 2 ); |
| 58 |
add_filter( 'page_row_actions', array( $this, 'row_actions_add_engagement_boost_link' ), 10, 2 ); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Includes a link to the statistics page for an article in the wp-admin |
| 64 |
* Posts List. |
| 65 |
* |
| 66 |
* If the post object is the "front page," this will include the main |
| 67 |
* dashboard link instead. |
| 68 |
* |
| 69 |
* @since 2.6.0 |
| 70 |
* |
| 71 |
* @see https://developer.wordpress.org/reference/hooks/page_row_actions/ |
| 72 |
* |
| 73 |
* @param array<string, string> $actions The existing list of actions. |
| 74 |
* @param WP_Post $post The individual post object the actions apply to. |
| 75 |
* @return array<string, string> The amended list of actions. |
| 76 |
*/ |
| 77 |
public function row_actions_add_parsely_link( array $actions, WP_Post $post ): array { |
| 78 |
if ( ! Dashboard_Link::can_show_link( $post, $this->parsely ) ) { |
| 79 |
return $actions; |
| 80 |
} |
| 81 |
|
| 82 |
$url = Dashboard_Link::generate_url( $post, $this->parsely->get_site_id(), 'wp-admin-posts-list', 'wp-admin' ); |
| 83 |
if ( '' !== $url ) { |
| 84 |
$actions['find_in_parsely'] = $this->generate_link_to_parsely( $post, $url ); |
| 85 |
} |
| 86 |
|
| 87 |
return $actions; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Generates the HTML link to Parse.ly. |
| 92 |
* |
| 93 |
* @since 2.6.0 |
| 94 |
* @since 3.1.2 Added `url` parameter. |
| 95 |
* |
| 96 |
* @param WP_Post $post Which post object or ID to add link to. |
| 97 |
* @param string $url Generated URL for the post. |
| 98 |
* @return string The HTML for the link to Parse.ly. |
| 99 |
*/ |
| 100 |
private function generate_link_to_parsely( WP_Post $post, string $url ): string { |
| 101 |
return sprintf( |
| 102 |
'<a href="%1$s" aria-label="%2$s">%3$s</a>', |
| 103 |
esc_url( $url ), |
| 104 |
esc_attr( $this->generate_aria_label_for_post( $post ) ), |
| 105 |
esc_html__( 'Parse.ly Stats', 'wp-parsely' ) |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Generates ARIA label content. |
| 111 |
* |
| 112 |
* @since 2.6.0 |
| 113 |
* |
| 114 |
* @param WP_Post $post Which post object or ID to generate the ARIA label for. |
| 115 |
* @return string ARIA label content. |
| 116 |
*/ |
| 117 |
private function generate_aria_label_for_post( WP_Post $post ): string { |
| 118 |
return sprintf( |
| 119 |
/* translators: Post title */ |
| 120 |
__( 'Go to Parse.ly stats for "%s"', 'wp-parsely' ), |
| 121 |
$post->post_title |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Adds a link to Engagement Boost in the row actions. |
| 127 |
* |
| 128 |
* If the user doesn't have permission to use Engagement Boost, or if the post |
| 129 |
* is a draft, private, future, pending, or trash, the link is not added. |
| 130 |
* |
| 131 |
* @since 3.19.0 |
| 132 |
* |
| 133 |
* @param array<string, string> $actions The existing list of actions. |
| 134 |
* @param WP_Post $post The individual post object the actions apply to. |
| 135 |
* @return array<string, string> The amended list of actions. |
| 136 |
*/ |
| 137 |
public function row_actions_add_engagement_boost_link( array $actions, WP_Post $post ): array { |
| 138 |
if ( ! Permissions::current_user_can_use_pch_feature( |
| 139 |
'traffic_boost', |
| 140 |
$this->parsely->get_options()['content_helper'], |
| 141 |
$post->ID |
| 142 |
) ) { |
| 143 |
return $actions; |
| 144 |
} |
| 145 |
|
| 146 |
// Disable for posts that are draft, private, future, pending, or trash. |
| 147 |
$non_public_statuses = array( 'draft', 'private', 'future', 'pending', 'trash' ); |
| 148 |
if ( in_array( $post->post_status, $non_public_statuses, true ) && |
| 149 |
! Parsely::post_has_trackable_status( $post ) ) { |
| 150 |
return $actions; |
| 151 |
} |
| 152 |
|
| 153 |
// Disable for post types that do not support the REST API, which is |
| 154 |
// required for getting target post data. |
| 155 |
$post_type = get_post_type_object( $post->post_type ); |
| 156 |
if ( null === $post_type || ! $post_type->show_in_rest ) { |
| 157 |
return $actions; |
| 158 |
} |
| 159 |
|
| 160 |
$actions['engagement_boost'] = $this->generate_link_to_engagement_boost( $post ); |
| 161 |
return $actions; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Generates the HTML link to Engagement Boost, with the Parse.ly icon. |
| 166 |
* |
| 167 |
* @since 3.19.0 |
| 168 |
* |
| 169 |
* @param WP_Post $post The individual post object the actions apply to. |
| 170 |
* @return string The HTML for the link to Engagement Boost. |
| 171 |
*/ |
| 172 |
private function generate_link_to_engagement_boost( WP_Post $post ): string { |
| 173 |
$parsely_icon = 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjAiIGhlaWdodD0iMjAiIHZpZXdCb3g9IjAgMCA2MCA2NSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48cGF0aCBmaWxsPSIjMWQyMzI3IiBkPSJNMjMuNzIsNTEuNTNjMC0uMTgsMC0uMzQtLjA2LS41MmExMy4xMSwxMy4xMSwwLDAsMC0yLjEtNS41M0ExNC43NCwxNC43NCwwLDAsMCwxOS4xMiw0M2MtLjI3LS4yMS0uNS0uMTEtLjUxLjIybC0uMjQsMy40MmMwLC4zMy0uMzguMzUtLjQ5LDBsLTEuNS00LjhhMS40LDEuNCwwLDAsMC0uNzctLjc4LDIzLjkxLDIzLjkxLDAsMCwwLTMuMS0uODRjLTEuMzgtLjI0LTMuMzktLjM5LTMuMzktLjM5LS4zNCwwLS40NS4yMS0uMjUuNDlsMi4wNiwzLjc2Yy4yLjI3LDAsLjU0LS4yOS4zM2wtNC41MS0zLjZhMy42OCwzLjY4LDAsMCwwLTIuODYtLjQ4Yy0xLC4xNi0yLjQ0LjQ2LTIuNDQuNDZhLjY4LjY4LDAsMCwwLS4zOS4yNS43My43MywwLDAsMC0uMTQuNDVTLjQxLDQzLC41NCw0NGEzLjYzLDMuNjMsMCwwLDAsMS4yNSwyLjYyTDYuNDgsNTBjLjI4LjIuMDkuNDktLjIzLjM3bC00LjE4LS45NGMtLjMyLS4xMi0uNSwwLS40LjM3LDAsMCwuNjksMS44OSwxLjMxLDMuMTZhMjQsMjQsMCwwLDAsMS42NiwyLjc0LDEuMzQsMS4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zMuMTksMyw1LjkzLDcuOCw3LjQ1LDExLjA4QTkuNiw5LjYsMCwwLDEsMzAsNDkuMDlhOS4zMSw5LjMxLDAsMCwxLDIuODYuNDVjMS41Mi0zLjI4LDQuMjYtOC4xMSw3LjQ0LTExLjA5YTIwLjQ2LDIwLjQ2LDAsMCwwLDUuMDktNywxOSwxOSwwLDAsMCwxLjExLTUuODIiPjwvcGF0aD48cGF0aCBmaWxsPSIjMWQyMzI3IiBkPSJNMzYuMTIsNTguNDRBNi4xMiw2LjEyLDAsMSwxLDMwLDUyLjMyYTYuMTEsNi4xMSwwLDAsMSw2LjEyLDYuMTIiPjwvcGF0aD48L3N2Zz4='; |
| 174 |
$engagement_boost_url = admin_url( 'admin.php?page=parsely-dashboard-page#/engagement-boost/' . $post->ID ); |
| 175 |
|
| 176 |
return sprintf( |
| 177 |
'<a href="%1$s" aria-label="%2$s" style="white-space:nowrap;"> |
| 178 |
<span style="display:inline-flex; align-items:center;"> |
| 179 |
%3$s |
| 180 |
<img src="%4$s" alt="" style="width:14px; height:14px; margin-left:2px; display:inline-block;"> |
| 181 |
</span> |
| 182 |
</a>', |
| 183 |
esc_url( $engagement_boost_url ), |
| 184 |
esc_attr( $this->generate_aria_label_for_post( $post ) ), |
| 185 |
esc_html__( 'Boost Engagement', 'wp-parsely' ), |
| 186 |
esc_attr( $parsely_icon ) |
| 187 |
); |
| 188 |
} |
| 189 |
} |
| 190 |
|