| 1 |
<?php |
| 2 |
/** |
| 3 |
* 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 |
use Parsely\Parsely; |
| 15 |
|
| 16 |
/** |
| 17 |
* Utility methods to build and generate dashboard links. |
| 18 |
* |
| 19 |
* @since 3.1.0 |
| 20 |
*/ |
| 21 |
class Dashboard_Link { |
| 22 |
/** |
| 23 |
* Generates the Parse.ly dashboard URL for the post. Returns an empty |
| 24 |
* string if the URL could not be generated. |
| 25 |
* |
| 26 |
* @since 2.6.0 |
| 27 |
* @since 3.1.0 Moved to class-dashboard-link.php. Added source parameter. |
| 28 |
* @since 3.16.1 Made the $campaign and $source parameters optional. |
| 29 |
* |
| 30 |
* @param WP_Post $post Which post object or ID to check. |
| 31 |
* @param string $site_id Site ID or empty string. |
| 32 |
* @param string $campaign Campaign name for the `utm_campaign` URL parameter. |
| 33 |
* @param string $source Source name for the `utm_source` URL parameter. |
| 34 |
* @return string |
| 35 |
*/ |
| 36 |
public static function generate_url( WP_Post $post, string $site_id, string $campaign = '', string $source = '' ): string { |
| 37 |
/** |
| 38 |
* Internal variable. |
| 39 |
* |
| 40 |
* @var string|false |
| 41 |
*/ |
| 42 |
$permalink = get_permalink( $post ); |
| 43 |
if ( ! is_string( $permalink ) ) { |
| 44 |
return ''; |
| 45 |
} |
| 46 |
|
| 47 |
$query_args = array( |
| 48 |
'url' => rawurlencode( $permalink ), |
| 49 |
'utm_campaign' => $campaign, |
| 50 |
'utm_source' => $source, |
| 51 |
'utm_medium' => 'wp-parsely', |
| 52 |
); |
| 53 |
|
| 54 |
if ( '' === $campaign ) { |
| 55 |
unset( $query_args['utm_campaign'] ); |
| 56 |
} |
| 57 |
|
| 58 |
if ( '' === $source ) { |
| 59 |
unset( $query_args['utm_source'] ); |
| 60 |
} |
| 61 |
|
| 62 |
if ( ! isset( $query_args['utm_campaign'] ) && ! isset( $query_args['utm_source'] ) ) { |
| 63 |
unset( $query_args['utm_medium'] ); |
| 64 |
} |
| 65 |
|
| 66 |
return add_query_arg( $query_args, Parsely::get_dash_url( $site_id ) ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Determines whether Parse.ly dashboard link should be shown or not. |
| 71 |
* |
| 72 |
* @since 2.6.0 |
| 73 |
* @since 3.1.0 Moved to class-dashboard-link.php. Renamed from `cannot_show_parsely_link`. |
| 74 |
* |
| 75 |
* @param WP_Post $post Which post object or ID to check. |
| 76 |
* @param Parsely $parsely Parsely object. |
| 77 |
* @return bool True if the link can be shown, false otherwise. |
| 78 |
*/ |
| 79 |
public static function can_show_link( WP_Post $post, Parsely $parsely ): bool { |
| 80 |
return Parsely::post_has_trackable_status( $post ) && is_post_type_viewable( $post->post_type ) && ! $parsely->site_id_is_missing(); |
| 81 |
} |
| 82 |
} |
| 83 |
|