first commit

This commit is contained in:
2024-07-15 11:28:08 +02:00
commit f52d538ea5
21891 changed files with 6161164 additions and 0 deletions

View File

@@ -0,0 +1,101 @@
<?php
/**
* Class by which we can push created methods data to the deactivation filter
*/
class WPDesk_Flexible_Shipping_Method_Created_Tracker_Deactivation_Data implements \FSVendor\WPDesk\PluginBuilder\Plugin\Hookable {
const OPTION_FS_METHOD_CREATED_TRACKER = 'fs_method_created_tracker';
const TRACK_USERS_AFTER_THIS_DATE = '2019-04-11 01:00:00';
const NO_ACTION_WITH_FS = 0;
const FLEXIBLE_SHIPPING_METHOD_ADDED_TO_ZONE = 1;
const FLEXIBLE_SHIPPING_METHOD_ADDED_TO_FS = 2;
/**
* Fires hooks
*/
public function hooks() {
add_filter( 'wpdesk_tracker_deactivation_data', array( $this, 'append_variant_id_to_data' ) );
add_action( 'woocommerce_shipping_zone_method_added', array( $this, 'maybe_update_option_on_zone_method_added' ), 10, 3 );
add_filter( 'flexible_shipping_process_admin_options', array( $this, 'maybe_update_option_on_fs_method_saved' ) );
}
/**
* Maybe update option on FS method saved.
*
* @param array $shipping_method Shipping method.
*
* @return array
*/
public function maybe_update_option_on_fs_method_saved( array $shipping_method ) {
if ( ! $this->is_old_installation() ) {
$option_value = intval( get_option( self::OPTION_FS_METHOD_CREATED_TRACKER, '0' ) );
if ( self::FLEXIBLE_SHIPPING_METHOD_ADDED_TO_FS !== $option_value ) {
update_option( self::OPTION_FS_METHOD_CREATED_TRACKER, self::FLEXIBLE_SHIPPING_METHOD_ADDED_TO_FS );
}
}
return $shipping_method;
}
/**
* Maybe update option on zone method added action.
*
* @param int $instance_id Instance ID.
* @param string $type Type.
* @param int $zone_id Zone ID.
*/
public function maybe_update_option_on_zone_method_added( $instance_id, $type, $zone_id ) {
if ( 'flexible_shipping' === $type ) {
if ( ! $this->is_old_installation() ) {
$option_value = intval( get_option( self::OPTION_FS_METHOD_CREATED_TRACKER, '0' ) );
if ( self::NO_ACTION_WITH_FS === $option_value ) {
update_option( self::OPTION_FS_METHOD_CREATED_TRACKER, self::FLEXIBLE_SHIPPING_METHOD_ADDED_TO_ZONE );
}
}
}
}
/**
* If this a old user? If so then FS should work like always.
*
* @return bool
*/
private function is_old_installation() {
return strtotime( self::TRACK_USERS_AFTER_THIS_DATE ) > $this->activation_date_according_to_wpdesk_helper();
}
/**
* Activation date according to wpdesk helper.
*
* @return int timestamp
*/
private function activation_date_according_to_wpdesk_helper() {
$option_name = 'plugin_activation_flexible-shipping/flexible-shipping.php';
$activation_date = get_option( $option_name, current_time( 'mysql' ) );
if ( ! $activation_date ) {
return time();
}
return strtotime( $activation_date );
}
/**
* Set fs_method_created option value to data array
*
* @param array $data Data.
*
* @return array
*/
public function append_variant_id_to_data( array $data ) {
if ( ! $this->is_old_installation() ) {
if ( WPDesk_Flexible_Shipping_Tracker::is_plugin_flexible_shipping_in_data( $data ) ) {
$data['fs_method_created'] = intval( get_option( self::OPTION_FS_METHOD_CREATED_TRACKER, '0' ) );
}
}
return $data;
}
}

View File

@@ -0,0 +1,237 @@
<?php
use FSVendor\WPDesk\PluginBuilder\Plugin\Hookable;
/**
* Handle Tracker actions and filrers.
*
* Class WPDesk_Flexible_Shipping_Tracker
*/
class WPDesk_Flexible_Shipping_Tracker implements Hookable {
const PLUGIN_ACTION_LINKS_FILTER_NAME = 'plugin_action_links_flexible-shipping/flexible-shipping.php';
const FLEXIBLE_SHIPPING_PLUGIN_FILE = 'flexible-shipping/flexible-shipping.php';
const FLEXIBLE_SHIPPING_PLUGIN_SLUG = 'flexible-shipping';
const FLEXIBLE_SHIPPING_PLUGIN_TITLE = 'Flexible Shipping';
/**
* Is plugin flexible shipping in data.
*
* @param array $data Data.
* @return bool
*/
public static function is_plugin_flexible_shipping_in_data( $data ) {
return is_array( $data ) && isset( $data['plugin'] ) && self::FLEXIBLE_SHIPPING_PLUGIN_FILE === $data['plugin'];
}
/**
* Hooks.
*/
public function hooks() {
add_filter( 'wpdesk_tracker_data', array( $this, 'wpdesk_tracker_data_flexible_shipping' ), 11 );
add_filter( 'wpdesk_tracker_notice_screens', array( $this, 'wpdesk_tracker_notice_screens' ) );
add_filter( self::PLUGIN_ACTION_LINKS_FILTER_NAME, array( $this, 'plugin_action_links' ) );
}
/**
* Append data.
*
* @param array $data Data.
*
* @return array
*/
public function wpdesk_tracker_data_flexible_shipping( $data ) {
$all_shipping_methods = flexible_shipping_get_all_shipping_methods();
$flexible_shipping = $all_shipping_methods['flexible_shipping'];
$flexible_shipping_rates = $flexible_shipping->get_all_rates();
$data['flexible_shipping'] = array();
$data['flexible_shipping']['total_shipping_methods'] = 0;
$data['flexible_shipping']['avg_rules'] = 0;
$data['flexible_shipping']['max_rules'] = 0;
$data['flexible_shipping']['integrations'] = array();
$data['flexible_shipping']['free_shipping_requires'] = array();
$data['flexible_shipping']['calculation_methods'] = array();
$data['flexible_shipping']['based_on'] = array();
$data['flexible_shipping']['shipping_class_option'] = array();
$data['flexible_shipping']['method_description_count'] = 0;
$data['flexible_shipping']['free_shipping_label_count'] = 0;
$data['flexible_shipping']['max_cost_count'] = 0;
$data['flexible_shipping']['visibility_count'] = 0;
$data['flexible_shipping']['default_count'] = 0;
$data['flexible_shipping']['additional_cost_count'] = 0;
$data['flexible_shipping']['min_count'] = 0;
$data['flexible_shipping']['max_count'] = 0;
$data['flexible_shipping']['cost_per_order_count'] = 0;
$data['flexible_shipping']['stop_count'] = 0;
$data['flexible_shipping']['cancel_count'] = 0;
foreach ( $flexible_shipping_rates as $flexible_shipping_rate ) {
$data['flexible_shipping']['total_shipping_methods'] ++;
$data['flexible_shipping']['avg_rules'] += count( $flexible_shipping_rate['method_rules'] );
if ( count( $flexible_shipping_rate['method_rules'] ) > $data['flexible_shipping']['max_rules'] ) {
$data['flexible_shipping']['max_rules'] = count( $flexible_shipping_rate['method_rules'] );
}
if ( empty( $flexible_shipping_rate['method_integration'] ) ) {
$flexible_shipping_rate['method_integration'] = 'none';
}
if ( empty( $data['flexible_shipping']['integrations'][ $flexible_shipping_rate['method_integration'] ] ) ) {
$data['flexible_shipping']['integrations'][ $flexible_shipping_rate['method_integration'] ] = 0;
}
$data['flexible_shipping']['integrations'][ $flexible_shipping_rate['method_integration'] ] ++;
if ( ! empty( $flexible_shipping_rate['method_free_shipping_requires'] ) ) {
if ( empty( $data['flexible_shipping']['free_shipping_requires'][ $flexible_shipping_rate['method_free_shipping_requires'] ] ) ) {
$data['flexible_shipping']['free_shipping_requires'][ $flexible_shipping_rate['method_free_shipping_requires'] ] = 0;
}
$data['flexible_shipping']['free_shipping_requires'][ $flexible_shipping_rate['method_free_shipping_requires'] ] ++;
}
if ( empty( $data['flexible_shipping']['calculation_methods'][ $flexible_shipping_rate['method_calculation_method'] ] ) ) {
$data['flexible_shipping']['calculation_methods'][ $flexible_shipping_rate['method_calculation_method'] ] = 0;
}
$data['flexible_shipping']['calculation_methods'][ $flexible_shipping_rate['method_calculation_method'] ] ++;
if ( ! empty( $flexible_shipping_rate['method_description'] ) ) {
$data['flexible_shipping']['method_description_count'] ++;
}
if ( ! empty( $flexible_shipping_rate['method_free_shipping_label'] ) ) {
$data['flexible_shipping']['free_shipping_label_count'] ++;
}
if ( ! empty( $flexible_shipping_rate['method_max_cost'] ) ) {
$data['flexible_shipping']['max_cost_count'] ++;
}
if ( ! empty( $flexible_shipping_rate['method_visibility'] ) && 'no' !== $flexible_shipping_rate['method_visibility'] ) {
$data['flexible_shipping']['visibility_count'] ++;
}
if ( ! empty( $flexible_shipping_rate['method_default'] ) && 'no' !== $flexible_shipping_rate['method_default'] ) {
$data['flexible_shipping']['default_count'] ++;
}
foreach ( $flexible_shipping_rate['method_rules'] as $method_rule ) {
if ( empty( $data['flexible_shipping']['based_on'][ $method_rule['based_on'] ] ) ) {
$data['flexible_shipping']['based_on'][ $method_rule['based_on'] ] = 0;
}
$data['flexible_shipping']['based_on'][ $method_rule['based_on'] ] ++;
if ( ! empty( $method_rule['shipping_class'] ) ) {
$shipping_class = $method_rule['shipping_class'];
if ( ! in_array( $shipping_class, array( 'all', 'any', 'none' ), true ) ) {
$shipping_class = 'shipping_class';
}
if ( empty( $data['flexible_shipping']['shipping_class_option'][ $shipping_class ] ) ) {
$data['flexible_shipping']['shipping_class_option'][ $shipping_class ] = 0;
}
$data['flexible_shipping']['shipping_class_option'][ $shipping_class ] ++;
}
if ( ! empty( $method_rule['cost_additional'] ) ) {
$data['flexible_shipping']['additional_cost_count'] ++;
}
if ( ! empty( $method_rule['min'] ) ) {
$data['flexible_shipping']['min_count'] ++;
}
if ( ! empty( $method_rule['max'] ) ) {
$data['flexible_shipping']['max_count'] ++;
}
if ( ! empty( $method_rule['cost_per_order'] ) ) {
$data['flexible_shipping']['cost_per_order_count'] ++;
}
if ( ! empty( $method_rule['stop'] ) ) {
$data['flexible_shipping']['stop_count'] ++;
}
if ( ! empty( $method_rule['cancel'] ) ) {
$data['flexible_shipping']['cancel_count'] ++;
}
}
}
if ( 0 !== intval( $data['flexible_shipping']['total_shipping_methods'] ) ) {
$data['flexible_shipping']['avg_rules'] = $data['flexible_shipping']['avg_rules'] / $data['flexible_shipping']['total_shipping_methods'];
}
return $data;
}
/**
* Tracker notice screens.
*
* @param array $screens Screens.
*
* @return array
*/
public function wpdesk_tracker_notice_screens( $screens ) {
$current_screen = get_current_screen();
if ( 'woocommerce_page_wc-settings' === $current_screen->id ) {
if ( isset( $_GET['tab'] ) && 'shipping' === $_GET['tab'] ) {
$screens[] = $current_screen->id;
}
}
return $screens;
}
/**
* Add action links.
*
* @param array $links Links.
*
* @return array
*/
public function plugin_action_links( $links ) {
if ( ! wpdesk_tracker_enabled() || apply_filters( 'wpdesk_tracker_do_not_ask', false ) ) {
return $links;
}
$options = get_option( 'wpdesk_helper_options', array() );
if ( ! is_array( $options ) ) {
$options = array();
}
if ( empty( $options['wpdesk_tracker_agree'] ) ) {
$options['wpdesk_tracker_agree'] = '0';
}
$plugin_links = array();
if ( '0' === $options['wpdesk_tracker_agree'] ) {
$opt_in_link = admin_url( 'admin.php?page=wpdesk_tracker&plugin=' . self::FLEXIBLE_SHIPPING_PLUGIN_FILE );
$plugin_links[] = '<a href="' . $opt_in_link . '">' . __( 'Opt-in', 'flexible-shipping' ) . '</a>';
} else {
$opt_in_link = admin_url( 'plugins.php?wpdesk_tracker_opt_out=1&plugin=' . self::FLEXIBLE_SHIPPING_PLUGIN_FILE );
$plugin_links[] = '<a href="' . $opt_in_link . '">' . __( 'Opt-out', 'flexible-shipping' ) . '</a>';
}
return array_merge( $plugin_links, $links );
}
}
if ( ! function_exists( 'wpdesk_tracker_enabled' ) ) {
/**
* Disable tracker on localhost.
*
* @return bool
*/
function wpdesk_tracker_enabled() {
$tracker_enabled = true;
if ( ! empty( $_SERVER['SERVER_ADDR'] ) && '127.0.0.1' === $_SERVER['SERVER_ADDR'] ) {
$tracker_enabled = false;
}
return apply_filters( 'wpdesk_tracker_enabled', $tracker_enabled );
}
}