PluginProbe
Parse.ly / 3.2.1
Parse.ly v3.2.1
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.2.1, at src/UI/class-row-actions.php

123 lines 3.0 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 Parsely\Dashboard_Link;
15 use WP_Post;
16
17 /**
18 * Handle the post/page row actions.
19 *
20 * @since 2.6.0
21 */
22 final class Row_Actions {
23 /**
24 * Instance of Parsely class.
25 *
26 * @var Parsely
27 */
28 private $parsely;
29
30 /**
31 * Constructor.
32 *
33 * @param Parsely $parsely Instance of Parsely class.
34 */
35 public function __construct( Parsely $parsely ) {
36 $this->parsely = $parsely;
37 }
38
39 /**
40 * Register action and filter hook callbacks.
41 *
42 * @since 2.6.0
43 *
44 * @return void
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 }
58 }
59
60 /**
61 * Include a link to the statistics page for an article in the wp-admin Posts List.
62 *
63 * If the post object is the "front page," this will include the main dashboard link instead.
64 *
65 * @since 2.6.0
66 *
67 * @see https://developer.wordpress.org/reference/hooks/page_row_actions/
68 * @see https://developer.wordpress.org/reference/hooks/post_row_actions/
69 *
70 * @param array<string, string> $actions The existing list of actions.
71 * @param WP_Post $post The individual post object the actions apply to.
72 *
73 * @return array<string, string> The amended list of actions.
74 */
75 public function row_actions_add_parsely_link( array $actions, WP_Post $post ): array {
76 if ( ! Dashboard_Link::can_show_link( $post, $this->parsely ) ) {
77 return $actions;
78 }
79
80 $url = Dashboard_Link::generate_url( $post, $this->parsely->get_api_key(), 'wp-admin-posts-list', 'wp-admin' );
81 if ( '' !== $url ) {
82 $actions['find_in_parsely'] = $this->generate_link_to_parsely( $post, $url );
83 }
84
85 return $actions;
86 }
87
88 /**
89 * Generate the HTML link to Parse.ly.
90 *
91 * @since 2.6.0
92 * @since 3.1.2 Added `url` parameter.
93 *
94 * @param WP_Post $post Which post object or ID to add link to.
95 * @param string $url Generated URL for the post.
96 * @return string The HTML for the link to Parse.ly.
97 */
98 private function generate_link_to_parsely( WP_Post $post, string $url ): string {
99 return sprintf(
100 '<a href="%1$s" aria-label="%2$s">%3$s</a>',
101 esc_url( $url ),
102 esc_attr( $this->generate_aria_label_for_post( $post ) ),
103 esc_html__( 'Parse.ly&nbsp;Stats', 'wp-parsely' )
104 );
105 }
106
107 /**
108 * Generate ARIA label content.
109 *
110 * @since 2.6.0
111 *
112 * @param WP_Post $post Which post object or ID to generate the ARIA label for.
113 * @return string ARIA label content.
114 */
115 private function generate_aria_label_for_post( WP_Post $post ): string {
116 return sprintf(
117 /* translators: Post title */
118 __( 'Go to Parse.ly stats for "%s"', 'wp-parsely' ),
119 $post->post_title
120 );
121 }
122 }
123