Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
68.18% |
30 / 44 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| LinkTracker | |
68.18% |
30 / 44 |
|
50.00% |
2 / 4 |
32.89 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| add_hooks | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| enqueue_scripts | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
| build_url | |
93.10% |
27 / 29 |
|
0.00% |
0 / 1 |
16.08 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\LinkTracker; |
| 4 | |
| 5 | use NewfoldLabs\WP\ModuleLoader\Container; |
| 6 | |
| 7 | /** |
| 8 | * Manages all the functionalities for the module. |
| 9 | */ |
| 10 | class LinkTracker { |
| 11 | /** |
| 12 | * Dependency injection container. |
| 13 | * |
| 14 | * @var Container |
| 15 | */ |
| 16 | protected $container; |
| 17 | |
| 18 | /** |
| 19 | * Constructor for the LinkTracker class. |
| 20 | * |
| 21 | * @param Container $container The module container. |
| 22 | */ |
| 23 | public function __construct( Container $container ) { |
| 24 | |
| 25 | $this->container = $container; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Adds hooks for the module. |
| 30 | * |
| 31 | * This method registers the necessary hooks for the link tracker module, |
| 32 | * specifically for enqueuing scripts in the admin area. |
| 33 | */ |
| 34 | public function add_hooks() { |
| 35 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 99 ); |
| 36 | add_filter( 'nfd_build_url', array( $this, 'build_url' ), 10, 2 ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Enqueues the JavaScript file for the link tracker. |
| 41 | * |
| 42 | * This method registers and enqueues the JavaScript file that will handle |
| 43 | * the link tracking functionality on the front end. |
| 44 | */ |
| 45 | public function enqueue_scripts() { |
| 46 | |
| 47 | $asset_file = NFD_LINK_TRACKER_BUILD_DIR . '/index.asset.php'; |
| 48 | if ( is_readable( $asset_file ) ) { |
| 49 | $asset = include_once $asset_file; |
| 50 | } else { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | wp_register_script( |
| 55 | 'wp-module-link-tracker', |
| 56 | NFD_LINK_TRACKER_BUILD_URL . '/index.js', |
| 57 | array_merge( $asset['dependencies'], array( 'nfd-runtime' ) ), |
| 58 | $asset['version'], |
| 59 | true |
| 60 | ); |
| 61 | |
| 62 | wp_enqueue_script( 'wp-module-link-tracker' ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Builds a URL with tracking parameters. |
| 67 | * |
| 68 | * This method constructs a URL with default and provided query parameters, |
| 69 | * ensuring that the URL is properly formatted and includes necessary tracking |
| 70 | * information. |
| 71 | * |
| 72 | * @param string $url The base URL to which parameters will be added. |
| 73 | * @param array $params Optional. Additional parameters to include in the URL. |
| 74 | * Default is an empty array. |
| 75 | * |
| 76 | * @return string The constructed URL with query parameters. |
| 77 | */ |
| 78 | public function build_url( string $url, $params = array() ) { |
| 79 | |
| 80 | $container = $this->container; |
| 81 | |
| 82 | $source = false; |
| 83 | |
| 84 | if ( ! empty( $params['source'] ) ) { |
| 85 | $source = $params['source']; |
| 86 | unset( $params['source'] ); |
| 87 | } |
| 88 | |
| 89 | $parts = wp_parse_url( $url ); |
| 90 | |
| 91 | $query_params = array(); |
| 92 | if ( isset( $parts['query'] ) ) { |
| 93 | parse_str( $parts['query'], $query_params ); |
| 94 | } |
| 95 | |
| 96 | $default_params = array( |
| 97 | 'channelid' => strpos( $url, 'wp-admin' ) !== false ? 'P99C100S1N0B3003A151D115E0000V111' : 'P99C100S1N0B3003A151D115E0000V112', |
| 98 | 'utm_medium' => $container ? $container->plugin()->get( 'id', 'bluehost' ) . '_plugin' : 'bluehost_plugin', |
| 99 | 'utm_source' => ( $_SERVER['PHP_SELF'] ?? '' ) . ( $source ? '?' . $source : false ), |
| 100 | ); |
| 101 | |
| 102 | foreach ( $default_params as $key => $value ) { |
| 103 | if ( ! array_key_exists( $key, $query_params ) ) { |
| 104 | $query_params[ $key ] = $value; |
| 105 | } |
| 106 | } |
| 107 | // Merge the default parameters with the provided parameters and clean the empty parameters. |
| 108 | $query_params = array_filter( |
| 109 | ! empty( $params ) ? array_merge( $query_params, $params ) : $query_params, |
| 110 | function ( $value ) { |
| 111 | return null !== $value && '' !== $value && false !== $value; |
| 112 | } |
| 113 | ); |
| 114 | // Build the final URL with the query parameters. |
| 115 | $base = ( isset( $parts['scheme'] ) ? $parts['scheme'] . '://' : '' ) . |
| 116 | ( isset( $parts['host'] ) ? $parts['host'] : '' ) . |
| 117 | ( isset( $parts['port'] ) ? ':' . $parts['port'] : '' ) . |
| 118 | ( isset( $parts['path'] ) ? $parts['path'] : '' ); |
| 119 | |
| 120 | // If the original URL has a fragment, append it to the final URL. |
| 121 | $fragment = ( isset( $parts['fragment'] ) ? '#' . $parts['fragment'] : '' ); |
| 122 | |
| 123 | return $base . '?' . http_build_query( $query_params, '', '&' ) . $fragment; |
| 124 | } |
| 125 | } |