$plugins = json_encode( $plugins ); //get theme info $theme = wp_get_theme(); $theme_name = $theme->get( 'Name' ); $theme_uri = $theme->get( 'ThemeURI' ); $theme_version = $theme->get( 'Version' ); $theme_author = $theme->get( 'Author' ); $theme_author_uri = $theme->get( 'AuthorURI' ); $im = is_multisite(); $lang = get_locale(); $post = array( 'plugins' => $plugins, 'tn' => $theme_name, 'tu' => $theme_uri, 'tv' => $theme_version, 'ta' => $theme_author, 'tau' => $theme_author_uri, 'im' => $im, 'lang' => $lang, ); /** * Allows the remote post parameters to be filtered to add more data. * * @since 1.0 * * @param array $post The current data array. * * @return array */ return apply_filters( 'gravity_api_remote_post_params', $post );; } /** * Update the usage data (call version.php in Gravity Manager). We will replace it once we have statistics API endpoints. * * @since 1.0 */ public function update_site_data() { // Whenever we update the plugins info, we call the versions.php to update usage data. $options = array( 'method' => 'POST' ); $options['headers'] = array( 'Content-Type' => 'application/x-www-form-urlencoded; charset=' . get_option( 'blog_charset' ), 'User-Agent' => 'WordPress/' . get_bloginfo( 'version' ), 'Referer' => get_bloginfo( 'url' ), ); $options['body'] = $this->common->get_remote_post_params(); // Set the version to 3 which lightens the burden of version.php, it won't return anything to us anymore. $options['body']['version'] = '3'; $options['timeout'] = 15; $nocache = 'nocache=1'; //disabling server side caching $this->common->post_to_manager( 'version.php', $nocache, $options ); } /** * Send an email to Hubspot to add to the list. * * @since 1.0 * * @param $email * * @return mixed */ public function send_email_to_hubspot( $email ) { $body = array( 'email' => $email, ); $result = $this->request( 'emails/installation/add-to-list', $body, 'POST', array( 'headers' => $this->get_license_info_header( $site_secret ) ) ); $result = $this->prepare_response_body( $result, true ); if ( is_wp_error( $result ) ) { return $result; } return true; } // # HELPERS /** * Get the stored license key. * * @since 1.0 * * @return mixed */ public function get_key() { return $this->common->get_key(); } /** * Get the site auth header. * * @since 1.0 * * @param $site_key * @param $site_secret * * @return string[] */ private function get_site_auth_header( $site_key, $site_secret ) { $auth = base64_encode( "{$site_key}:{$site_secret}" ); return array( 'Authorization' => 'GravityAPI ' . $auth ); } /** * Get the license info header. * * @since 1.0 * * @param $site_secret * * @return string[] */ private function get_license_info_header( $site_secret ) { $auth = base64_encode( "gravityforms.com:{$site_secret}" ); return array( 'Authorization' => 'GravityAPI ' . $auth ); } /** * Get the license auth header. * * @since 1.0 * * @param $license_key_md5 * * @return string[] */ private function get_license_auth_header( $license_key_md5 ) { $auth = base64_encode( "license:{$license_key_md5}" ); return array( 'Authorization' => 'GravityAPI ' . $auth ); } /** * Prepare response body. * * @since 1.0 * * @param WP_Error|WP_REST_Response $raw_response The API response. * @param bool $as_array Whether to return the response as an array or object. * * @return array|object|WP_Error */ public function prepare_response_body( $raw_response, $as_array = false ) { if ( is_wp_error( $raw_response ) ) { return $raw_response; } $response_body = json_decode( wp_remote_retrieve_body( $raw_response ), $as_array ); $response_code = wp_remote_retrieve_response_code( $raw_response ); $response_message = wp_remote_retrieve_response_message( $raw_response ); if ( $response_code > 200 ) { // If a WP_Error was returned in the body. if ( $this->common->rgar( $response_body, 'code' ) ) { // Restore the WP_Error. $error = new WP_Error( $response_body['code'], $response_body['message'], $response_body['data'] ); } else { $error = new WP_Error( 'server_error', 'Error from server: ' . $response_message ); } return $error; } return $response_body; } /** * Purge the site credentials. * * @since 1.0 * * @return void */ public function purge_site_credentials() { delete_option( 'gf_site_key' ); delete_option( 'gf_site_secret' ); delete_option( 'gf_site_registered' ); } /** * Making API requests. * * @since 1.0 * * @param string $resource The API route. * @param array $body The request body. * @param string $method The method. * @param array $options The options. * * @return array|WP_Error */ public function request( $resource, $body, $method = 'POST', $options = array() ) { $body['timestamp'] = time(); // set default options $options = wp_parse_args( $options, array( 'method' => $method, 'timeout' => 10, 'body' => in_array( $method, array( 'GET', 'DELETE' ) ) ? null : json_encode( $body ), 'headers' => array(), 'sslverify' => false, ) ); // set default header options $options['headers'] = wp_parse_args( $options['headers'], array( 'Content-Type' => 'application/json; charset=' . get_option( 'blog_charset' ), 'User-Agent' => 'WordPress/' . get_bloginfo( 'version' ), 'Referer' => get_bloginfo( 'url' ), ) ); // WP docs say method should be uppercase $options['method'] = strtoupper( $options['method'] ); $request_url = $this->get_gravity_api_url() . $resource; return wp_remote_request( $request_url, $options ); } /** * Get the site key. * * @since 1.0 * * @return false|mixed|void */ public function get_site_key() { if ( defined( 'GRAVITY_API_SITE_KEY' ) ) { return GRAVITY_API_SITE_KEY; } $site_key = get_option( 'gf_site_key' ); if ( empty( $site_key ) ) { return false; } return $site_key; } /** * Get the site secret. * * @since 1.0 * * @return false|mixed|void */ public function get_site_secret() { if ( defined( 'GRAVITY_API_SITE_SECRET' ) ) { return GRAVITY_API_SITE_SECRET; } $site_secret = get_option( 'gf_site_secret' ); if ( empty( $site_secret ) ) { return false; } return $site_secret; } /** * Get the gravity URL. * * @since 1.0 * * @return string */ public function get_gravity_api_url() { return trailingslashit( GRAVITY_API_URL ); } /** * Check if the site has the gf_site_key and gf_site_secret options. * * @since 1.0 * * @return bool */ public function is_site_registered() { return $this->get_site_key() && $this->get_site_secret(); } /** * Check if the site has the gf_site_key, gf_site_secret and also the gf_site_registered options. * * @since 1.0 * * @return bool */ public function is_legacy_registration() { return $this->is_site_registered() && ! get_option( 'gf_site_registered' ); } }