true]); wp_enqueue_script('acyc_script'.$scriptNumber); wp_add_inline_script('acyc_script'.$scriptNumber, $script); } else { wp_enqueue_script('acyc_script'.$scriptNumber, $script, $deps, '{__VERSION__}', ['in_footer' => true]); } return 'acyc_script'.$scriptNumber; } public static function addStyle($raw, $style, $type = 'text/css', $media = null, $attribs = []) { static $styleNumber = 0; $styleNumber++; if ($raw) { wp_add_inline_style('style'.$styleNumber, $style); } else { wp_enqueue_style('style'.$styleNumber, $style, [], '{__VERSION__}'); } } public static function redirect(string $url, string $msg = '', string $msgType = 'message') { if (Security::isAdmin() && substr($url, 0, 4) != 'http' && substr($url, 0, 4) != 'www.') { $url = Url::addPageParam($url); } if (empty($url)) { $url = Url::rootURI(); } if (headers_sent()) { echo ''; } else { wp_safe_redirect($url); } exit; } /** * @param array $options * - verifySsl: bool, default true * - headers: array for example ['Content-Type' => 'application/json'] * - method: string GET or POST * - data: array */ public static function curlCall(string $url, array $options = []): array { $options['verifySsl'] = $options['verifySsl'] ?? true; $headers = []; if (!empty($options['headers']) && is_array($options['headers'])) { $headers = $options['headers']; } $data = []; if (!empty($options['data']) && is_array($options['data'])) { $data = $options['data']; } $allowedMethods = ['GET', 'POST']; $method = 'GET'; if (!empty($options['method']) && in_array($options['method'], $allowedMethods, true)) { $method = $options['method']; } $dataFormatted = ''; if (!empty($data)) { $isHeaderContentTypeJson = !empty($headers['Content-Type']) && $headers['Content-Type'] === self::CONTENT_TYPE_JSON; if ($method === 'POST' && $isHeaderContentTypeJson) { $dataFormatted = json_encode($data); } else { $dataFormatted = http_build_query($data); } } if ($method === 'GET' && !empty($dataFormatted)) { $url .= strpos($url, '?') === false ? '?'.$dataFormatted : '&'.$dataFormatted; } $args = [ 'method' => $method, 'headers' => $headers, 'sslverify' => $options['verifySsl'], 'timeout' => 30, ]; if ($method === 'POST' && !empty($dataFormatted)) { $args['body'] = $dataFormatted; } $response = wp_remote_request($url, $args); if (is_wp_error($response)) { return [ 'error' => $response->get_error_message(), 'status_code' => 0, ]; } $httpCode = (int)wp_remote_retrieve_response_code($response); $body = wp_remote_retrieve_body($response); $result = json_decode($body, true) ?? []; $result['status_code'] = $httpCode; return $result; } }