Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Jetpack MC Stats package. |
||
| 4 | * |
||
| 5 | * @package automattic/jetpack-mc-stats |
||
| 6 | */ |
||
| 7 | |||
| 8 | namespace Automattic\Jetpack; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Class MC Stats, used to record stats using https://pixel.wp.com/g.gif |
||
| 12 | */ |
||
| 13 | class A8c_Mc_Stats { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Holds the stats to be processed |
||
| 17 | * |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | private $stats = array(); |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Indicates whether to use the transparent pixel (b.gif) instead of the regular smiley (g.gif) |
||
| 24 | * |
||
| 25 | * @var boolean |
||
| 26 | */ |
||
| 27 | public $use_transparent_pixel = true; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Class Constructor |
||
| 31 | * |
||
| 32 | * @param boolean $use_transparent_pixel Use the transparent pixel instead of the smiley. |
||
| 33 | */ |
||
| 34 | public function __construct( $use_transparent_pixel = true ) { |
||
| 35 | $this->use_transparent_pixel = $use_transparent_pixel; |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Store a stat for later output. |
||
| 40 | * |
||
| 41 | * @param string $group The stat group. |
||
| 42 | * @param string $name The stat name to bump. |
||
| 43 | * |
||
| 44 | * @return boolean true if stat successfully added |
||
| 45 | */ |
||
| 46 | public function add( $group, $name ) { |
||
| 47 | |||
| 48 | if ( ! \is_string( $group ) || ! \is_string( $name ) ) { |
||
| 49 | return false; |
||
| 50 | } |
||
| 51 | |||
| 52 | if ( ! isset( $this->stats[ $group ] ) ) { |
||
| 53 | $this->stats[ $group ] = array(); |
||
| 54 | } |
||
| 55 | |||
| 56 | if ( \in_array( $name, $this->stats[ $group ], true ) ) { |
||
| 57 | return false; |
||
| 58 | } |
||
| 59 | |||
| 60 | $this->stats[ $group ][] = $name; |
||
| 61 | |||
| 62 | return true; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Gets current stats stored to be processed |
||
| 67 | * |
||
| 68 | * @return array $stats |
||
| 69 | */ |
||
| 70 | public function get_current_stats() { |
||
| 71 | return $this->stats; |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Return the stats from a group in an array ready to be added as parameters in a query string |
||
| 76 | * |
||
| 77 | * @param string $group_name The name of the group to retrieve. |
||
| 78 | * @return array Array with one item, where the key is the prefixed group and the value are all stats concatenated with a comma. If group not found, an empty array will be returned |
||
| 79 | */ |
||
| 80 | public function get_group_query_args( $group_name ) { |
||
| 81 | $stats = $this->get_current_stats(); |
||
| 82 | if ( isset( $stats[ $group_name ] ) && ! empty( $stats[ $group_name ] ) ) { |
||
| 83 | return array( "x_jetpack-{$group_name}" => implode( ',', $stats[ $group_name ] ) ); |
||
| 84 | } |
||
| 85 | return array(); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Gets a list of trac URLs for every stored URL |
||
| 90 | * |
||
| 91 | * @return array An array of URLs |
||
| 92 | */ |
||
| 93 | public function get_stats_urls() { |
||
| 94 | |||
| 95 | $urls = array(); |
||
| 96 | |||
| 97 | foreach ( $this->get_current_stats() as $group => $stat ) { |
||
| 98 | $group_query_string = $this->get_group_query_args( $group ); |
||
| 99 | $urls[] = $this->build_stats_url( $group_query_string ); |
||
| 100 | } |
||
| 101 | |||
| 102 | return $urls; |
||
| 103 | |||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Outputs the tracking pixels for the current stats and empty the stored stats from the object |
||
| 108 | * |
||
| 109 | * @return void |
||
| 110 | */ |
||
| 111 | public function do_stats() { |
||
| 112 | $urls = $this->get_stats_urls(); |
||
| 113 | foreach ( $urls as $url ) { |
||
| 114 | echo '<img src="' . esc_url( $url ) . '" width="1" height="1" style="display:none;" />'; |
||
| 115 | } |
||
| 116 | $this->stats = array(); |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Pings the stats server for the current stats and empty the stored stats from the object |
||
| 121 | * |
||
| 122 | * @return void |
||
| 123 | */ |
||
| 124 | public function do_server_side_stats() { |
||
| 125 | $urls = $this->get_stats_urls(); |
||
| 126 | foreach ( $urls as $url ) { |
||
| 127 | $this->do_server_side_stat( $url ); |
||
| 128 | } |
||
| 129 | $this->stats = array(); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Runs stats code for a one-off, server-side. |
||
| 134 | * |
||
| 135 | * @param string $url string The URL to be pinged. Should include `x_jetpack-{$group}={$stats}` or whatever we want to store. |
||
| 136 | * |
||
| 137 | * @return bool If it worked. |
||
| 138 | */ |
||
| 139 | public function do_server_side_stat( $url ) { |
||
| 140 | $response = wp_remote_get( esc_url_raw( $url ) ); |
||
| 141 | if ( is_wp_error( $response ) ) { |
||
| 142 | return false; |
||
| 143 | } |
||
| 144 | |||
| 145 | if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { |
||
| 146 | return false; |
||
| 147 | } |
||
| 148 | |||
| 149 | return true; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Builds the stats url. |
||
| 154 | * |
||
| 155 | * @param array $args array|string The arguments to append to the URL. |
||
| 156 | * |
||
| 157 | * @return string The URL to be pinged. |
||
| 158 | */ |
||
| 159 | public function build_stats_url( $args ) { |
||
| 160 | $defaults = array( |
||
| 161 | 'v' => 'wpcom2', |
||
| 162 | 'rand' => md5( wp_rand( 0, 999 ) . time() ), |
||
| 163 | ); |
||
| 164 | $args = wp_parse_args( $args, $defaults ); |
||
|
0 ignored issues
–
show
|
|||
| 165 | $gifname = true === $this->use_transparent_pixel ? 'b.gif' : 'g.gif'; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Filter the URL used as the Stats tracking pixel. |
||
| 169 | * |
||
| 170 | * @since 2.3.2 |
||
| 171 | * |
||
| 172 | * @param string $url Base URL used as the Stats tracking pixel. |
||
| 173 | */ |
||
| 174 | $base_url = apply_filters( |
||
| 175 | 'jetpack_stats_base_url', |
||
| 176 | 'https://pixel.wp.com/' . $gifname |
||
| 177 | ); |
||
| 178 | $url = add_query_arg( $args, $base_url ); |
||
| 179 | return $url; |
||
| 180 | } |
||
| 181 | |||
| 182 | } |
||
| 183 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: