session->get( 'wc_notices', array() ); if ( ! empty( $notices['error'] ) ) { $return = false; foreach ( $notices['error'] as $key => $error ) { if ( 'EMPTY_ERROR' !== $error ) { $return = $this->add_error( $return, html_entity_decode( $error ),'appmaker_wc_error', array( 'status' => 405 ) ); } } wc_clear_notices(); return $return; } else { return false; } } /** * @param mixed|WP_Error $return * * @param string $message * @param string $code * @param string $data * * @return WP_Error */ public function add_error( $return, $message = '', $code = 'appmaker_wc_error', $data = '' ) { $message = strip_tags( $message ); $message = html_entity_decode( $message ); if ( is_wp_error( $return ) ) { $return->add( $code, $message, $data ); } else { $return = new WP_Error( $code, $message, $data ); } return $return; } /** * @param string $url * * @return string */ public function ensure_absolute_link( $url ) { if ( ! preg_match( '~^(?:f|ht)tps?://~i', $url ) ) { $url = get_site_url( null, $url ); } if ( substr( $url, 0, 2 ) === '//' ) { $url = 'https:' . $url; } return $url; } /** * @param $function * @param array $params * * @return string */ public function return_data( $function, $params = array() ) { $return = ''; ob_start(); call_user_func_array( $function, $params ); $return = ob_get_clean(); return $return; } /** * Cache response. */ public function cache_response() { if ( apply_filters( 'appmaker_wc_should_send_cache_header', false ) ) { add_filter( 'rest_post_dispatch', array( $this, 'set_cache_header' ), 10, 3 ); } } /** * @param mixed $response Current response, either response or `null` to indicate pass-through. * @param WP_REST_Server $server ResponseHandler instance (usually WP_REST_Server). * @param WP_REST_Request $request The request that was used to make current response. * @return WP_REST_Response Modified response, either response or `null` to indicate pass-through. */ public function set_cache_header( $response, $server, $request ) { $expires_offset = (60 * 5); $server->send_header( 'Cache-Control' , 'max-age=' . $expires_offset ); $server->send_header( 'Expires' , gmdate( 'D, d M Y H:i:s', time() + $expires_offset ) . ' GMT' ); return $response; } }