" . __( 'The Affiliates WooCommerce Integration Light plugin requires the Affiliates plugin.', 'affiliates-woocommerce-light' ) . ""; } if ( !$woocommerce_is_active ) { self::$admin_messages[] = "
" . __( 'The Affiliates WooCommerce Integration Light plugin requires the WooCommerce plugin to be activated.', 'affiliates-woocommerce-light' ) . "
"; } if ( $affiliates_woocommerce_is_active ) { self::$admin_messages[] = "
" . __( 'You do not need to use the Affiliates WooCommerce Integration Light plugin because you are already using the advanced Affiliates WooCommerce Integration plugin. Please deactivate the Affiliates WooCommerce Integration Light plugin now.', 'affiliates-woocommerce-light' ) . "
"; } if ( !$affiliates_is_active || !$woocommerce_is_active || $affiliates_woocommerce_is_active ) { if ( $disable ) { include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); deactivate_plugins( array( __FILE__ ) ); } $verified = false; } if ( $verified ) { load_plugin_textdomain( 'affiliates-woocommerce-light', null, 'affiliates-woocommerce-light' . '/languages' ); add_action ( 'woocommerce_checkout_order_processed', array( __CLASS__, 'woocommerce_checkout_order_processed' ) ); $options = get_option( self::PLUGIN_OPTIONS , array() ); add_filter( 'post_type_link', array( __CLASS__, 'post_type_link' ), 10, 4 ); add_action( 'affiliates_admin_menu', array( __CLASS__, 'affiliates_admin_menu' ) ); add_filter( 'affiliates_footer', array( __CLASS__, 'affiliates_footer' ) ); add_filter( 'affiliates_setup_buttons', array( __CLASS__, 'affiliates_setup_buttons' ) ); } } /** * Add a setup hint button. * * @param array $buttons * @return array */ public static function affiliates_setup_buttons( $buttons ) { $buttons['affiliates-woocommerce-light'] = sprintf ( '%s', add_query_arg( 'section', 'affiliates-woocommerce-light', admin_url( 'admin.php?page=affiliates-admin-woocommerce-light' ) ), esc_html__( 'Set the Commission Rate', 'affiliates-woocommerce-light' ) ); return $buttons; } /** * Adds a submenu item to the Affiliates menu for the WooCommerce integration options. */ public static function affiliates_admin_menu() { $page = add_submenu_page( 'affiliates-admin', esc_html__( 'Affiliates WooCommerce Integration Light', 'affiliates-woocommerce-light' ), esc_html__( 'WooCommerce Integration Light', 'affiliates-woocommerce-light' ), AFFILIATES_ADMINISTER_OPTIONS, 'affiliates-admin-woocommerce-light', array( __CLASS__, 'affiliates_admin_woocommerce_light' ) ); $pages[] = $page; add_action( 'admin_print_styles-' . $page, 'affiliates_admin_print_styles' ); add_action( 'admin_print_scripts-' . $page, 'affiliates_admin_print_scripts' ); } /** * Affiliates WooCommerce Integration Light : admin section. */ public static function affiliates_admin_woocommerce_light() { $output = ''; if ( !current_user_can( AFFILIATES_ADMINISTER_OPTIONS ) ) { wp_die( esc_html__( 'Access denied.', 'affiliates-woocommerce-light' ) ); } $options = get_option( self::PLUGIN_OPTIONS , array() ); if ( isset( $_POST['submit'] ) ) { if ( wp_verify_nonce( $_POST[self::NONCE], self::SET_ADMIN_OPTIONS ) ) { $options[self::REFERRAL_RATE] = floatval( $_POST[self::REFERRAL_RATE] ); if ( $options[self::REFERRAL_RATE] > 1.0 ) { $options[self::REFERRAL_RATE] = 1.0; } else if ( $options[self::REFERRAL_RATE] < 0 ) { $options[self::REFERRAL_RATE] = 0.0; } $options[self::USAGE_STATS] = !empty( $_POST[self::USAGE_STATS] ); } update_option( self::PLUGIN_OPTIONS, $options ); } $referral_rate = isset( $options[self::REFERRAL_RATE] ) ? $options[self::REFERRAL_RATE] : self::REFERRAL_RATE_DEFAULT; $usage_stats = isset( $options[self::USAGE_STATS] ) ? $options[self::USAGE_STATS] : self::USAGE_STATS_DEFAULT; $output .= '
' . '

' . esc_html__( 'Affiliates WooCommerce Integration Light', 'affiliates-woocommerce-light' ) . '

' . '
'; $output .= '

'; $output .= wp_kses( sprintf( __( 'Get additional features with %s and %s!', 'affiliates-woocommerce-light' ), 'https://www.itthinx.com/shop/affiliates-pro/', 'Affiliates Pro', 'https://www.itthinx.com/shop/affiliates-enterprise/', 'Affiliates Enterprise' ), array( 'a' => array( 'href' => array(), 'target' => array() ) ) ); $output .= '

'; $output .= '
'; $output .= '
'; $output .= '
'; $output .= '

' . esc_html__( 'Referral Rate', 'affiliates-woocommerce-light' ) . '

'; $output .= '

'; $output .= ''; $output .= ' '; $output .= ''; $output .= '

'; $output .= '

'; $output .= esc_html__( 'The referral rate determines the referral amount based on the net sale made.', 'affiliates-woocommerce-light' ); $output .= '

'; $output .= '

'; $output .= wp_kses( __( 'Example: Set the referral rate to 0.1 if you want your affiliates to get a 10% commission on each sale.', 'affiliates-woocommerce-light' ), array( 'strong' => array() ) ); $output .= '

'; $output .= '

' . esc_html__( 'Usage stats', 'affiliates-woocommerce-light' ) . '

'; $output .= '

'; $output .= ''; $output .= ' '; $output .= ''; $output .= '
'; $output .= '' . esc_html__( 'This will allow the plugin to help in computing how many installations are actually using it. No personal or site data is transmitted, this simply embeds an icon on the bottom of the Affiliates admin pages, so that the number of visits to these can be counted. This is useful to help prioritize development.', 'affiliates-woocommerce-light' ) . ''; $output .= '

'; $output .= '

'; $output .= wp_nonce_field( self::SET_ADMIN_OPTIONS, self::NONCE, true, false ); $output .= ''; $output .= '

'; $output .= '
'; $output .= '
'; $output .= '
'; echo $output; affiliates_footer(); } /** * Add a notice to the footer that the integration is active. * * @param string $footer * * @return string footer */ public static function affiliates_footer( $footer ) { $options = get_option( self::PLUGIN_OPTIONS , array() ); $usage_stats = isset( $options[self::USAGE_STATS] ) ? $options[self::USAGE_STATS] : self::USAGE_STATS_DEFAULT; return '
' . '

' . ( $usage_stats ? "Logo" : '' ) . wp_kses( sprintf( __( "Powered by itthinx.com", 'affiliates-woocommerce-light' ), 'https://www.itthinx.com/shop/', 'itthinx' ), array( 'a' => array( 'href' => array(), 'target' => array() ) ) ) . '

' . '
' . $footer; } /** * Returns an edit link for shop_order post types. * * @param string $post_link * @param array $post * @param boolean $leavename * @param boolean $sample * * @return string link URL */ public static function post_type_link( $post_link, $post, $leavename, $sample ) { $link = $post_link; if ( // right post type isset( $post->post_type) && ( $post->post_type == self::SHOP_ORDER_POST_TYPE ) && // admin page is_admin() && // right admin page isset( $_REQUEST['page'] ) && in_array( $_REQUEST['page'], self::$shop_order_link_modify_pages ) && // check link ( ( preg_match( "/" . self::SHOP_ORDER_POST_TYPE . "=([^&]*)/", $post_link, $matches ) === 1 ) && isset( $matches[1] ) && ( $matches[1] === $post->post_name ) || ( strpos( $post_link, 'post_type=' . self::SHOP_ORDER_POST_TYPE ) !== false ) && ( preg_match( '/p=([0-9]+)/', $post_link, $matches ) === 1 ) && isset( $matches[1] ) && ( $matches[1] == $post->ID ) ) ) { $link = admin_url( 'post.php?post=' . $post->ID . '&action=edit' ); } return $link; } /** * Record a referral when a new order has been processed. * * Note that we can't hook into the order process before(*), because * the meta data would not have been added. * * (*) We could hook into woocommerce_checkout_update_order_meta but the * 'coupons' meta data would not be there, so if we want to use it here at * some point, woocommerce_checkout_order_processed is a better choice. * * @param int $order_id the post id of the order */ public static function woocommerce_checkout_order_processed( $order_id ) { $order_subtotal = null; $currency = get_option( 'woocommerce_currency' ); if ( function_exists( 'wc_get_order' ) ) { if ( $order = wc_get_order( $order_id ) ) { if ( method_exists( $order, 'get_subtotal' ) ) { $order_subtotal = $order->get_subtotal(); } if ( method_exists( $order, 'get_total_discount' ) ) { $order_subtotal -= $order->get_total_discount(); // excluding tax if ( $order_subtotal < 0 ) { $order_subtotal = 0; } } if ( method_exists( $order, 'get_currency' ) ) { $currency = $order->get_currency(); } else if ( method_exists( $order, 'get_order_currency' ) ) { $currency = $order->get_order_currency(); } } } if ( $order_subtotal === null ) { $order_total = get_post_meta( $order_id, '_order_total', true ); $order_tax = get_post_meta( $order_id, '_order_tax', true ); $order_shipping = get_post_meta( $order_id, '_order_shipping', true ); $order_shipping_tax = get_post_meta( $order_id, '_order_shipping_tax', true ); $order_subtotal = $order_total - $order_tax - $order_shipping - $order_shipping_tax; } $order_link = ''; $order_link .= sprintf( esc_html__( 'Order #%s', 'affiliates-woocommerce-light' ), $order_id ); $order_link .= ""; $data = array( 'order_id' => array( 'title' => 'Order #', 'domain' => 'affiliates-woocommerce-light', 'value' => esc_sql( $order_id ) ), 'order_total' => array( 'title' => 'Total', 'domain' => 'affiliates-woocommerce-light', 'value' => esc_sql( $order_subtotal ) ), 'order_currency' => array( 'title' => 'Currency', 'domain' => 'affiliates-woocommerce-light', 'value' => esc_sql( $currency ) ), 'order_link' => array( 'title' => 'Order', 'domain' => 'affiliates-woocommerce-light', 'value' => esc_sql( $order_link ) ) ); $options = get_option( self::PLUGIN_OPTIONS , array() ); $referral_rate = isset( $options[self::REFERRAL_RATE] ) ? $options[self::REFERRAL_RATE] : self::REFERRAL_RATE_DEFAULT; $amount = round( floatval( $referral_rate ) * floatval( $order_subtotal ), AFFILIATES_REFERRAL_AMOUNT_DECIMALS ); $description = sprintf( 'Order #%s', $order_id ); affiliates_suggest_referral( $order_id, $description, $data, $amount, $currency ); } } /** * Hooked on the plugins_loaded action to boot the plugin. */ function affiliates_woocommerce_light_plugins_loaded() { Affiliates_WooCommerce_Light_Integration::init(); } add_action( 'plugins_loaded', 'affiliates_woocommerce_light_plugins_loaded' );