| @@ -1,10 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Parsely row actions class | |
| 3 | + * UI: Row actions class. | |
| 4 | 4 | * |
| 5 | 5 | * @package Parsely |
| 6 | - * @since 2.6.0 | |
| 6 | + * @since 2.6.0 | |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | declare(strict_types=1); |
| 10 | 10 | |
| @@ -11,14 +11,16 @@ | ||
| 11 | 11 | namespace Parsely\UI; |
| 12 | 12 | |
| 13 | 13 | use Parsely\Parsely; |
| 14 | 14 | use Parsely\Dashboard_Link; |
| 15 | +use Parsely\Permissions; | |
| 15 | 16 | use WP_Post; |
| 16 | 17 | |
| 17 | 18 | /** |
| 18 | - * Handle the post/page row actions. | |
| 19 | + * Handles the post/page row actions. | |
| 19 | 20 | * |
| 20 | 21 | * @since 2.6.0 |
| 22 | + * @since 3.19.0 Added Engagement Boost link. | |
| 21 | 23 | */ |
| 22 | 24 | final class Row_Actions { |
| 23 | 25 | /** |
| 24 | 26 | * Instance of Parsely class. |
| @@ -36,13 +38,11 @@ | ||
| 36 | 38 | $this->parsely = $parsely; |
| 37 | 39 | } |
| 38 | 40 | |
| 39 | 41 | /** |
| 40 | - * Register action and filter hook callbacks. | |
| 42 | + * Registers action and filter hook callbacks. | |
| 41 | 43 | * |
| 42 | 44 | * @since 2.6.0 |
| 43 | - * | |
| 44 | - * @return void | |
| 45 | 45 | */ |
| 46 | 46 | public function run(): void { |
| 47 | 47 | /** |
| 48 | 48 | * Filter whether row action links are enabled or not. |
| @@ -53,24 +53,26 @@ | ||
| 53 | 53 | */ |
| 54 | 54 | if ( apply_filters( 'wp_parsely_enable_row_action_links', true ) ) { |
| 55 | 55 | add_filter( 'post_row_actions', array( $this, 'row_actions_add_parsely_link' ), 10, 2 ); |
| 56 | 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 ); | |
| 57 | 59 | } |
| 58 | 60 | } |
| 59 | 61 | |
| 60 | 62 | /** |
| 61 | - * Include a link to the statistics page for an article in the wp-admin Posts List. | |
| 63 | + * Includes a link to the statistics page for an article in the wp-admin | |
| 64 | + * Posts List. | |
| 62 | 65 | * |
| 63 | - * If the post object is the "front page," this will include the main dashboard link instead. | |
| 66 | + * If the post object is the "front page," this will include the main | |
| 67 | + * dashboard link instead. | |
| 64 | 68 | * |
| 65 | 69 | * @since 2.6.0 |
| 66 | 70 | * |
| 67 | 71 | * @see https://developer.wordpress.org/reference/hooks/page_row_actions/ |
| 68 | - * @see https://developer.wordpress.org/reference/hooks/post_row_actions/ | |
| 69 | 72 | * |
| 70 | 73 | * @param array<string, string> $actions The existing list of actions. |
| 71 | 74 | * @param WP_Post $post The individual post object the actions apply to. |
| 72 | - * | |
| 73 | 75 | * @return array<string, string> The amended list of actions. |
| 74 | 76 | */ |
| 75 | 77 | public function row_actions_add_parsely_link( array $actions, WP_Post $post ): array { |
| 76 | 78 | if ( ! Dashboard_Link::can_show_link( $post, $this->parsely ) ) { |
| @@ -76,9 +78,9 @@ | ||
| 76 | 78 | if ( ! Dashboard_Link::can_show_link( $post, $this->parsely ) ) { |
| 77 | 79 | return $actions; |
| 78 | 80 | } |
| 79 | 81 | |
| 80 | - $url = Dashboard_Link::generate_url( $post, $this->parsely->get_api_key(), 'wp-admin-posts-list', 'wp-admin' ); | |
| 82 | + $url = Dashboard_Link::generate_url( $post, $this->parsely->get_site_id(), 'wp-admin-posts-list', 'wp-admin' ); | |
| 81 | 83 | if ( '' !== $url ) { |
| 82 | 84 | $actions['find_in_parsely'] = $this->generate_link_to_parsely( $post, $url ); |
| 83 | 85 | } |
| 84 | 86 | |
| @@ -85,9 +87,9 @@ | ||
| 85 | 87 | return $actions; |
| 86 | 88 | } |
| 87 | 89 | |
| 88 | 90 | /** |
| 89 | - * Generate the HTML link to Parse.ly. | |
| 91 | + * Generates the HTML link to Parse.ly. | |
| 90 | 92 | * |
| 91 | 93 | * @since 2.6.0 |
| 92 | 94 | * @since 3.1.2 Added `url` parameter. |
| 93 | 95 | * |
| @@ -104,9 +106,9 @@ | ||
| 104 | 106 | ); |
| 105 | 107 | } |
| 106 | 108 | |
| 107 | 109 | /** |
| 108 | - * Generate ARIA label content. | |
| 110 | + * Generates ARIA label content. | |
| 109 | 111 | * |
| 110 | 112 | * @since 2.6.0 |
| 111 | 113 | * |
| 112 | 114 | * @param WP_Post $post Which post object or ID to generate the ARIA label for. |
| @@ -116,7 +118,72 @@ | ||
| 116 | 118 | return sprintf( |
| 117 | 119 | /* translators: Post title */ |
| 118 | 120 | __( 'Go to Parse.ly stats for "%s"', 'wp-parsely' ), |
| 119 | 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,PHN2ZyB3aWR0aD0iMjAiIGhlaWdodD0iMjAiIHZpZXdCb3g9IjAgMCA2MCA2NSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48cGF0aCBmaWxsPSIjMWQyMzI3IiBkPSJNMjMuNzIsNTEuNTNjMC0uMTgsMC0uMzQtLjA2LS41MmExMy4xMSwxMy4xMSwwLDAsMC0yLjEtNS41M0ExNC43NCwxNC43NCwwLDAsMCwxOS4xMiw0M2MtLjI3LS4yMS0uNS0uMTEtLjUxLjIybC0uMjQsMy40MmMwLC4zMy0uMzguMzUtLjQ5LDBsLTEuNS00LjhhMS40LDEuNCwwLDAsMC0uNzctLjc4LDIzLjkxLDIzLjkxLDAsMCwwLTMuMS0uODRjLTEuMzgtLjI0LTMuMzktLjM5LTMuMzktLjM5LS4zNCwwLS40NS4yMS0uMjUuNDlsMi4wNiwzLjc2Yy4yLjI3LDAsLjU0LS4yOS4zM2wtNC41MS0zLjZhMy42OCwzL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TUtLjU4LjA1bC0yLjI5LTUuNjljLS4xNy0uNS0uNTctLjU2LS45MS0uMTRhMzUuNzcsMzUuNzcsMCwwLDAtMyw0LjIsMzUuNTUsMzUuNTUsMCwwLDAtMiw0LjYyLDIsMiwwLDAsMCwuMjcsMS42N2w0LjY3LDYuMjRjLjMzLjQyLjIzLDEtLjIyLjY5bC00Ljg3LTIuNjZjLS40NS0uMjktLjgyLS4xLS44Mi40M2ExOC42LDE4LjYsMCwwLDAsLjgzLDUuMDcsMjAuMTYsMjAuMTYsMCwwLDAsNS4zNyw3Ljc3YzMuMTksMyw1LjkzLDcuOCw3LjQ1LDExLjA4QTkuNiw5LjYsMCwwLDEsMzAsNDkuMDlhOS4zMSw5LjMxLDAsMCwxLDIuODYuNDVjMS41Mi0zLjI4LDQuMjYtOC4xMSw3LjQ0LTExLjA5YTIwLjQ2LDIwLjQ2LDAsMCwwLDUuMDktNywxOSwxOSwwLDAsMCwxLjExLTUuODIiPjwvcGF0aD48cGF0aCBmaWxsPSIjMWQyMzI3IiBkPSJNMzYuMTIsNTguNDRBNi4xMiw2LjEyLDAsMSwxLDMwLDUyLjMyYTYuMTEsNi4xMSwwLDAsMSw2LjEyLDYuMTIiPjwvcGF0aD48L3N2Zz4='; | |
| 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 ) | |
| 120 | 187 | ); |
| 121 | 188 | } |
| 122 | 189 | } |