api_key = $api_key; $this->api_secret = $api_secret; $this->debug = $debug; $this->log = new ConvertKit_Log(); } /** * Gets account information from the API. * * @since 1.9.6 * * @return WP_Error|array */ public function account() { $this->log( 'API: account()' ); return $this->get( 'account', array( 'api_secret' => $this->api_secret, ) ); } /** * Gets all subscription forms from the API. * * @since 1.9.6 * * @return WP_Error|array */ public function get_subscription_forms() { $this->log( 'API: get_subscription_forms()' ); // Send request. return $this->get( 'subscription_forms', array( 'api_key' => $this->api_key, ) ); } /** * Gets all forms from the API. * * @since 1.9.6 * * @return WP_Error|array */ public function get_forms() { $this->log( 'API: get_forms()' ); // Get all forms and landing pages from the API. $forms = $this->get_forms_landing_pages(); // If an error occured, log and return it now. if ( is_wp_error( $forms ) ) { $this->log( 'API: get_forms(): Error: ' . $forms->get_error_message() ); return $forms; } return $forms['forms']; } /** * Subscribes an email address to a form. * * @since 1.9.6 * * @param int $form_id Form ID. * @param string $email Email Address. * @param string $first_name First Name. * @param mixed $fields Custom Fields (false|array). * @return WP_Error|array */ public function form_subscribe( $form_id, $email, $first_name = '', $fields = false ) { // Backward compat. if $email is an array comprising of email and name keys. if ( is_array( $email ) ) { // @phpstan-ignore-line. _deprecated_function( __FUNCTION__, '1.9.6', 'form_subscribe( $form_id, $email, $first_name )' ); $first_name = $email['name']; $email = $email['email']; } $this->log( 'API: form_subscribe(): [ form_id: ' . $form_id . ', email: ' . $email . ', first_name: ' . $first_name . ' ]' ); // Sanitize some parameters. $form_id = absint( $form_id ); $email = trim( $email ); $first_name = trim( $first_name ); // Return error if no Form ID or email address is specified. if ( empty( $form_id ) ) { return new WP_Error( 'convertkit_api_error', __( 'form_subscribe(): the form_id parameter is empty.', 'convertkit' ) ); } if ( empty( $email ) ) { return new WP_Error( 'convertkit_api_error', __( 'form_subscribe(): the email parameter is empty.', 'convertkit' ) ); } // Build request parameters. $params = array( 'api_key' => $this->api_key, 'email' => $email, 'first_name' => $first_name, ); if ( $fields ) { $params['fields'] = $fields; } // Send request. $response = $this->post( 'forms/' . $form_id . '/subscribe', $params ); // If an error occured, log and return it now. if ( is_wp_error( $response ) ) { $this->log( 'API: form_subscribe(): Error: ' . $response->get_error_message() ); return $response; } /** * Runs actions immediately after the email address was successfully subscribed to the form. * * @since 1.9.6 * * @param array $response API Response * @param int $form_id Form ID * @param string $email Email Address * @param string $first_name First Name * @param mixed $fields Custom Fields (false|array) */ do_action( 'convertkit_api_form_subscribe_success', $response, $form_id, $email, $first_name, $fields ); return $response; } /** * Gets all landing pages from the API. * * @since 1.9.6 * * @return WP_Error|array */ public function get_landing_pages() { $this->log( 'API: get_landing_pages()' ); // Get all forms and landing pages from the API. $forms = $this->get_forms_landing_pages(); // If an error occured, log and return it now. if ( is_wp_error( $forms ) ) { $this->log( 'API: get_landing_pages(): Error: ' . $forms->get_error_message() ); return $forms; } return $forms['landing_pages']; } /** * Fetches all sequences from the API. * * @since 1.9.6 * * @return WP_Error|array */ public function get_sequences() { $this->log( 'API: get_sequences()' ); $sequences = array(); // Send request. $response = $this->get( 'sequences', array( 'api_key' => $this->api_key, ) ); // If an error occured, log and return it now. if ( is_wp_error( $response ) ) { $this->log( 'API: get_sequences(): Error: ' . $response->get_error_message() ); return $response; } // If no sequences exist, return WP_Error. if ( ! isset( $response['courses'] ) ) { $this->log( 'API: get_sequences(): Error: No sequences exist in ConvertKit.' ); return new WP_Error( 'convertkit_api_error', __( 'No sequences exist in ConvertKit. Visit your ConvertKit account and create your first sequence.', 'convertkit' ) ); } if ( ! count( $response['courses'] ) ) { $this->log( 'API: get_sequences(): Error: No sequences exist in ConvertKit.' ); return new WP_Error( 'convertkit_api_error', __( 'No sequences exist in ConvertKit. Visit your ConvertKit account and create your first sequence.', 'convertkit' ) ); } foreach ( $response['courses'] as $sequence ) { $sequences[] = $sequence; } return $sequences; } /** * Subscribes an email address to a sequence. * * @since 1.9.6 * * @param string $sequence_id Sequence ID. * @param string $email Email Address. * @param string $first_name First Name. * @param mixed $fields Custom Fields (false|array). * @return WP_Error|array */ public function sequence_subscribe( $sequence_id, $email, $first_name = '', $fields = false ) { $this->log( 'API: sequence_subscribe(): [ sequence_id: ' . $sequence_id . ', email: ' . $email . ']' ); // Sanitize some parameters. $sequence_id = trim( $sequence_id ); $email = trim( $email ); $first_name = trim( $first_name ); // Return error if no Sequence ID or email address is specified. if ( empty( $sequence_id ) ) { return new WP_Error( 'convertkit_api_error', __( 'sequence_subscribe(): the sequence_id parameter is empty.', 'convertkit' ) ); } if ( empty( $email ) ) { return new WP_Error( 'convertkit_api_error', __( 'sequence_subscribe(): the email parameter is empty.', 'convertkit' ) ); } // Build request parameters. $params = array( 'api_key' => $this->api_key, 'email' => $email, 'first_name' => $first_name, ); if ( $fields ) { $params['fields'] = $fields; } // Send request. $response = $this->post( 'sequences/' . $sequence_id . '/subscribe', $params ); // If an error occured, log and return it now. if ( is_wp_error( $response ) ) { $this->log( 'API: sequence_subscribe(): Error: ' . $response->get_error_message() ); return $response; } /** * Runs actions immediately after the email address was successfully subscribed to the sequence. * * @since 1.9.6 * * @param array $response API Response * @param string $sequence_id Sequence ID * @param string $email Email Address * @param mixed $fields Custom Fields (false|array) */ do_action( 'convertkit_api_sequence_subscribe_success', $response, $sequence_id, $email, $fields ); return $response; } /** * Fetches all tags from the API. * * @since 1.9.6 * * @return WP_Error|array */ public function get_tags() { $this->log( 'API: get_tags()' ); $tags = array(); // Send request. $response = $this->get( 'tags', array( 'api_key' => $this->api_key, ) ); // If an error occured, log and return it now. if ( is_wp_error( $response ) ) { $this->log( 'API: get_tags(): Error: ' . $response->get_error_message() ); return $response; } // If no tags exist, return WP_Error. if ( ! isset( $response['tags'] ) ) { $this->log( 'API: get_tags(): Error: No tags exist in ConvertKit.' ); return new WP_Error( 'convertkit_api_error', __( 'No tags exist in ConvertKit. Visit your ConvertKit account and create your first tag.', 'convertkit' ) ); } if ( ! count( $response['tags'] ) ) { $this->log( 'API: get_tags(): Error: No tags exist in ConvertKit.' ); return new WP_Error( 'convertkit_api_error', __( 'No tags exist in ConvertKit. Visit your ConvertKit account and create your first tag.', 'convertkit' ) ); } foreach ( $response['tags'] as $tag ) { $tags[] = $tag; } return $tags; } /** * Subscribes an email address to a tag. * * @since 1.9.6 * * @param int $tag_id Tag ID. * @param string $email Email Address. * @param string $first_name First Name. * @param mixed $fields Custom Fields (false|array). * @return WP_Error|array */ public function tag_subscribe( $tag_id, $email, $first_name = '', $fields = false ) { $this->log( 'API: tag_subscribe(): [ tag_id: ' . $tag_id . ', email: ' . $email . ']' ); // Sanitize some parameters. $tag_id = absint( $tag_id ); $email = trim( $email ); $first_name = trim( $first_name ); // Return error if no Tag ID or email address is specified. if ( empty( $tag_id ) ) { return new WP_Error( 'convertkit_api_error', __( 'tag_subscribe(): the tag_id parameter is empty.', 'convertkit' ) ); } if ( empty( $email ) ) { return new WP_Error( 'convertkit_api_error', __( 'tag_subscribe(): the email parameter is empty.', 'convertkit' ) ); } // Build request parameters. $params = array( 'api_key' => $this->api_key, 'email' => $email, 'first_name' => $first_name, ); if ( $fields ) { $params['fields'] = $fields; } // Send request. $response = $this->post( 'tags/' . $tag_id . '/subscribe', $params ); // If an error occured, log and return it now. if ( is_wp_error( $response ) ) { $this->log( 'API: tag_subscribe(): Error: ' . $response->get_error_message() ); return $response; } /** * Runs actions immediately after the email address was successfully subscribed to the tag. * * @since 1.9.6 * * @param array $response API Response * @param int $tag_id Tag ID * @param string $email Email Address * @param mixed $fields Custom Fields (false|array). */ do_action( 'convertkit_api_tag_subscribe_success', $response, $tag_id, $email, $fields ); return $response; } /** * Gets a subscriber by their email address. * * @since 1.9.6 * * @param string $email Email Address. * @return WP_Error|array */ public function get_subscriber_by_email( $email ) { $this->log( 'API: get_subscriber_by_email(): [ email: ' . $email . ']' ); // Sanitize some parameters. $email = trim( $email ); // Return error if email address is specified. if ( empty( $email ) ) { return new WP_Error( 'convertkit_api_error', __( 'get_subscriber_by_email(): the email parameter is empty.', 'convertkit' ) ); } // Send request. $response = $this->get( 'subscribers', array( 'api_secret' => $this->api_secret, 'email_address' => $email, ) ); // If an error occured, log and return it now. if ( is_wp_error( $response ) ) { $this->log( 'API: get_subscriber_by_email(): Error: ' . $response->get_error_message() ); return $response; } // If no subscribers exist, return WP_Error. if ( ! absint( $response['total_subscribers'] ) ) { $error = new WP_Error( 'convertkit_api_error', sprintf( /* translators: Email Address */ __( 'No subscriber(s) exist in ConvertKit matching the email address %s.', 'convertkit' ), $email ) ); $this->log( 'API: get_subscriber_by_email(): Error: ' . $error->get_error_message() ); return $error; } return $response['subscribers'][0]; } /** * Gets a subscriber by their ConvertKit subscriber ID. * * @since 1.9.6 * * @param int $subscriber_id Subscriber ID. * @return WP_Error|array */ public function get_subscriber_by_id( $subscriber_id ) { $this->log( 'API: get_subscriber_by_id(): [ subscriber_id: ' . $subscriber_id . ']' ); // Sanitize some parameters. $subscriber_id = absint( $subscriber_id ); // Return error if no Subscriber ID is specified. if ( empty( $subscriber_id ) ) { return new WP_Error( 'convertkit_api_error', __( 'get_subscriber_by_id(): the subscriber_id parameter is empty.', 'convertkit' ) ); } // Send request. $response = $this->get( 'subscribers/' . $subscriber_id, array( 'api_secret' => $this->api_secret, ) ); // If an error occured, log and return it now. if ( is_wp_error( $response ) ) { $this->log( 'API: get_subscriber_by_id(): Error: ' . $response->get_error_message() ); return $response; } // If no subscriber exists, return WP_Error. if ( ! isset( $response['subscriber'] ) ) { $error = new WP_Error( 'convertkit_api_error', sprintf( /* translators: Subscriber ID */ __( 'No subscriber exist in ConvertKit matching the subscriber ID %s.', 'convertkit' ), $subscriber_id ) ); $this->log( 'API: get_subscriber_by_id(): Error: ' . $error->get_error_message() ); return $error; } return $response['subscriber']; } /** * Gets a list of tags for the given ConvertKit subscriber ID. * * @since 1.9.6 * * @param int $subscriber_id Subscriber ID. * @return WP_Error|array */ public function get_subscriber_tags( $subscriber_id ) { $this->log( 'API: get_subscriber_tags(): [ subscriber_id: ' . $subscriber_id . ']' ); // Sanitize some parameters. $subscriber_id = absint( $subscriber_id ); // Return error if no Subscriber ID is specified. if ( empty( $subscriber_id ) ) { return new WP_Error( 'convertkit_api_error', __( 'get_subscriber_tags(): the subscriber_id parameter is empty.', 'convertkit' ) ); } // Send request. $response = $this->get( 'subscribers/' . $subscriber_id . '/tags', array( 'api_key' => $this->api_key, ) ); // If an error occured, log and return it now. if ( is_wp_error( $response ) ) { $this->log( 'API: get_subscriber_tags(): Error: ' . $response->get_error_message() ); return $response; } // If no tags exists, return WP_Error. if ( ! isset( $response['tags'] ) ) { $error = new WP_Error( 'convertkit_api_error', sprintf( /* translators: Subscriber ID */ __( 'No tags exist in ConvertKit for the subscriber ID %s.', 'convertkit' ), $subscriber_id ) ); $this->log( 'API: get_subscriber_tags(): Error: ' . $error->get_error_message() ); return $error; } return $response['tags']; } /** * Returns the subscriber's ID by their email address. * * @since 1.9.6 * * @param string $email_address Email Address. * @return WP_Error|int */ public function get_subscriber_id( $email_address ) { // Get subscriber. $subscriber = $this->get_subscriber_by_email( $email_address ); // If an error occured, log and return it now. if ( is_wp_error( $subscriber ) ) { return $subscriber; } // Return ID. return $subscriber['id']; } /** * Unsubscribes an email address. * * @since 1.9.6 * * @param string $email Email Address. * @return WP_Error|array */ public function unsubscribe( $email ) { $this->log( 'API: unsubscribe(): [ email: ' . $email . ']' ); // Sanitize some parameters. $email = trim( $email ); // Return error if no email address is specified. if ( empty( $email ) ) { return new WP_Error( 'convertkit_api_error', __( 'unsubscribe(): the email parameter is empty.', 'convertkit' ) ); } // Send request. $response = $this->post( 'unsubscribe', array( 'api_secret' => $this->api_secret, 'email' => $email, ) ); // If an error occured, log and return it now. if ( is_wp_error( $response ) ) { $this->log( 'API: unsubscribe(): Error: ' . $response->get_error_message() ); return $response; } /** * Runs actions immediately after the email address was successfully unsubscribed. * * @since 1.9.6 * * @param array $response API Response * @param string $email Email Address */ do_action( 'convertkit_api_form_unsubscribe_success', $response, $email ); return $response; } /** * Gets all custom fields from the API. * * @since 1.9.6.9 * * @return WP_Error|array */ public function get_custom_fields() { $this->log( 'API: get_custom_fields()' ); $custom_fields = array(); // Send request. $response = $this->get( 'custom_fields', array( 'api_key' => $this->api_key, ) ); // If an error occured, return WP_Error. if ( is_wp_error( $response ) ) { $this->log( 'API: get_custom_fields(): Error: ' . $response->get_error_message() ); return $response; } // If no custom fields exist, return WP_Error. if ( ! isset( $response['custom_fields'] ) ) { $this->log( 'API: get_custom_fields(): Error: No custom fields exist in ConvertKit.' ); return new WP_Error( 'convertkit_api_error', __( 'No custom fields exist in ConvertKit. Visit your ConvertKit account and create your first custom field.', 'convertkit' ) ); } if ( ! count( $response['custom_fields'] ) ) { $this->log( 'API: get_custom_fields(): Error: No custom fields exist in ConvertKit.' ); return new WP_Error( 'convertkit_api_error', __( 'No custom fields exist in ConvertKit. Visit your ConvertKit account and create your first custom field.', 'convertkit' ) ); } foreach ( $response['custom_fields'] as $custom_field ) { $custom_fields[] = $custom_field; } return $custom_fields; } /** * Get HTML from ConvertKit for the given Legacy Form ID. * * This isn't specifically an API function, but for now it's best suited here. * * @param int $id Form ID. * @return WP_Error|string HTML */ public function get_form_html( $id ) { // Define Legacy Form URL. $url = add_query_arg( array( 'k' => $this->api_key, 'v' => 2, ), 'https://api.convertkit.com/forms/' . $id . '/embed' ); // Get HTML. $body = $this->get_html( $url ); return $body; } /** * Get HTML from ConvertKit for the given Landing Page URL. * * This isn't specifically an API function, but for now it's best suited here. * * @param string $url URL of Landing Page. * @return string HTML */ public function get_landing_page_html( $url ) { // Get HTML. $body = $this->get_html( $url, false ); // Inject JS for subscriber forms to work. $scripts = new WP_Scripts(); $script = ""; // phpcs:ignore $script .= ""; // phpcs:ignore $script .= "'; // phpcs:ignore $body = str_replace( '', '' . $script, $body ); return $body; } /** * Create a Purchase. * * @since 1.9.6.9 * * @param array $purchase Purchase Data. * @return WP_Error|array */ public function purchase_create( $purchase ) { $this->log( 'API: purchase_create(): [ purchase: ' . print_r( $purchase, true ) . ']' ); // phpcs:ignore $response = $this->post( 'purchases', array( 'api_secret' => $this->api_secret, 'purchase' => $purchase, ) ); if ( is_wp_error( $response ) ) { $this->log( 'API: purchase_create(): Error: ' . $response->get_error_message() ); } /** * Runs actions immediately after the purchase data address was successfully created. * * @since 1.9.6.9 * * @param array $response API Response * @param array $purchase Purchase Data */ do_action( 'convertkit_api_purchase_create_success', $response, $purchase ); return $response; } /** * Backward compat. function for updating Forms, Landing Pages and Tags in WordPress options table. * * @since 1.0.0 * * @param string $api_key API Key. * @param string $api_secret API Secret. */ public function update_resources( $api_key, $api_secret ) { // phpcs:ignore // Warn the developer that they shouldn't use this function. _deprecated_function( __FUNCTION__, '1.9.6', 'refresh() in ConvertKit_Resource_Forms, ConvertKit_Resource_Landing_Pages and ConvertKit_Resource_Tags classes.' ); // Initialize resource classes. $forms = new ConvertKit_Resource_Forms(); $landing_pages = new ConvertKit_Resource_Landing_Pages(); $tags = new ConvertKit_Resource_Tags(); // Refresh resources by calling the API and storing the results. $forms->refresh(); $landing_pages->refresh(); $tags->refresh(); } /** * Backward compat. function for getting a ConvertKit subscriber by their ID. * * @since 1.9.6 * * @param int $id Subscriber ID. * @return WP_Error|array */ public function get_subscriber( $id ) { // Warn the developer that they shouldn't use this function. _deprecated_function( __FUNCTION__, '1.9.6', 'get_subscriber_by_id()' ); // Pass request to new function. return $this->get_subscriber_by_id( $id ); } /** * Backward compat. function for subscribing a ConvertKit subscriber to the given Tag. * * @since 1.9.6 * * @param int $tag Tag ID. * @param array $args Arguments. * @return WP_Error|array */ public function add_tag( $tag, $args ) { // Warn the developer that they shouldn't use this function. _deprecated_function( __FUNCTION__, '1.9.6', 'tag_subscribe( $tag_id, $email_address )' ); // Pass request to new function. return $this->tag_subscribe( $tag, $args['email'] ); } /** * Backward compat. function for fetching Legacy Form or Landing Page markup for the given URL. * * @since 1.9.6 * * @param string $url URL. * @return WP_Error|string */ public function get_resource( $url ) { // Warn the developer that they shouldn't use this function. _deprecated_function( __FUNCTION__, '1.9.6', 'get_form_html( $form_id ) or get_landing_page_html( $url )' ); // Pass request to new function. return $this->get_landing_page_html( $url ); } /** * Backward compat. function for fetching Legacy Form or Landing Page markup for the given URL. * * @since 1.9.6 * * @param array $args Arguments (single email key). * @return WP_Error|array */ public function form_unsubscribe( $args ) { // Warn the developer that they shouldn't use this function. _deprecated_function( __FUNCTION__, '1.9.6', 'unsubscribe( $email_address )' ); // Pass request to new function. return $this->unsubscribe( $args['email'] ); } /** * Get HTML for the given URL. * * This isn't specifically an API function, but for now it's best suited here. * * @param string $url URL of Form or Landing Page. * @param bool $body_only Return HTML between
and tags only. * @return WP_Error|string */ private function get_html( $url, $body_only = true ) { // Get HTML from URL. $result = wp_remote_get( $url, array( 'Accept-Encoding' => 'gzip', 'timeout' => $this->get_timeout(), 'user-agent' => $this->get_user_agent(), ) ); // If an error occured, log and return it now. if ( is_wp_error( $result ) ) { return $result; } // Fetch HTTP response code and body. $http_response_code = wp_remote_retrieve_response_code( $result ); $body = wp_remote_retrieve_body( $result ); // If the body appears to be JSON containing an error, the request for a Legacy Form // through api.convertkit.com failed, so return a WP_Error now. if ( $this->is_json( $body ) ) { $json = json_decode( $body ); return new WP_Error( 'convertkit_api_error', sprintf( /* translators: API Error Message */ __( 'ConvertKit: %s', 'convertkit' ), $json->error_message ) ); } // Get just the scheme and host from the URL. $url_scheme = wp_parse_url( $url ); $url_scheme_host_only = $url_scheme['scheme'] . '://' . $url_scheme['host']; // Load the landing page HTML into a DOMDocument. libxml_use_internal_errors( true ); $html = new DOMDocument(); if ( $body_only ) { // Prevent DOMDocument from including a doctype on saveHTML(). // We don't use LIBXML_HTML_NOIMPLIED, as it requires a single root element, which Legacy Forms don't have. $html->loadHTML( mb_convert_encoding( $body, 'HTML-ENTITIES', 'UTF-8' ), LIBXML_HTML_NODEFDTD ); } else { $html->loadHTML( mb_convert_encoding( $body, 'HTML-ENTITIES', 'UTF-8' ) ); } // Convert any relative URLs to absolute URLs in the HTML DOM. $this->convert_relative_to_absolute_urls( $html->getElementsByTagName( 'a' ), 'href', $url_scheme_host_only ); $this->convert_relative_to_absolute_urls( $html->getElementsByTagName( 'link' ), 'href', $url_scheme_host_only ); $this->convert_relative_to_absolute_urls( $html->getElementsByTagName( 'img' ), 'src', $url_scheme_host_only ); $this->convert_relative_to_absolute_urls( $html->getElementsByTagName( 'script' ), 'src', $url_scheme_host_only ); $this->convert_relative_to_absolute_urls( $html->getElementsByTagName( 'form' ), 'action', $url_scheme_host_only ); // If the entire HTML needs to be returned, return it now. if ( ! $body_only ) { return $html->saveHTML(); } // Remove some HTML tags that DOMDocument adds, returning the output. // We do this instead of using LIBXML_HTML_NOIMPLIED in loadHTML(), because Legacy Forms are not always contained in // a single root / outer element, which is required for LIBXML_HTML_NOIMPLIED to correctly work. return $this->strip_html_head_body_tags( $html->saveHTML() ); } /** * Determines if the given string is JSON. * * @since 1.9.6.4 * * @param string $string Possible JSON String. * @return bool Is JSON String. */ private function is_json( $string ) { json_decode( $string ); return json_last_error() === JSON_ERROR_NONE; } /** * Converts any relative URls to absolute, fully qualified HTTP(s) URLs for the given * DOM Elements. * * @since 1.9.6 * * @param DOMNodeList