' . sprintf( /* translators: plugin name, link */ esc_html__( 'The %1$s plugin requires the %2$s plugin.', 'affiliates-woocommerce-light' ), 'Affiliates WooCommerce Light', 'Affiliates' ) . ''; } if ( !$woocommerce_is_active ) { self::$admin_messages[] = '
' . sprintf( /* translators: plugin name, link */ esc_html__( 'The %1$s plugin requires the %2$s plugin.', 'affiliates-woocommerce-light' ), 'Affiliates WooCommerce Light', 'WooCommerce' ) . "
"; } if ( $affiliates_woocommerce_is_active ) { self::$admin_messages[] = '
' . sprintf( /* translators: plugin name, plugin name, plugin name */ esc_html__( 'You do not need to use the %1$s plugin because you are already using the advanced %2$s plugin. Please deactivate the %3$s plugin now.', 'affiliates-woocommerce-light' ), 'Affiliates WooCommerce Light', 'Affiliates WooCommerce Integration', 'Affiliates WooCommerce Light' ) . '
'; } } /** * Checks dependencies and adds appropriate actions and filters. */ public static function init() { add_action( 'init', array( __CLASS__, 'wp_init' ) ); add_action( 'admin_notices', array( __CLASS__, 'admin_notices' ) ); $verified = true; $disable = false; $affiliates_is_active = self::is_active( 'affiliates/affiliates.php' ) || self::is_active( 'affiliates-pro/affiliates-pro.php' ) || self::is_active( 'affiliates-enterprise/affiliates-enterprise.php' ); $woocommerce_is_active = self::is_active( 'woocommerce/woocommerce.php' ); $affiliates_woocommerce_is_active = self::is_active( 'affiliates-woocommerce/affiliates-woocommerce.php' ); 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 ) { add_action ( 'woocommerce_checkout_order_processed', array( __CLASS__, 'woocommerce_checkout_order_processed' ), 10, 3 ); add_action( 'woocommerce_store_api_checkout_order_processed', array( __CLASS__, 'woocommerce_store_api_checkout_order_processed' ), 10 ); if ( function_exists( 'affiliates_get_referral_post_permalink' ) ) { add_filter( 'affiliates_referral_post_permalink', array( __CLASS__, 'affiliates_referral_post_permalink' ), 10, 2 ); } else { add_filter( 'post_type_link', array( __CLASS__, 'post_type_link' ), 10, 4 ); } if ( function_exists( 'affiliates_get_referral_post_title' ) ) { add_filter( 'affiliates_referral_post_title', array( __CLASS__, 'affiliates_referral_post_title' ), 10, 2 ); } 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_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( __CLASS__, 'plugin_action_links' ) ); } } /** * Settings link. * * @since 3.2.0 * * @param string[] $links * * @return string[] */ public static function plugin_action_links( $links ) { if ( current_user_can( AFFILIATES_ADMINISTER_OPTIONS ) ) { $url = add_query_arg( array( 'page' => 'affiliates-admin-woocommerce-light' ), admin_url( 'admin.php' ) ); $links[] = sprintf( '%2$s', esc_url( $url ), esc_html__( 'Settings', 'affiliates-woocommerce-light' ) ); } return $links; } /** * Check whether the plugin is active. * * @return boolean */ public static function is_active( $plugin ) { if ( !isset( self::$plugins[$plugin] ) ) { self::$plugins[$plugin] = false; if ( function_exists( 'wp_get_active_and_valid_plugins' ) ) { $plugin_path = trailingslashit( WP_PLUGIN_DIR ) . $plugin; $active_plugin_paths = wp_get_active_and_valid_plugins(); self::$plugins[$plugin] = in_array( $plugin_path, $active_plugin_paths ); if ( !self::$plugins[$plugin] && is_multisite() && function_exists( 'wp_get_active_network_plugins' ) ) { $active_network_plugin_paths = wp_get_active_network_plugins(); self::$plugins[$plugin] = in_array( $plugin_path, $active_network_plugin_paths ); } } else { $active_plugins = get_option( 'active_plugins', array() ); if ( is_multisite() ) { $active_sitewide_plugins = get_site_option( 'active_sitewide_plugins', array() ); $active_sitewide_plugins = array_keys( $active_sitewide_plugins ); $active_plugins = array_merge( $active_plugins, $active_sitewide_plugins ); } self::$plugins[$plugin] = in_array( $plugin, $active_plugins ); } } return self::$plugins[$plugin]; } /** * 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 ( isset( $_POST[self::NONCE] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST[self::NONCE] ) ), self::SET_ADMIN_OPTIONS ) ) { $options[self::REFERRAL_RATE] = floatval( $_POST[self::REFERRAL_RATE] ?? 0.0 ); 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 .= '

'; $output .= esc_html__( 'Affiliates WooCommerce Integration Light', 'affiliates-woocommerce-light' ); $output .= '

'; $output .= '

'; $output .= sprintf( /* translators: */ esc_html__( 'Get additional features with %1$s and %2$s!', 'affiliates-woocommerce-light' ), 'Affiliates Pro', 'Affiliates Enterprise' ); $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; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped 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; $protocol = is_ssl() ? 'https://' : 'http://'; return '
' . '

' . ( $usage_stats ? sprintf( "Logo", $protocol ) : '' ) . sprintf( /* translators: link */ esc_html__( 'Powered by %1$s', 'affiliates-woocommerce-light' ), 'itthinx.com', ) . '

' . '
' . $footer; } /** * Filter to use the URL to edit the order. * * @since 2.0.0 * * @param string $url * @param array|object $referral * * @return string */ public static function affiliates_referral_post_permalink( $url, $referral ) { $data = null; if ( is_array( $referral ) && array_key_exists( 'data', $referral ) ) { $data = $referral['data']; } else if ( is_object( $referral ) && property_exists( $referral, 'data' ) ) { $data = $referral->data; } if ( $data !== null ) { if ( !empty( $data ) ) { $data = unserialize( $data ); if ( is_array( $data ) ) { if ( array_key_exists( 'order_id', $data ) ) { $order_id_data = $data['order_id']; if ( is_array( $order_id_data ) && array_key_exists( 'domain', $order_id_data ) && $order_id_data['domain'] === 'affiliates-woocommerce-light' && array_key_exists( 'value', $order_id_data ) ) { $order = wc_get_order( $order_id_data['value'] ); if ( $order instanceof WC_Order ) { if ( current_user_can( 'manage_woocommerce' ) ) { $url = $order->get_edit_order_url(); } } } } } } } // A simpler way would be like this, but we could mistakenly modify links related to referrals from other integrations // if ( is_array( $referral ) && array_key_exists( 'post_id', $referral ) ) { // $order = wc_get_order( $referral['post_id'] ); // if ( $order instanceof WC_Order ) { // $url = $order->get_edit_order_url(); // } // } return $url; } /** * Filter to display the order title. * * @param string $title * @param array|object $referral * * @return string */ public static function affiliates_referral_post_title( $title, $referral ) { $data = null; if ( is_array( $referral ) && array_key_exists( 'data', $referral ) ) { $data = $referral['data']; } else if ( is_object( $referral ) && property_exists( $referral, 'data' ) ) { $data = $referral->data; } if ( $data !== null ) { if ( !empty( $data ) ) { $data = unserialize( $data ); if ( is_array( $data ) ) { if ( array_key_exists( 'order_id', $data ) ) { $order_id_data = $data['order_id']; if ( is_array( $order_id_data ) && array_key_exists( 'domain', $order_id_data ) && $order_id_data['domain'] === 'affiliates-woocommerce-light' && array_key_exists( 'value', $order_id_data ) ) { $order = wc_get_order( $order_id_data['value'] ); if ( $order instanceof WC_Order ) { if ( current_user_can( 'manage_woocommerce' ) ) { $title = sprintf( '%s #%s', esc_html( $order->get_title() ), esc_html( $order->get_order_number() ) ); } } } } } } } return $title; } /** * Returns an edit link for shop_order post types. * * @deprecated since 2.0.0 * * @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 ) { $url = $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( sanitize_text_field( wp_unslash( $_REQUEST['page'] ) ), self::$shop_order_link_modify_pages ) && // phpcs:ignore WordPress.Security.NonceVerification.Recommended // 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 ) ) ) { $order = wc_get_order( $post->ID ); if ( $order instanceof WC_Order ) { $url = $order->get_edit_order_url(); } } return $url; } /** * Make sure the shop_order type is in the $order_types array. * * @param array $order_types * @param string $for * * @return array */ public static function wc_order_types( $order_types, $for ) { if ( !in_array( 'shop_order', $order_types ) ) { $order_types[] = 'shop_order'; } return $order_types; } /** * Record a product referral when a new order has been processed. * * @param WC_Order $order */ public static function woocommerce_store_api_checkout_order_processed( $order ) { self::order_processed( $order ); } /** * 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 * @param array $posted_data an array of data * @param WC_Order $order the order object */ public static function woocommerce_checkout_order_processed( $order_id, $posted_data, $order ) { self::order_processed( $order ); } /** * Record a referral when a new order has been processed. * * @param WC_Order $order */ public static function order_processed( $order ) { if ( !( $order instanceof WC_Order ) ) { return; } $order_id = $order->get_id(); $currency = $order->get_currency(); $order_subtotal = $order->get_subtotal(); $discount = $order->get_total_discount(); $order_subtotal -= $discount; if ( $order_subtotal < 0 ) { $order_subtotal = 0; } // Workaround to an issue observed as of WooCommerce 7.8.0: Invalid order type: shop_order during checkout with the $order->get_edit_order_url() call: add_filter( 'wc_order_types', array( __CLASS__, 'wc_order_types' ), 10, 2 ); $edit_url = $order->get_edit_order_url(); remove_filter( 'wc_order_types', array( __CLASS__, 'wc_order_types' ) ); $order_link = sprintf( '%s', esc_url( $edit_url ), sprintf( /* translators: order number */ esc_html__( 'Order #%s', 'affiliates-woocommerce-light' ), $order_id ) ); $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' ); // @since 2.0.0 HPOS compatibility add_action( 'before_woocommerce_init', function() { if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) { \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true ); } } );