PluginProbe
Parse.ly / 3.0.0
Parse.ly v3.0.0
3.24.1 3.24.0 3.23.7 3.23.6 3.23.5 3.23.4 3.23.3 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.17.0 3.18.0 3.18.1 3.19.0 3.19.1 3.19.2 3.19.3 3.2.0 3.2.1 3.20.0 3.20.1 3.20.2 3.20.3 All 105 releases
wp-parsely / src / UI / class-row-actions.php

class-row-actions.php in Parse.ly 3.0.0, at src/UI/class-row-actions.php

153 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Parsely 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 WP_Post;
15
16 /**
17 * Handle the post/page row actions.
18 *
19 * @since 2.6.0
20 */
21 final class Row_Actions {
22 /**
23 * Instance of Parsely class.
24 *
25 * @var Parsely
26 */
27 private $parsely;
28
29 /**
30 * Constructor.
31 *
32 * @param Parsely $parsely Instance of Parsely class.
33 */
34 public function __construct( Parsely $parsely ) {
35 $this->parsely = $parsely;
36 }
37
38 /**
39 * Register action and filter hook callbacks.
40 *
41 * @since 2.6.0
42 *
43 * @return void
44 */
45 public function run(): void {
46 /**
47 * Filter whether row action links are enabled or not.
48 *
49 * @since 2.6.0
50 *
51 * @param bool $enabled True if enabled, false if not.
52 */
53 if ( apply_filters( 'wp_parsely_enable_row_action_links', true ) ) {
54 add_filter( 'post_row_actions', array( $this, 'row_actions_add_parsely_link' ), 10, 2 );
55 add_filter( 'page_row_actions', array( $this, 'row_actions_add_parsely_link' ), 10, 2 );
56 }
57 }
58
59 /**
60 * Include a link to the statistics page for an article in the wp-admin Posts List.
61 *
62 * If the post object is the "front page," this will include the main dashboard link instead.
63 *
64 * @since 2.6.0
65 *
66 * @see https://developer.wordpress.org/reference/hooks/page_row_actions/
67 * @see https://developer.wordpress.org/reference/hooks/post_row_actions/
68 *
69 * @param array<string, string> $actions The existing list of actions.
70 * @param WP_Post $post The individual post object the actions apply to.
71 *
72 * @return array<string, string> The amended list of actions.
73 */
74 public function row_actions_add_parsely_link( array $actions, WP_Post $post ): array {
75 if ( $this->cannot_show_parsely_link( $post ) ) {
76 return $actions;
77 }
78
79 $actions['find_in_parsely'] = $this->generate_link_to_parsely( $post );
80
81 return $actions;
82 }
83
84 /**
85 * Determine whether Parse.ly row action link should be shown or not.
86 *
87 * @since 2.6.0
88 *
89 * @param WP_Post $post Which post object or ID to check.
90 * @return bool True if the link cannot be shown, false if the link can be shown.
91 */
92 private function cannot_show_parsely_link( WP_Post $post ): bool {
93 return ! Parsely::post_has_trackable_status( $post ) ||
94 ! is_post_type_viewable( $post->post_type ) ||
95 $this->parsely->api_key_is_missing();
96 }
97
98 /**
99 * Generate the HTML link to Parse.ly.
100 *
101 * @since 2.6.0
102 *
103 * @param WP_Post $post Which post object or ID to add link to.
104 * @return string The HTML for the link to Parse.ly.
105 */
106 private function generate_link_to_parsely( WP_Post $post ): string {
107 return sprintf(
108 '<a href="%1$s" aria-label="%2$s">%3$s</a>',
109 esc_url( $this->generate_url( $post, $this->parsely->get_api_key() ) ),
110 esc_attr( $this->generate_aria_label_for_post( $post ) ),
111 esc_html__( 'Parse.ly&nbsp;Stats', 'wp-parsely' )
112 );
113 }
114
115 /**
116 * Generate the URL for the link.
117 *
118 * @since 2.6.0
119 *
120 * @param WP_Post $post Which post object or ID to check.
121 * @param string $apikey API key or empty string.
122 * @return string
123 */
124 private function generate_url( WP_Post $post, string $apikey ): string {
125 $query_args = array(
126 'url' => rawurlencode( get_permalink( $post ) ),
127 'utm_campaign' => 'wp-admin-posts-list',
128 'utm_medium' => 'wp-parsely',
129 'utm_source' => 'wp-admin',
130 );
131
132 $base_url = trailingslashit( 'https://dash.parsely.com/' . $apikey ) . 'find';
133
134 return add_query_arg( $query_args, $base_url );
135 }
136
137 /**
138 * Generate ARIA label content.
139 *
140 * @since 2.6.0
141 *
142 * @param WP_Post $post Which post object or ID to generate the ARIA label for.
143 * @return string ARIA label content.
144 */
145 private function generate_aria_label_for_post( WP_Post $post ): string {
146 return sprintf(
147 /* translators: Post title */
148 __( 'Go to Parse.ly stats for "%s"', 'wp-parsely' ),
149 $post->post_title
150 );
151 }
152 }
153