| 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 |
* @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 |
* Generates 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 Stats', 'wp-parsely' ) |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Generates 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 |
|