'' . __( 'Settings', 'abn-lookup-for-gravity-forms' ) . '', ); return array_merge( $action_links, $links ); } // END plugin_action_links /* * Ran when plugin is activated * - adds daily cron job to clear ABN Lookup cache */ public static function activation() { wp_schedule_event( time(), 'daily', 'itsg_abnlookup_clear_cache_cron' ); } // END activation /* * Ran when plugin is deactivated * - clear ABN Lookup cache * - delete daily cron job that clears ABN Lookup cache */ public function deactivation() { self::clear_database_cache(); wp_clear_scheduled_hook( 'itsg_abnlookup_clear_cache_cron' ); } // END deactivation /* * Clears ABN Lookup cache * - triggered through daily cron job and when plugin is deactivated */ public function clear_database_cache() { global $wpdb; $table_incomplete = $wpdb->prefix . "options"; $result = $wpdb->query( "DELETE FROM ".$table_incomplete." WHERE `option_name` like 'itsg_abnlookup_%'" ); } // END clear_database_cache /* * Handles Ajax request for ABN Lookup */ public static function itsg_gf_abnlookup_check_ajax() { // get abn from post request $abn = isset( $_POST['abn'] ) ? $_POST['abn'] : null; $numbersOnly = preg_replace( "/[^0-9]/","", $abn ); if ( is_Null($numbersOnly) || '' == $numbersOnly ) { $result = array( 'exception' => array ( 'exceptionDescription' => 'Empty ABN value passed.' ) ); } else { $result = self::do_abnlookup( $numbersOnly ); } die( json_encode( $result ) ); } // END itsg_gf_abnlookup_check_ajax /* * Handles ABN Lookup * - first checks cache * - if not in cache, checks ABN against the ABR * - saves results to cache * - returns results */ public static function do_abnlookup( $abn ) { if ( empty( $abn ) ) { return false; } $abn = sanitize_text_field( $abn ); $abnlookup_options = self::get_options(); if ( '' == $abnlookup_options['guid'] ) { return array( 'exception' => array ( 'exceptionDescription' => 'ABN Lookup for Gravity Forms has not been configured. The GUID necessary to communicate with the Australia Business Register has not been specified.' ) ); } /** supply from cache **/ //$result_cache = get_option( "itsg_abnlookup_{$abn}", 0 ); // redundant - using native WordPress transients $result_cache = get_transient( "itsg_abnlookup_{$abn}" ); if( $result_cache ){ $cache_datetime = strtotime( $result_cache->dateRegisterLastUpdated ); $current_datetime = strtotime( 'now' ); if ( ( $current_datetime - $cache_datetime ) < DAY_IN_SECONDS ) { return $result_cache; } } /** cache end **/ //$abnlookup = new abnlookup($abnlookup_options['guid']); //$result = $abnlookup->searchByAbn($abn)->ABRPayloadSearchResults->response; $url = "https://abr.business.gov.au/ABRXMLSearch/AbrXmlSearch.asmx/ABRSearchByABN?searchString={$abn}&includeHistoricalDetails=N&authenticationGuid={$abnlookup_options['guid']}"; $result = wp_remote_get( $url ); $result = simplexml_load_string( $result['body'] )->response; $result = json_encode( $result ); $result = json_decode( $result ); /** save the cache **/ //update_option( "itsg_abnlookup_{$abn}", $result ); // redundant - using native WordPress transients set_transient( "itsg_abnlookup_{$abn}" , $result, DAY_IN_SECONDS ); // transient will live for a day /** end cache **/ return $result; } // END do_abnlookup /* * Function for enqueuing all the required scripts */ public static function enqueue_scripts( $form, $is_ajax ) { if ( is_array( $form['fields'] ) || is_object( $form['fields'] ) ) { // get Ajax Upload options $abnlookup_options = self::get_options(); if ( is_array( $form['fields'] ) || is_object( $form['fields'] ) ) { foreach ( $form['fields'] as $field ) { if ( ITSG_GF_AbnLookup_Fields::is_abnlookup_field( $field ) ) { if ( true == $abnlookup_options['includecss'] ) { wp_enqueue_style( 'itsg_gfabnlookup_css', plugins_url( 'css/abnlookup.css', __FILE__ ) ); } } } } } } // END enqueue_scripts /* * Handles the plugin options. * Default values are stored in an array. */ public static function get_options(){ $defaults = array( 'guid' => '', 'includecss' => true, 'validation_message_not_valid' => 'The ABN provided is not valid. Check the number entered and try again, or use the ABN Lookup website to confirm for your ABN.', 'validation_message_activeabn' => 'The ABN provided is not active. Entities that do not have an active ABN cannot complete this form.', 'validation_message_reggst' => 'The ABN provided is not registered for GST. Entities that are not registered for GST cannot complete this form.', 'validation_message_notreggst' => 'The ABN provided is registered for GST. Entities that are registered for GST cannot complete this form.', 'validation_message_11_char' => "The information entered does not match a valid ABN. ABN's need to be 11 digits.", 'validation_message_loading' => 'Checking ABN with the Australian Business Register.', 'validation_message_error_communicating' => 'Error comminicating with the Australian Business Register.', 'lookup_timeout' => 5, 'lookup_retries' => 3, ); $options = wp_parse_args( get_option( 'gravityformsaddon_itsg_gf_abnlookup_settings_settings' ), $defaults ); return $options; } // END get_options /* * Warning message if Gravity Forms is installed and enabled */ public static function admin_warnings() { $abnlookup_options = self::get_options(); if ( !self::is_gravityforms_installed() ) { $html = sprintf( '

%s

%s

%s

', __( 'Warning', 'abn-lookup-for-gravity-forms' ), sprintf ( __( 'The plugin %s requires Gravity Forms to be installed.', 'abn-lookup-for-gravity-forms' ), ''.self::$name.'' ), sprintf ( __( 'Please %sdownload the latest version%s of Gravity Forms and try again.', 'abn-lookup-for-gravity-forms' ), '', '' ) ); echo $html; } elseif ( '' == $abnlookup_options['guid'] ) { $html = sprintf( '

%s

%s

%s

%s

', __( 'Warning', 'abn-lookup-for-gravity-forms' ), sprintf ( __( 'The plugin %s requires a GUID to communicate with the Australian Business Register.', 'abn-lookup-for-gravity-forms' ), ''.self::$name.'' ), sprintf ( __( 'To receive a GUID see %sweb services registration%s on the Australian Business Register website.', 'abn-lookup-for-gravity-forms' ), '', '' ), sprintf ( __( 'Once you have a GUID you will need to enter it in the %sABN Lookup for Gravity Forms Settings%s page.', 'abn-lookup-for-gravity-forms' ), '', '' ) ); echo $html; } } // END admin_warnings /* * Check if GF is installed */ private static function is_gravityforms_installed() { return class_exists( 'GFCommon' ); } // END is_gravityforms_installed } } $ITSG_GF_AbnLookup = new ITSG_GF_AbnLookup();