| 1 |
<?php |
| 2 |
/** |
| 3 |
* Parse.ly Dashboard Link utility class. |
| 4 |
* |
| 5 |
* @package Parsely |
| 6 |
* @since 3.1.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare(strict_types=1); |
| 10 |
|
| 11 |
namespace Parsely; |
| 12 |
|
| 13 |
use WP_Post; |
| 14 |
|
| 15 |
/** |
| 16 |
* Utility methods to build and generate dashboard links. |
| 17 |
* |
| 18 |
* @since 3.1.0 |
| 19 |
*/ |
| 20 |
class Dashboard_Link { |
| 21 |
/** |
| 22 |
* Generate the Parse.ly dashboard URL for the post. Returns an empty string if the URL could not be generated. |
| 23 |
* |
| 24 |
* @since 2.6.0 |
| 25 |
* @since 3.1.0 Moved to class-dashboard-link.php. Added source parameter. |
| 26 |
* |
| 27 |
* @param WP_Post $post Which post object or ID to check. |
| 28 |
* @param string $apikey API key or empty string. |
| 29 |
* @param string $campaign Campaign name for the `utm_campaign` URL parameter. |
| 30 |
* @param string $source Source name for the `utm_source` URL parameter. |
| 31 |
* @return string |
| 32 |
*/ |
| 33 |
public static function generate_url( WP_Post $post, string $apikey, string $campaign, string $source ): string { |
| 34 |
$permalink = get_permalink( $post ); |
| 35 |
if ( ! is_string( $permalink ) ) { |
| 36 |
return ''; |
| 37 |
} |
| 38 |
|
| 39 |
$query_args = array( |
| 40 |
'url' => rawurlencode( $permalink ), |
| 41 |
'utm_campaign' => $campaign, |
| 42 |
'utm_source' => $source, |
| 43 |
'utm_medium' => 'wp-parsely', |
| 44 |
); |
| 45 |
|
| 46 |
$base_url = trailingslashit( 'https://dash.parsely.com/' . $apikey ) . 'find'; |
| 47 |
|
| 48 |
return add_query_arg( $query_args, $base_url ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Determine whether Parse.ly dashboard link should be shown or not. |
| 53 |
* |
| 54 |
* @since 2.6.0 |
| 55 |
* @since 3.1.0 Moved to class-dashboard-link.php. Renamed from `cannot_show_parsely_link`. |
| 56 |
* |
| 57 |
* @param WP_Post $post Which post object or ID to check. |
| 58 |
* @param Parsely $parsely Parsely object. |
| 59 |
* @return bool True if the link can be shown, false otherwise. |
| 60 |
*/ |
| 61 |
public static function can_show_link( WP_Post $post, Parsely $parsely ): bool { |
| 62 |
return Parsely::post_has_trackable_status( $post ) && is_post_type_viewable( $post->post_type ) && ! $parsely->api_key_is_missing(); |
| 63 |
} |
| 64 |
} |
| 65 |
|