__construct(); } /** * Creates a contact widget. */ function __construct() { parent::__construct( false, $name = 'Affiliates Contact' ); add_action( 'wp_print_styles', array( 'Affiliates_Contact', '_print_styles' ) ); add_action( 'wp_enqueue_scripts', array( 'Affiliates_Contact', '_enqueue_scripts' ) ); } /** * Enqueues required stylesheets. */ function _print_styles() { global $affiliates_version; wp_enqueue_style( 'affiliates', AFFILIATES_PLUGIN_URL . 'css/affiliates.css', array(), $affiliates_version ); } /** * Enqueues required scripts. */ function _enqueue_scripts() { if ( !is_admin() ) { wp_enqueue_script( 'jquery' ); } } /** * Widget output * * @see WP_Widget::widget() */ function widget( $args, $instance ) { extract( $args ); $title = apply_filters( 'widget_title', $instance['title'] ); $widget_id = $args['widget_id']; // output echo $before_widget; if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } //echo "instance: " . var_export( $instance, true ); if ( $this->is_singleton ) { $ext = ''; } else { $ext = '-' . $widget_id; } if ( $this->is_singleton ) { Affiliates_Contact::render_form( '', isset( $instance['amount'] ) ? $instance['amount'] : null, isset( $instance['currency_id'] ) ? $instance['currency_id'] : null ); } else { Affiliates_Contact::render_form( $widget_id, isset( $instance['amount'] ) ? $instance['amount'] : null, isset( $instance['currency_id'] ) ? $instance['currency_id'] : null ); } echo $after_widget; } /** * Renders the contact form. * Remember NOT to use any form input elements named 'name', 'year', ... * @static */ static function render_form( $widget_id = '', $amount = null, $currency_id = null ) { $method = 'post'; $action = ""; if ( !empty( $widget_id ) ) { $ext = '-' . $widget_id; } else { $ext = ''; } $submit_name = 'affiliates-contact-submit'; $nonce = 'affiliates-contact-nonce'; $send = false; $sender_class = ''; $email_class = ''; $message_class = ''; $captcha = ''; $error = false; if ( !empty( $_POST[$submit_name] ) ) { if ( !wp_verify_nonce( $_POST[$nonce], plugin_basename( __FILE__ ) ) ) { $error = true; // fail but don't give clues } $captcha = $_POST[Affiliates_Contact::$captcha_field_id]; if ( !Affiliates_Contact::captcha_validates( $captcha ) ) { $error = true; // dumbot } $sender = Affiliates_Contact::filter( $_POST['sender'] ); $email = Affiliates_Contact::filter( $_POST['email'] ); $message = Affiliates_Contact::filter( $_POST['message'] ); if ( empty( $sender ) ) { $sender_class .= ' class="missing" '; $error = true; } if ( empty( $email ) || !is_email( $email ) ) { $email_class .= ' class="missing" '; $error = true; } if ( empty( $message ) ) { $message_class .= ' class="missing" '; $error = true; } if ( !$error ) { $send = true; $description = __( 'Affiliates contact form submission', AFFILIATES_PLUGIN_DOMAIN ); $data = array( 'name' => array( 'title' => 'Name', 'domain' => AFFILIATES_PLUGIN_DOMAIN, 'value' => $sender ), 'email' => array( 'title' => 'Email', 'domain' => AFFILIATES_PLUGIN_DOMAIN, 'value' => $email ), 'message' => array( 'title' => 'Message', 'domain' => AFFILIATES_PLUGIN_DOMAIN, 'value' => $message ) ); // request a referral $affiliate = null; if ( function_exists('affiliates_suggest_referral') ) { $post_id = get_the_ID(); $affiliate_id = affiliates_suggest_referral( $post_id, $description, $data, $amount, $currency_id ); if ( $affiliate_id ) { $affiliate = affiliates_get_affiliate( $affiliate_id ); // Now you could send an email to the affiliate ... } } } } else { $sender = ''; $email = ''; $message = ''; } if ( !$send ) { echo '
' . __( 'Thanks!', AFFILIATES_PLUGIN_DOMAIN ) . '
'; } } /** * Filters mail header injection, html, ... * @param unknown_type $unfiltered_value */ static function filter( $unfiltered_value ) { $mail_filtered_value = preg_replace('/(%0A|%0D|content-type:|to:|cc:|bcc:)/i', '', $unfiltered_value ); return stripslashes( wp_filter_nohtml_kses( Affiliates_Contact::filter_xss( trim( strip_tags( $mail_filtered_value ) ) ) ) ); } /** * Filter xss * * @param string $string input * @return filtered string */ static function filter_xss( $string ) { // Remove NUL characters (ignored by some browsers) $string = str_replace(chr(0), '', $string); // Remove Netscape 4 JS entities $string = preg_replace('%&\s*\{[^}]*(\}\s*;?|$)%', '', $string); // Defuse all HTML entities $string = str_replace('&', '&', $string); // Change back only well-formed entities in our whitelist // Decimal numeric entities $string = preg_replace('/&#([0-9]+;)/', '\1', $string); // Hexadecimal numeric entities $string = preg_replace('/&#[Xx]0*((?:[0-9A-Fa-f]{2})+;)/', '\1', $string); // Named entities $string = preg_replace('/&([A-Za-z][A-Za-z0-9]*;)/', '&\1', $string); return preg_replace('% ( <(?=[^a-zA-Z!/]) # a lone < | # or <[^>]*(>|$) # a string that starts with a <, up until the > or the end of the string | # or > # just a > )%x', '', $string); } /** * Returns captcha field markup. * * @return captcha field markup */ static function captcha_get( $value ) { $style = 'display:none;'; $field = ''; return $field; } /** * Validates a captcha field. * * @param string $field_value field content * @return true if the field validates */ static function captcha_validates( $field_value = null ) { $result = false; if ( empty( $field_value ) ) { $result = true; } return $result; } /** * Save widget options * * @see WP_Widget::update() */ function update( $new_instance, $old_instance ) { $settings = $old_instance; $settings['title'] = strip_tags( $new_instance['title'] ); if ( !empty( $new_instance['amount'] ) ) { $settings['amount'] = Affiliates_Utility::verify_referral_amount( $new_instance['amount'] ); } else { unset( $settings['amount'] ); } if ( !empty( $new_instance['currency_id'] ) ) { $settings['currency_id'] = Affiliates_Utility::verify_currency_id( $new_instance['currency_id'] ); } else { unset( $settings['currency_id'] ); } return $settings; } /** * Output admin widget options form * * @see WP_Widget::form() */ function form( $instance ) { $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; $amount = isset( $instance['amount'] ) ? esc_attr( $instance['amount'] ) : ''; $currency_id = isset( $instance['currency_id'] ) ? esc_attr( $instance['currency_id'] ) : ''; ?>