$method, 'parameter' => $parameter, ); } return $id; } private static function parameter_to_string( $parameter ) { $return = ''; if ( is_array( $parameter ) ) { foreach ( $parameter as $key => $value ) { $return .= '&' . $key . '=' . $value; } } return $return; } public function perform( $id ) { if ( self::$settings->get_global_option( 'cache' ) ) { $cached = get_transient( 'wp-piwik_c_' . md5( self::$is_cacheable[ $id ] ) ); if ( ! empty( $cached ) && ! ( ! empty( $cached['result'] ) && 'error' === $cached['result'] ) ) { self::$wp_piwik->log( 'Deliver cached data: ' . $id ); return $cached; } } self::$wp_piwik->log( 'Perform request: ' . $id ); if ( ! isset( self::$requests[ $id ] ) ) { return array( 'result' => 'error', 'message' => 'Request ' . $id . ' was not registered.', ); } elseif ( ! isset( self::$results[ $id ] ) ) { $this->request( $id ); } if ( isset( self::$results[ $id ] ) ) { if ( self::$settings->get_global_option( 'cache' ) && self::$is_cacheable[ $id ] ) { set_transient( 'wp-piwik_c_' . md5( self::$is_cacheable[ $id ] ), self::$results[ $id ], WEEK_IN_SECONDS ); } return self::$results[ $id ]; } else { return false; } } public function get_debug( $id ) { return isset( self::$debug[ $id ] ) ? self::$debug[ $id ] : false; } protected function build_url( $config ) { return http_build_query( $this->get_url_params( $config ), '', '&' ); } protected function get_url_params( $config ) { $params = [ 'method' => $config['method'], 'idSite' => self::$settings->get_option( 'site_id' ), ]; $params = array_merge( $params, $config['parameter'] ); return $params; } protected function unserialize( $str ) { self::$wp_piwik->log( 'Result string: ' . $str ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged return ( json_decode( '', true ) === $str || false !== @json_decode( $str, true ) ) ? json_decode( $str, true ) : array(); } public static function get_last_error() { return self::$last_error; } abstract protected function request( $id ); }