'Bearer ' . $access_token, 'Content-Type' => 'application/json', ); $tinyurl_payload = array( 'url' => $home_url, 'domain' => $domain, ); $tinyurl_payload_encode = wp_json_encode( $tinyurl_payload ); $tinyurl_request = wp_remote_post( 'https://api.tinyurl.com/create', array( 'method' => 'POST', 'headers' => $headers, 'body' => $tinyurl_payload_encode, ) ); $response_code = wp_remote_retrieve_response_code( $tinyurl_request ); $tinyurl_body = wp_remote_retrieve_body( $tinyurl_request ); $tinyurl_decode = json_decode( $tinyurl_body ); if ( 200 === (int) $response_code ) { $tinyurl = $tinyurl_decode->data->tiny_url; if ( empty( $get_url ) || $tinyurl !== $get_url ) { update_post_meta( $item_id, 'tinyurl_link', $tinyurl ); $response = array( 'response_code' => $response_code, 'message' => 'OK', ); return $response; } $response = array( 'response_code' => $response_code, 'message' => 'Exists', ); return $response; } elseif ( 400 === (int) $response_code ) { $response = array( 'response_code' => $response_code, 'message' => 'Bad Request', ); return $response; } elseif ( 403 === (int) $response_code ) { $response = array( 'response_code' => $response_code, 'message' => 'FORBIDDEN', ); return $response; } elseif ( 401 === (int) $response_code ) { update_option( 'tinyurl_access_token_options', '' ); $response = array( 'response_code' => $response_code, 'message' => $tinyurl_decode->errors[0], ); return $response; } else { $response = array( 'response_code' => $response_code, 'message' => $tinyurl_decode->errors[0], ); return $response; } } else { $tinyurl_request = wp_remote_post( 'https://tinyurl.com/api-create.php?url=' . $home_url ); $response_code = wp_remote_retrieve_response_code( $tinyurl_request ); $tinyurl = wp_remote_retrieve_body( $tinyurl_request ); if ( 200 === (int) $response_code ) { if ( empty( $get_url ) || $tinyurl !== $get_url ) { update_post_meta( $item_id, 'tinyurl_link', $tinyurl ); $response = array( 'response_code' => $response_code, 'message' => 'OK', ); return $response; } $response = array( 'response_code' => $response_code, 'message' => 'Exists', ); return $response; } else { $response = array( 'response_code' => $response_code, 'message' => $tinyurl, ); return $response; } } } /** * Validates tinyurl. * * @param int $item item_id of product. * @return string */ public function cklph_wdl_check_tinyurl( $item ) { $tinyurl_domain = get_option( 'tinyurl_domain' ); $access_token = get_option( 'tinyurl_access_token_options' ); if ( ! empty( $tinyurl_domain ) && empty( $access_token ) ) { return '401-tinyurl'; } $tinyurl_response = $this->cklph_wdl_get_tinyurl( $item ); $tinyurl_response_code = $tinyurl_response['response_code']; $tinyurl_response_body = $tinyurl_response['message']; if ( 422 === (int) $tinyurl_response_code ) { return '422-tinyurl'; } switch ( $tinyurl_response_body ) { case 'OK': return 201; case 'Exists': return 200; default: $i18n_code = empty( $tinyurl_response_code ) ? '0' : $tinyurl_response_code; $i18n_body = empty( $tinyurl_response_body ) ? 'Unknown Error.' : $tinyurl_response_body; /* * Translators: %1$d: tinyurl response code. * Translators: %2$s: tinyurl error message. */ return sprintf( __( 'An Error has occured. Error code: %1$d : %2$s', 'woocommerce-direct-links' ), intval( $i18n_code ), esc_attr( $i18n_body ) ); } } /** * Returns the headers based from the parameters * * @param string $host The host API. * @param string $access_token The authorization token from the API. * @param string $content_type Specifiy the media tpye of the resource. * @return array */ protected function cklph_wdl_get_api_headers( $host, $access_token, $content_type ) { return array( 'Host' => $host, 'Authorization' => 'Bearer ' . $access_token, 'Content-Type' => $content_type, ); } /** * Http request GET the group_guid for bitly url. * * @return @group_guid | @guid_response */ protected function cklph_wdl_group_guid() { $access_token = get_option( 'bitly_access_token_options' ); $headers = $this->cklph_wdl_get_api_headers( 'api-ssl.bitly.com', $access_token, 'application/json' ); $guid_request = wp_remote_get( 'https://api-ssl.bitly.com/v4/groups', array( 'timeout' => 0, 'headers' => $headers, ) ); $guid_response_code = wp_remote_retrieve_response_code( $guid_request ); $guid_body = wp_remote_retrieve_body( $guid_request ); $guid_json = json_decode( $guid_body ); if ( 200 !== (int) $guid_response_code ) { $guid_response = array( 'response_code' => $guid_response_code, 'message' => $guid_json->message, ); return $guid_response; } $guid_response = array( 'response_code' => $guid_response_code, 'message' => $guid_json->groups[0]->guid, ); return $guid_response; } /** * Converts a url into a bitly link * * @param int $item_id item_id_id of product. * @return @response return the error response and message if error occured. */ protected function cklph_wdl_get_bitly( $item_id ) { $access_token = get_option( 'bitly_access_token_options' ); $headers = $this->cklph_wdl_get_api_headers( 'api-ssl.bitly.com', $access_token, 'application/json' ); $long_url = home_url( 'checkout/?add-to-cart=' . intval( $item_id ) . '&quantity=1' ); $bitly_payload = array( 'long_url' => $long_url, 'group_guid' => $this->cklph_wdl_group_guid()['message'], ); $bitly_payload_encode = wp_json_encode( $bitly_payload ); $bitly_request = wp_remote_post( 'https://api-ssl.bitly.com/v4/shorten', array( 'method' => 'POST', 'headers' => $headers, 'body' => $bitly_payload_encode, ) ); $response_code = wp_remote_retrieve_response_code( $bitly_request ); $bitly_body = wp_remote_retrieve_body( $bitly_request ); $bitly_decode = json_decode( $bitly_body ); if ( 201 < (int) $response_code ) { $response = array( 'response_code' => $response_code, 'message' => $bitly_decode->message, ); return $response; } if ( 200 === (int) $response_code || 201 === (int) $response_code ) { $get_url = get_post_meta( $item_id, 'bitly_link', true ); if ( empty( $get_url ) || $get_url !== $bitly_decode->link ) { update_post_meta( $item_id, 'bitly_link', $bitly_decode->link ); $response = array( 'response_code' => $response_code, 'message' => 'OK', ); return $response; } $response = array( 'response_code' => $response_code, 'message' => 'Exists', ); return $response; } } /** * Validates the bitly API Key. * * @param int $item item_id of product. * @return string */ public function cklph_wdl_check_bitly( $item ) { $access_token = get_option( 'bitly_access_token_options' ); if ( empty( $access_token ) ) { return '401-bitly'; } $group_guid_response = $this->cklph_wdl_group_guid(); $guid_response_code = $group_guid_response['response_code']; $guid_response_body = $group_guid_response['message']; if ( 200 !== (int) $guid_response_code ) { $i18n_code = empty( $guid_response_code ) ? '0' : $guid_response_code; $i18n_body = empty( $guid_response_body ) ? 'Unknown error.' : $guid_response_body; /* * Translators: %1$d: bitly response code. * Translators: %2$s: bitly error message. */ return sprintf( __( 'An Error has occured while validating API key for Bitly. Error code: %1$d : %2$s', 'woocommerce-direct-links' ), intval( $i18n_code ), esc_attr( $i18n_body ) ); } $bitly_response = $this->cklph_wdl_get_bitly( $item ); $bitly_response_code = $bitly_response['response_code']; $bitly_response_body = $bitly_response['message']; switch ( $bitly_response_body ) { case 'OK': return 201; case 'Exists': return 200; default: $i18n_code = empty( $bitly_response_code ) ? '0' : $bitly_response_code; $i18n_body = empty( $bitly_response_body ) ? 'Unknown Error.' : $bitly_response_body; /* * Translators: %1$d: bitly response code. * Translators: %2$s: bitly error message. */ return sprintf( __( 'An Error has occured while validating API key for Bitly. Error code: %1$d : %2$s', 'woocommerce-direct-links' ), intval( $i18n_code ), esc_attr( $i18n_body ) ); } } /** * Converts a url into joturl. * * @param int $item_id item_id of product. * @return $response if error occur. */ protected function cklph_wdl_get_joturl( $item_id ) { $get_url = get_post_meta( $item_id, 'joturl_link', true ); if ( empty( $get_url ) ) { $joturl_private = get_option( 'joturl_private' ); $joturl_public = get_option( 'joturl_public' ); $home_url = home_url( 'checkout/?add-to-cart=' . intval( $item_id ) . '&quantity=1' ); $joturl_request = wp_remote_post( 'https://api.joturl.com/a/v1/shorten?login=' . $joturl_public . '&key=' . $joturl_private . '&url=' . $home_url . '' ); $response_code = wp_remote_retrieve_response_code( $joturl_request ); $joturl_body = wp_remote_retrieve_body( $joturl_request ); $joturl_decode = json_decode( $joturl_body ); if ( 200 === (int) $response_code ) { $joturl = $joturl_decode->result->short_url; update_post_meta( $item_id, 'joturl_link', $joturl ); $response = array( 'response_code' => $response_code, 'message' => 'OK', ); return $response; } else { $response = array( 'response_code' => $response_code, 'message' => $joturl_decode->status->text, ); return $response; } } $response = array( 'response_code' => 409, 'message' => 'Exists', ); return $response; } /** * Validates the JotURL API Keys. * * @param int $item_id item_id of product. * @return string */ public function cklph_wdl_check_joturl( $item_id ) { $joturl_private_key = get_option( 'joturl_private' ); $joturl_public_key = get_option( 'joturl_public' ); if ( empty( $joturl_private_key ) || empty( $joturl_public_key ) ) { return '401-joturl'; } $joturl_response = $this->cklph_wdl_get_joturl( $item_id ); $joturl_response_code = $joturl_response['response_code']; $joturl_response_body = $joturl_response['message']; switch ( $joturl_response_body ) { case 'OK': return 201; case 'Exists': return 200; default: $i18n_code = empty( $joturl_response_code ) ? '0' : $joturl_response_code; $i18n_body = empty( $joturl_response_body ) ? 'Unknown Error.' : $joturl_response_body; /* * Translators: %1$d: joturl response code. * Translators: %2$s: joturl error message. */ return sprintf( __( 'An Error has occured while validating API keys for JotURL. Error code: %1$d : %2$s', 'woocommerce-direct-links' ), intval( $i18n_code ), esc_attr( $i18n_body ) ); } } /** * Force convert a url into joturl. * * @param int $item_id item_id of product. * @return $response if error occur. */ protected function cklph_wdl_force_get_joturl( $item_id ) { $joturl_private = get_option( 'joturl_private' ); $joturl_public = get_option( 'joturl_public' ); $home_url = home_url( 'checkout/?add-to-cart=' . intval( $item_id ) . '&quantity=1' ); $joturl_request = wp_remote_post( 'https://api.joturl.com/a/v1/shorten?login=' . $joturl_public . '&key=' . $joturl_private . '&url=' . $home_url . '' ); $response_code = wp_remote_retrieve_response_code( $joturl_request ); $joturl_body = wp_remote_retrieve_body( $joturl_request ); $joturl_decode = json_decode( $joturl_body ); if ( 200 === (int) $response_code ) { $joturl = $joturl_decode->result->short_url; update_post_meta( $item_id, 'joturl_link', $joturl ); $response = array( 'response_code' => $response_code, 'message' => 'OK', ); return $response; } else { $response = array( 'response_code' => $response_code, 'message' => $joturl_decode->status->text, ); return $response; } } /** * Validates the JotURL API Keys when JotURL is forced to refresh link. * * @param int $item_id item_id of product. * @return string */ public function cklph_wdl_check_force_joturl( $item_id ) { $joturl_private_key = get_option( 'joturl_private' ); $joturl_public_key = get_option( 'joturl_public' ); if ( empty( $joturl_private_key ) || empty( $joturl_public_key ) ) { return '401-joturl'; } $joturl_response = $this->cklph_wdl_force_get_joturl( $item_id ); $joturl_response_code = $joturl_response['response_code']; $joturl_response_body = $joturl_response['message']; switch ( $joturl_response_body ) { case 'OK': return 201; case 'Exists': return 200; default: $i18n_code = empty( $joturl_response_code ) ? '0' : $joturl_response_code; $i18n_body = empty( $joturl_response_body ) ? 'Unknown Error.' : $joturl_response_body; /* * Translators: %1$d: joturl response code. * Translators: %2$s: joturl error message. */ return sprintf( __( 'An Error has occured while validating API keys for JotURL. Error code: %1$d : %2$s', 'woocommerce-direct-links' ), intval( $i18n_code ), esc_attr( $i18n_body ) ); } } /** * Converts checkout links to YoURLS. * * @param int $item_id id of product. * @return @response if error occur. */ protected function cklph_wdl_get_yourls( $item_id ) { $get_url = get_post_meta( $item_id, 'yourls_link', true ); $home_url = home_url( 'checkout/?add-to-cart=' . intval( $item_id ) . '&quantity=1' ); $signature = get_option( 'yourls_signature' ); $domain = get_option( 'yourls_domain' ); $yourls_request = wp_remote_post( 'http://' . $domain . '/yourls-api.php?format=json&action=shorturl&signature=' . $signature . '&url=' . $home_url . '' ); $response_code = wp_remote_retrieve_response_code( $yourls_request ); $yourls_body = wp_remote_retrieve_body( $yourls_request ); $yourls_decode = json_decode( $yourls_body ); switch ( $response_code ) { case 200: if ( empty( $get_url ) || $yourls_decode->shorturl !== $get_url ) { update_post_meta( $item_id, 'yourls_link', $yourls_decode->shorturl ); $response = array( 'response_code' => $response_code, 'message' => 'OK', ); return $response; } $response = array( 'response_code' => $response_code, 'message' => 'Exists', ); return $response; case 400: if ( empty( $get_url ) || $yourls_decode->shorturl !== $get_url ) { update_post_meta( $item_id, 'yourls_link', $yourls_decode->shorturl ); $response = array( 'response_code' => $response_code, 'message' => 'OK', ); return $response; } $response = array( 'response_code' => $response_code, 'message' => 'Exists', ); return $response; case 0: $response = array( 'response_code' => 404, 'message' => 'Site not found', ); return $response; case 403: $response = array( 'response_code' => 403, 'message' => 'Please use a valid YoURLS Signature', ); return $response; default: $response = array( 'response_code' => $response_code, 'message' => $yourls_decode->message, ); return $response; } } /** * Validates the YoURLS Signature. * * @param int $item item_id of product. * @return string */ public function cklph_wdl_check_yourls( $item ) { $yourls_signature = get_option( 'yourls_signature' ); $yourls_domain = get_option( 'yourls_domain' ); if ( empty( $yourls_signature ) || empty( $yourls_domain ) ) { return '401-yourls'; } $yourls_response = $this->cklph_wdl_get_yourls( $item ); $yourls_response_code = $yourls_response['response_code']; $yourls_response_body = $yourls_response['message']; switch ( $yourls_response_body ) { case 'OK': return 201; case 'Exists': return 200; default: $i18n_code = empty( $yourls_response_code ) ? '0' : $yourls_response_code; $i18n_body = empty( $yourls_response_body ) ? 'Unknown Error.' : $yourls_response_body; /* * Translators: %1$d: YoURLS response code. * Translators: %2$s: YoURLS error message. */ return sprintf( __( 'An Error has occured while validating YoURLS credentials. Error code: %1$d : %2$s', 'woocommerce-direct-links' ), intval( $i18n_code ), esc_attr( $i18n_body ) ); } } /** * Get shortlink to use in list tables. * * @param int $item_id ID of product. * @return @turl shortlink */ public function cklph_wdl_get_link( $item_id ) { $url_option = get_option( 'url_options' ); $home_url = esc_url( home_url( 'checkout/?add-to-cart=' . intval( $item_id ) . '&quantity=1' ) ); switch ( $url_option ) { case 'tinyurl': $get_url = get_post_meta( $item_id, 'tinyurl_link', true ); $url = empty( $get_url ) ? $home_url : $get_url; break; case 'bitly': $get_url = get_post_meta( $item_id, 'bitly_link', true ); $url = empty( $get_url ) ? $home_url : $get_url; break; case 'yourls': $get_url = get_post_meta( $item_id, 'yourls_link', true ); $url = empty( $get_url ) ? $home_url : $get_url; break; case 'joturl': $get_url = get_post_meta( $item_id, 'joturl_link', true ); $url = empty( $get_url ) ? $home_url : $get_url; break; case 'none': default: $url = $home_url; } return $url; } } }