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

121 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 WP_Post;
16
17 /**
18 * Handles 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 * Registers action and filter hook callbacks.
41 *
42 * @since 2.6.0
43 */
44 public function run(): void {
45 /**
46 * Filter whether row action links are enabled or not.
47 *
48 * @since 2.6.0
49 *
50 * @param bool $enabled True if enabled, false if not.
51 */
52 if ( apply_filters( 'wp_parsely_enable_row_action_links', true ) ) {
53 add_filter( 'post_row_actions', array( $this, 'row_actions_add_parsely_link' ), 10, 2 );
54 add_filter( 'page_row_actions', array( $this, 'row_actions_add_parsely_link' ), 10, 2 );
55 }
56 }
57
58 /**
59 * Includes a link to the statistics page for an article in the wp-admin
60 * Posts List.
61 *
62 * If the post object is the "front page," this will include the main
63 * dashboard link instead.
64 *
65 * @since 2.6.0
66 *
67 * @see https://developer.wordpress.org/reference/hooks/page_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 * @return array<string, string> The amended list of actions.
72 */
73 public function row_actions_add_parsely_link( array $actions, WP_Post $post ): array {
74 if ( ! Dashboard_Link::can_show_link( $post, $this->parsely ) ) {
75 return $actions;
76 }
77
78 $url = Dashboard_Link::generate_url( $post, $this->parsely->get_site_id(), 'wp-admin-posts-list', 'wp-admin' );
79 if ( '' !== $url ) {
80 $actions['find_in_parsely'] = $this->generate_link_to_parsely( $post, $url );
81 }
82
83 return $actions;
84 }
85
86 /**
87 * Generates the HTML link to Parse.ly.
88 *
89 * @since 2.6.0
90 * @since 3.1.2 Added `url` parameter.
91 *
92 * @param WP_Post $post Which post object or ID to add link to.
93 * @param string $url Generated URL for the post.
94 * @return string The HTML for the link to Parse.ly.
95 */
96 private function generate_link_to_parsely( WP_Post $post, string $url ): string {
97 return sprintf(
98 '<a href="%1$s" aria-label="%2$s">%3$s</a>',
99 esc_url( $url ),
100 esc_attr( $this->generate_aria_label_for_post( $post ) ),
101 esc_html__( 'Parse.ly&nbsp;Stats', 'wp-parsely' )
102 );
103 }
104
105 /**
106 * Generates ARIA label content.
107 *
108 * @since 2.6.0
109 *
110 * @param WP_Post $post Which post object or ID to generate the ARIA label for.
111 * @return string ARIA label content.
112 */
113 private function generate_aria_label_for_post( WP_Post $post ): string {
114 return sprintf(
115 /* translators: Post title */
116 __( 'Go to Parse.ly stats for "%s"', 'wp-parsely' ),
117 $post->post_title
118 );
119 }
120 }
121