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 | * Base class for Jetpack's debugging tests. |
||
| 4 | * |
||
| 5 | * @package Jetpack. |
||
| 6 | */ |
||
| 7 | |||
| 8 | use Automattic\Jetpack\Status; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Jetpack Connection Testing |
||
| 12 | * |
||
| 13 | * Framework for various "unit tests" against the Jetpack connection. |
||
| 14 | * |
||
| 15 | * Individual tests should be added to the class-jetpack-cxn-tests.php file. |
||
| 16 | * |
||
| 17 | * @author Brandon Kraft |
||
| 18 | * @package Jetpack |
||
| 19 | */ |
||
| 20 | |||
| 21 | /** |
||
| 22 | * "Unit Tests" for the Jetpack connection. |
||
| 23 | * |
||
| 24 | * @since 7.1.0 |
||
| 25 | */ |
||
| 26 | class Jetpack_Cxn_Test_Base { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Tests to run on the Jetpack connection. |
||
| 30 | * |
||
| 31 | * @var array $tests |
||
| 32 | */ |
||
| 33 | protected $tests = array(); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Results of the Jetpack connection tests. |
||
| 37 | * |
||
| 38 | * @var array $results |
||
| 39 | */ |
||
| 40 | protected $results = array(); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Status of the testing suite. |
||
| 44 | * |
||
| 45 | * Used internally to determine if a test should be skipped since the tests are already failing. Assume passing. |
||
| 46 | * |
||
| 47 | * @var bool $pass |
||
| 48 | */ |
||
| 49 | protected $pass = true; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Jetpack_Cxn_Test constructor. |
||
| 53 | */ |
||
| 54 | public function __construct() { |
||
| 55 | $this->tests = array(); |
||
| 56 | $this->results = array(); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Adds a new test to the Jetpack Connection Testing suite. |
||
| 61 | * |
||
| 62 | * @since 7.1.0 |
||
| 63 | * @since 7.3.0 Adds name parameter and returns WP_Error on failure. |
||
| 64 | * |
||
| 65 | * @param callable $callable Test to add to queue. |
||
| 66 | * @param string $name Unique name for the test. |
||
| 67 | * @param string $type Optional. Core Site Health type: 'direct' if test can be run during initial load or 'async' if test should run async. |
||
| 68 | * @param array $groups Optional. Testing groups to add test to. |
||
| 69 | * |
||
| 70 | * @return mixed True if successfully added. WP_Error on failure. |
||
| 71 | */ |
||
| 72 | public function add_test( $callable, $name, $type = 'direct', $groups = array( 'default' ) ) { |
||
| 73 | if ( is_array( $name ) ) { |
||
| 74 | // Pre-7.3.0 method passed the $groups parameter here. |
||
| 75 | return new WP_Error( __( 'add_test arguments changed in 7.3.0. Please reference inline documentation.', 'jetpack' ) ); |
||
| 76 | } |
||
| 77 | if ( array_key_exists( $name, $this->tests ) ) { |
||
| 78 | return new WP_Error( __( 'Test names must be unique.', 'jetpack' ) ); |
||
| 79 | } |
||
| 80 | if ( ! is_callable( $callable ) ) { |
||
| 81 | return new WP_Error( __( 'Tests must be valid PHP callables.', 'jetpack' ) ); |
||
| 82 | } |
||
| 83 | |||
| 84 | $this->tests[ $name ] = array( |
||
| 85 | 'name' => $name, |
||
| 86 | 'test' => $callable, |
||
| 87 | 'group' => $groups, |
||
| 88 | 'type' => $type, |
||
| 89 | ); |
||
| 90 | return true; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Lists all tests to run. |
||
| 95 | * |
||
| 96 | * @since 7.3.0 |
||
| 97 | * |
||
| 98 | * @param string $type Optional. Core Site Health type: 'direct' or 'async'. All by default. |
||
| 99 | * @param string $group Optional. A specific testing group. All by default. |
||
| 100 | * |
||
| 101 | * @return array $tests Array of tests with test information. |
||
| 102 | */ |
||
| 103 | public function list_tests( $type = 'all', $group = 'all' ) { |
||
| 104 | if ( ! ( 'all' === $type || 'direct' === $type || 'async' === $type ) ) { |
||
| 105 | _doing_it_wrong( 'Jetpack_Cxn_Test_Base->list_tests', 'Type must be all, direct, or async', '7.3.0' ); |
||
| 106 | } |
||
| 107 | |||
| 108 | $tests = array(); |
||
| 109 | foreach ( $this->tests as $name => $value ) { |
||
| 110 | // Get all valid tests by group staged. |
||
| 111 | if ( 'all' === $group || $group === $value['group'] ) { |
||
| 112 | $tests[ $name ] = $value; |
||
| 113 | } |
||
| 114 | |||
| 115 | // Next filter out any that do not match the type. |
||
| 116 | if ( 'all' !== $type && $type !== $value['type'] ) { |
||
| 117 | unset( $tests[ $name ] ); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | return $tests; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Run a specific test. |
||
| 126 | * |
||
| 127 | * @since 7.3.0 |
||
| 128 | * |
||
| 129 | * @param string $name Name of test. |
||
| 130 | * |
||
| 131 | * @return mixed $result Test result array or WP_Error if invalid name. { |
||
| 132 | * @type string $name Test name |
||
| 133 | * @type mixed $pass True if passed, false if failed, 'skipped' if skipped. |
||
| 134 | * @type string $message Human-readable test result message. |
||
| 135 | * @type string $resolution Human-readable resolution steps. |
||
| 136 | * } |
||
| 137 | */ |
||
| 138 | public function run_test( $name ) { |
||
| 139 | if ( array_key_exists( $name, $this->tests ) ) { |
||
| 140 | return call_user_func( $this->tests[ $name ]['test'] ); |
||
| 141 | } |
||
| 142 | return new WP_Error( __( 'There is no test by that name: ', 'jetpack' ) . $name ); |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Runs the Jetpack connection suite. |
||
| 147 | */ |
||
| 148 | public function run_tests() { |
||
| 149 | foreach ( $this->tests as $test ) { |
||
| 150 | $result = call_user_func( $test['test'] ); |
||
| 151 | $result['group'] = $test['group']; |
||
| 152 | $result['type'] = $test['type']; |
||
| 153 | $this->results[] = $result; |
||
| 154 | if ( false === $result['pass'] ) { |
||
| 155 | $this->pass = false; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Returns the full results array. |
||
| 162 | * |
||
| 163 | * @since 7.1.0 |
||
| 164 | * @since 7.3.0 Add 'type' |
||
| 165 | * |
||
| 166 | * @param string $type Test type, async or direct. |
||
| 167 | * @param string $group Testing group whose results we want. Defaults to all tests. |
||
| 168 | * @return array Array of test results. |
||
| 169 | */ |
||
| 170 | public function raw_results( $type = 'all', $group = 'all' ) { |
||
| 171 | if ( ! $this->results ) { |
||
| 172 | $this->run_tests(); |
||
| 173 | } |
||
| 174 | |||
| 175 | $results = $this->results; |
||
| 176 | |||
| 177 | if ( 'all' !== $group ) { |
||
| 178 | foreach ( $results as $test => $result ) { |
||
| 179 | if ( ! in_array( $group, $result['group'], true ) ) { |
||
| 180 | unset( $results[ $test ] ); |
||
| 181 | } |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | if ( 'all' !== $type ) { |
||
| 186 | foreach ( $results as $test => $result ) { |
||
| 187 | if ( $type !== $result['type'] ) { |
||
| 188 | unset( $results[ $test ] ); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | return $results; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Returns the status of the connection suite. |
||
| 198 | * |
||
| 199 | * @since 7.1.0 |
||
| 200 | * @since 7.3.0 Add 'type' |
||
| 201 | * |
||
| 202 | * @param string $type Test type, async or direct. Optional, direct all tests. |
||
| 203 | * @param string $group Testing group to check status of. Optional, default all tests. |
||
| 204 | * |
||
| 205 | * @return true|array True if all tests pass. Array of failed tests. |
||
| 206 | */ |
||
| 207 | public function pass( $type = 'all', $group = 'all' ) { |
||
| 208 | $results = $this->raw_results( $type, $group ); |
||
| 209 | |||
| 210 | foreach ( $results as $result ) { |
||
| 211 | // 'pass' could be true, false, or 'skipped'. We only want false. |
||
| 212 | if ( isset( $result['pass'] ) && false === $result['pass'] ) { |
||
| 213 | return false; |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | return true; |
||
| 218 | |||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Return array of failed test messages. |
||
| 223 | * |
||
| 224 | * @since 7.1.0 |
||
| 225 | * @since 7.3.0 Add 'type' |
||
| 226 | * |
||
| 227 | * @param string $type Test type, direct or async. |
||
| 228 | * @param string $group Testing group whose failures we want. Defaults to "all". |
||
| 229 | * |
||
| 230 | * @return false|array False if no failed tests. Otherwise, array of failed tests. |
||
| 231 | */ |
||
| 232 | public function list_fails( $type = 'all', $group = 'all' ) { |
||
| 233 | $results = $this->raw_results( $type, $group ); |
||
| 234 | |||
| 235 | foreach ( $results as $test => $result ) { |
||
| 236 | // We do not want tests that passed or ones that are misconfigured (no pass status or no failure message). |
||
| 237 | if ( ! isset( $result['pass'] ) || false !== $result['pass'] || ! isset( $result['message'] ) ) { |
||
| 238 | unset( $results[ $test ] ); |
||
| 239 | } |
||
| 240 | } |
||
| 241 | |||
| 242 | return $results; |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Helper function to return consistent responses for a passing test. |
||
| 247 | * Possible Args: |
||
| 248 | * - name: string The raw method name that runs the test. Default 'unnamed_test'. |
||
| 249 | * - label: bool|string If false, tests will be labeled with their `name`. You can pass a string to override this behavior. Default false. |
||
| 250 | * - pass: bool|string True if the test passed. Default true. |
||
| 251 | * - short_description: bool|string A brief, non-html description that will appear in CLI results. Default 'Test passed!'. |
||
| 252 | * - long_description: bool|string An html description that will appear in the site health page. Default false. |
||
| 253 | * - severity: bool|string 'critical', 'recommended', or 'good'. Default: false. |
||
| 254 | * - action: bool|string A URL for the recommended action. Default: false |
||
| 255 | * - action_label: bool|string The label for the recommended action. Default: false |
||
| 256 | * - show_in_site_health: bool True if the test should be shown on the Site Health page. Default: true |
||
| 257 | * |
||
| 258 | * @param array $args Arguments to override defaults. |
||
| 259 | * |
||
| 260 | * @return array Test results. |
||
| 261 | */ |
||
| 262 | View Code Duplication | public static function passing_test( $args ) { |
|
| 263 | return wp_parse_args( |
||
| 264 | $args, |
||
| 265 | array( |
||
|
0 ignored issues
–
show
|
|||
| 266 | 'name' => 'unnamed_test', |
||
| 267 | 'label' => false, |
||
| 268 | 'pass' => true, |
||
| 269 | 'short_description' => __( 'Test passed!', 'jetpack' ), |
||
| 270 | 'long_description' => false, |
||
| 271 | 'severity' => false, |
||
| 272 | 'action' => false, |
||
| 273 | 'action_label' => false, |
||
| 274 | 'show_in_site_health' => true, |
||
| 275 | ) |
||
| 276 | ); |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Helper function to return consistent responses for a skipped test. |
||
| 281 | * Possible Args: |
||
| 282 | * - name: string The raw method name that runs the test. Default unnamed_test. |
||
| 283 | * - label: bool|string If false, tests will be labeled with their `name`. You can pass a string to override this behavior. Default false. |
||
| 284 | * - pass: bool|string True if the test passed. Default 'skipped'. |
||
| 285 | * - short_description: bool|string A brief, non-html description that will appear in CLI results, and as headings in admin UIs. Default false. |
||
| 286 | * - long_description: bool|string An html description that will appear in the site health page. Default false. |
||
| 287 | * - severity: bool|string 'critical', 'recommended', or 'good'. Default: false. |
||
| 288 | * - action: bool|string A URL for the recommended action. Default: false |
||
| 289 | * - action_label: bool|string The label for the recommended action. Default: false |
||
| 290 | * - show_in_site_health: bool True if the test should be shown on the Site Health page. Default: true |
||
| 291 | * |
||
| 292 | * @param array $args Arguments to override defaults. |
||
| 293 | * |
||
| 294 | * @return array Test results. |
||
| 295 | */ |
||
| 296 | View Code Duplication | public static function skipped_test( $args = array() ) { |
|
| 297 | return wp_parse_args( |
||
| 298 | $args, |
||
| 299 | array( |
||
|
0 ignored issues
–
show
array('name' => 'unnamed...n_site_health' => true) is of type array<string,string|bool...ite_health":"boolean"}>, but the function expects a string.
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: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
| 300 | 'name' => 'unnamed_test', |
||
| 301 | 'label' => false, |
||
| 302 | 'pass' => 'skipped', |
||
| 303 | 'short_description' => false, |
||
| 304 | 'long_description' => false, |
||
| 305 | 'severity' => false, |
||
| 306 | 'action' => false, |
||
| 307 | 'action_label' => false, |
||
| 308 | 'show_in_site_health' => true, |
||
| 309 | ) |
||
| 310 | ); |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Helper function to return consistent responses for a failing test. |
||
| 315 | * Possible Args: |
||
| 316 | * - name: string The raw method name that runs the test. Default unnamed_test. |
||
| 317 | * - label: bool|string If false, tests will be labeled with their `name`. You can pass a string to override this behavior. Default false. |
||
| 318 | * - pass: bool|string True if the test passed. Default false. |
||
| 319 | * - short_description: bool|string A brief, non-html description that will appear in CLI results, and as headings in admin UIs. Default 'Test failed!'. |
||
| 320 | * - long_description: bool|string An html description that will appear in the site health page. Default false. |
||
| 321 | * - severity: bool|string 'critical', 'recommended', or 'good'. Default: 'critical'. |
||
| 322 | * - action: bool|string A URL for the recommended action. Default: false. |
||
| 323 | * - action_label: bool|string The label for the recommended action. Default: false. |
||
| 324 | * - show_in_site_health: bool True if the test should be shown on the Site Health page. Default: true |
||
| 325 | * |
||
| 326 | * @since 7.1.0 |
||
| 327 | * |
||
| 328 | * @param array $args Arguments to override defaults. |
||
| 329 | * |
||
| 330 | * @return array Test results. |
||
| 331 | */ |
||
| 332 | View Code Duplication | public static function failing_test( $args ) { |
|
| 333 | return wp_parse_args( |
||
| 334 | $args, |
||
| 335 | array( |
||
|
0 ignored issues
–
show
array('name' => 'unnamed...n_site_health' => true) is of type array<string,?,{"name":"...ite_health":"boolean"}>, but the function expects a string.
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: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
| 336 | 'name' => 'unnamed_test', |
||
| 337 | 'label' => false, |
||
| 338 | 'pass' => false, |
||
| 339 | 'short_description' => __( 'Test failed!', 'jetpack' ), |
||
| 340 | 'long_description' => false, |
||
| 341 | 'severity' => 'critical', |
||
| 342 | 'action' => false, |
||
| 343 | 'action_label' => false, |
||
| 344 | 'show_in_site_health' => true, |
||
| 345 | ) |
||
| 346 | ); |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Provide WP_CLI friendly testing results. |
||
| 351 | * |
||
| 352 | * @since 7.1.0 |
||
| 353 | * @since 7.3.0 Add 'type' |
||
| 354 | * |
||
| 355 | * @param string $type Test type, direct or async. |
||
| 356 | * @param string $group Testing group whose results we are outputting. Default all tests. |
||
| 357 | */ |
||
| 358 | public function output_results_for_cli( $type = 'all', $group = 'all' ) { |
||
| 359 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
||
| 360 | if ( ( new Status() )->is_development_mode() ) { |
||
| 361 | WP_CLI::line( __( 'Jetpack is in Development Mode:', 'jetpack' ) ); |
||
| 362 | WP_CLI::line( Jetpack::development_mode_trigger_text() ); |
||
| 363 | } |
||
| 364 | WP_CLI::line( __( 'TEST RESULTS:', 'jetpack' ) ); |
||
| 365 | foreach ( $this->raw_results( $group ) as $test ) { |
||
| 366 | if ( true === $test['pass'] ) { |
||
| 367 | WP_CLI::log( WP_CLI::colorize( '%gPassed:%n ' . $test['name'] ) ); |
||
| 368 | } elseif ( 'skipped' === $test['pass'] ) { |
||
| 369 | WP_CLI::log( WP_CLI::colorize( '%ySkipped:%n ' . $test['name'] ) ); |
||
| 370 | if ( $test['short_description'] ) { |
||
| 371 | WP_CLI::log( ' ' . $test['short_description'] ); // Number of spaces to "tab indent" the reason. |
||
| 372 | } |
||
| 373 | } else { // Failed. |
||
| 374 | WP_CLI::log( WP_CLI::colorize( '%rFailed:%n ' . $test['name'] ) ); |
||
| 375 | WP_CLI::log( ' ' . $test['short_description'] ); // Number of spaces to "tab indent" the reason. |
||
| 376 | } |
||
| 377 | } |
||
| 378 | } |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Output results of failures in format expected by Core's Site Health tool for async tests. |
||
| 383 | * |
||
| 384 | * Specifically not asking for a testing group since we're opinionated that Site Heath should see all. |
||
| 385 | * |
||
| 386 | * @since 7.3.0 |
||
| 387 | * |
||
| 388 | * @return array Array of test results |
||
| 389 | */ |
||
| 390 | public function output_results_for_core_async_site_health() { |
||
| 391 | $result = array( |
||
| 392 | 'label' => __( 'Jetpack passed all async tests.', 'jetpack' ), |
||
| 393 | 'status' => 'good', |
||
| 394 | 'badge' => array( |
||
| 395 | 'label' => __( 'Jetpack', 'jetpack' ), |
||
| 396 | 'color' => 'green', |
||
| 397 | ), |
||
| 398 | 'description' => sprintf( |
||
| 399 | '<p>%s</p>', |
||
| 400 | __( "Jetpack's async local testing suite passed all tests!", 'jetpack' ) |
||
| 401 | ), |
||
| 402 | 'actions' => '', |
||
| 403 | 'test' => 'jetpack_debugger_local_testing_suite_core', |
||
| 404 | ); |
||
| 405 | |||
| 406 | if ( $this->pass() ) { |
||
| 407 | return $result; |
||
| 408 | } |
||
| 409 | |||
| 410 | $fails = $this->list_fails( 'async' ); |
||
| 411 | $error = false; |
||
| 412 | foreach ( $fails as $fail ) { |
||
| 413 | if ( ! $error ) { |
||
| 414 | $error = true; |
||
| 415 | $result['label'] = $fail['message']; |
||
| 416 | $result['status'] = $fail['severity']; |
||
| 417 | $result['description'] = sprintf( |
||
| 418 | '<p>%s</p>', |
||
| 419 | $fail['resolution'] |
||
| 420 | ); |
||
| 421 | View Code Duplication | if ( ! empty( $fail['action'] ) ) { |
|
| 422 | $result['actions'] = sprintf( |
||
| 423 | '<a class="button button-primary" href="%1$s" target="_blank" rel="noopener noreferrer">%2$s <span class="screen-reader-text">%3$s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a>', |
||
| 424 | esc_url( $fail['action'] ), |
||
| 425 | __( 'Resolve', 'jetpack' ), |
||
| 426 | /* translators: accessibility text */ |
||
| 427 | __( '(opens in a new tab)', 'jetpack' ) |
||
| 428 | ); |
||
| 429 | } |
||
| 430 | } else { |
||
| 431 | $result['description'] .= sprintf( |
||
| 432 | '<p>%s</p>', |
||
| 433 | __( 'There was another problem:', 'jetpack' ) |
||
| 434 | ) . ' ' . $fail['message'] . ': ' . $fail['resolution']; |
||
| 435 | if ( 'critical' === $fail['severity'] ) { // In case the initial failure is only "recommended". |
||
| 436 | $result['status'] = 'critical'; |
||
| 437 | } |
||
| 438 | } |
||
| 439 | } |
||
| 440 | |||
| 441 | return $result; |
||
| 442 | |||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Provide single WP Error instance of all failures. |
||
| 447 | * |
||
| 448 | * @since 7.1.0 |
||
| 449 | * @since 7.3.0 Add 'type' |
||
| 450 | * |
||
| 451 | * @param string $type Test type, direct or async. |
||
| 452 | * @param string $group Testing group whose failures we want converted. Default all tests. |
||
| 453 | * |
||
| 454 | * @return WP_Error|false WP_Error with all failed tests or false if there were no failures. |
||
| 455 | */ |
||
| 456 | public function output_fails_as_wp_error( $type = 'all', $group = 'all' ) { |
||
| 457 | if ( $this->pass( $group ) ) { |
||
| 458 | return false; |
||
| 459 | } |
||
| 460 | $fails = $this->list_fails( $type, $group ); |
||
| 461 | $error = false; |
||
| 462 | |||
| 463 | foreach ( $fails as $result ) { |
||
| 464 | $code = 'failed_' . $result['name']; |
||
| 465 | $message = $result['short_description']; |
||
| 466 | $data = array( |
||
| 467 | 'resolution' => $result['action'] ? |
||
| 468 | $result['action_label'] . ' :' . $result['action'] : |
||
| 469 | '', |
||
| 470 | ); |
||
| 471 | if ( ! $error ) { |
||
| 472 | $error = new WP_Error( $code, $message, $data ); |
||
| 473 | } else { |
||
| 474 | $error->add( $code, $message, $data ); |
||
| 475 | } |
||
| 476 | } |
||
| 477 | |||
| 478 | return $error; |
||
| 479 | } |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Encrypt data for sending to WordPress.com. |
||
| 483 | * |
||
| 484 | * @todo When PHP minimum is 5.3+, add cipher detection to use an agreed better cipher than RC4. RC4 should be the last resort. |
||
| 485 | * |
||
| 486 | * @param string $data Data to encrypt with the WP.com Public Key. |
||
| 487 | * |
||
| 488 | * @return false|array False if functionality not available. Array of encrypted data, encryption key. |
||
| 489 | */ |
||
| 490 | public function encrypt_string_for_wpcom( $data ) { |
||
| 491 | $return = false; |
||
| 492 | if ( ! function_exists( 'openssl_get_publickey' ) || ! function_exists( 'openssl_seal' ) ) { |
||
| 493 | return $return; |
||
| 494 | } |
||
| 495 | |||
| 496 | $public_key = openssl_get_publickey( JETPACK__DEBUGGER_PUBLIC_KEY ); |
||
| 497 | |||
| 498 | if ( $public_key && openssl_seal( $data, $encrypted_data, $env_key, array( $public_key ) ) ) { |
||
| 499 | // We are returning base64-encoded values to ensure they're characters we can use in JSON responses without issue. |
||
| 500 | $return = array( |
||
| 501 | 'data' => base64_encode( $encrypted_data ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
||
| 502 | 'key' => base64_encode( $env_key[0] ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
||
| 503 | 'cipher' => 'RC4', // When Jetpack's minimum WP version is at PHP 5.3+, we will add in detecting and using a stronger one. |
||
| 504 | ); |
||
| 505 | } |
||
| 506 | |||
| 507 | openssl_free_key( $public_key ); |
||
| 508 | |||
| 509 | return $return; |
||
| 510 | } |
||
| 511 | } |
||
| 512 |
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: