File: /var/www/vhosts/uyarreklam.com.tr/httpdocs/WooCommerce.tar
Order.php 0000644 00000007755 15154215743 0006355 0 ustar 00 <?php
/**
* WooCommerce Order class.
*
* @since 2.13.8
*
* @package OMAPI
* @author Justin Sternberg
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* WooCommerce Order class.
*
* @since 2.13.8
*/
class OMAPI_WooCommerce_Order {
/**
* Holds instances of this order object.
*
* @since 2.13.8
*
* @var OMAPI_WooCommerce_Order[]
*/
protected static $instances = array();
/**
* Holds the ID of the order.
*
* @since 2.13.8
*
* @var string
*/
protected $id;
/**
* Holds the order object.
*
* @since 2.13.8
*
* @var WC_Order
*/
protected $order;
/**
* Get instance of the OMAPI_WooCommerce_Order and cache it.
*
* @since 2.13.8
*
* @param string $id The order ID.
* @param boolean $cached Whether to use the cached instance or not.
*
* @return self
*/
public static function get( $id = '', $cached = true ) {
if ( empty( $id ) ) {
return new self( $id );
}
if ( $id instanceof WC_Order || $id instanceof WC_Abstract_Order ) {
$order = $id;
$id = (int) $order->get_id();
if ( ! isset( self::$instances[ $id ] ) ) {
new self( $id );
}
self::$instances[ $id ]->set_order( $order );
} elseif ( ! empty( $id->ID ) ) {
$id = (int) $id->ID;
} else {
$id = (int) $id;
}
if ( ! $cached || ! isset( self::$instances[ $id ] ) ) {
$me = new self( $id );
$me->fetch_and_set_order();
}
return self::$instances[ $id ];
}
/**
* Class constructor.
*
* @since 2.13.8
*
* @param string $id The order ID.
*/
protected function __construct( $id = '' ) {
// If no data has been passed, don't setup anything. Maybe we are in test or create mode?
if ( empty( $id ) ) {
return;
}
// Prepare properties.
$this->id = $id;
self::$instances[ $id ] = $this;
}
/**
* Fetches the order object and sets it.
*
* @since 2.13.8
*
* @return self
*/
protected function fetch_and_set_order() {
$this->set_order( wc_get_order( $this->id ) );
return $this;
}
/**
* Sets the order object.
*
* @since 2.13.8
*
* @param WC_Order $order The order object.
*
* @return self
*/
protected function set_order( $order ) {
$this->order = $order;
return $this;
}
/**
* Checks if the order exists/is valid.
*
* @since 2.13.8
*
* @return boolean
*/
public function is() {
if ( empty( $this->order ) ) {
return false;
}
$id = $this->order->get_id();
return ! empty( $id );
}
/**
* Gets order meta, use HPOS API when possible.
*
* @since 2.13.8
*
* @param string $key Meta Key.
* @param bool $single return first found meta with key, or all with $key.
* @param string $context What the value is for. Valid values are view and edit.
* @return mixed
*/
public function get_meta( $key = '', $single = true, $context = 'edit' ) {
return ! empty( $this->order ) && method_exists( $this->order, 'get_meta' )
? $this->order->get_meta( $key, $single, $context )
: get_post_meta( $this->id, $key, $single );
}
/**
* Updates order meta, use HPOS API when possible.
*
* If using HPOS, can pass $save = false to not save the order (for bulk updates).
*
* @since 2.13.8
*
* @param string $key The meta key.
* @param mixed $value The meta value.
* @param bool $save Whether to save the order after meta update (if using HPOS).
*
* @return boolean
*/
public function update_meta_data( $key, $value, $save = true ) {
if ( ! empty( $this->order ) && method_exists( $this->order, 'update_meta_data' ) ) {
$this->order->update_meta_data( $key, $value );
return $save ? $this->order->save() : false;
}
return update_post_meta( $this->id, $key, $value );
}
/**
* Proxy calls to the order object.
*
* @since 2.13.8
*
* @param string $method The method name.
* @param array $args The method arguments.
*
* @return mixed
*/
public function __call( $method, $args ) {
return $this->order
? call_user_func_array( array( $this->order, $method ), $args )
: null;
}
}
RestApi.php 0000644 00000017776 15154215743 0006655 0 ustar 00 <?php
/**
* WooCommerce API routes for usage in WP's RestApi.
*
* @since 2.8.0
*
* @author Eduardo Nakatsuka
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Rest Api class.
*
* @since 2.8.0
*/
class OMAPI_WooCommerce_RestApi extends OMAPI_BaseRestApi {
/**
* The OMAPI_WooCommerce_Save instance.
*
* @since 2.13.0
*
* @var OMAPI_WooCommerce_Save
*/
public $save;
/**
* Constructor
*
* @since 2.13.0
*
* @param OMAPI_WooCommerce_Save $save
*/
public function __construct( OMAPI_WooCommerce_Save $save ) {
$this->save = $save;
parent::__construct();
}
/**
* Registers the Rest API routes for WooCommerce
*
* @since 2.8.0
*
* @return void
*/
public function register_rest_routes() {
register_rest_route(
$this->namespace,
'woocommerce/autogenerate',
array(
'methods' => WP_REST_Server::CREATABLE,
'permission_callback' => array( $this, 'can_update_settings' ),
'callback' => array( $this, 'autogenerate' ),
)
);
register_rest_route(
$this->namespace,
'woocommerce/save',
array(
'methods' => WP_REST_Server::CREATABLE,
'permission_callback' => array( $this, 'can_update_settings' ),
'callback' => array( $this, 'save' ),
)
);
register_rest_route(
$this->namespace,
'woocommerce/disconnect',
array(
'methods' => WP_REST_Server::CREATABLE,
'permission_callback' => array( $this, 'can_update_settings' ),
'callback' => array( $this, 'disconnect' ),
)
);
register_rest_route(
$this->namespace,
'woocommerce/key',
array(
'methods' => WP_REST_Server::READABLE,
'permission_callback' => array( $this, 'logged_in_and_can_access_route' ),
'callback' => array( $this, 'get_key' ),
)
);
register_rest_route(
$this->namespace,
'woocommerce/display-rules',
array(
'methods' => WP_REST_Server::READABLE,
'permission_callback' => '__return_true',
'callback' => array( $this, 'get_display_rules_info' ),
)
);
}
/**
* Handles auto-generating the WooCommerce API key/secret.
*
* Route: POST omapp/v1/woocommerce/autogenerate
*
* @since 2.0.0
* @since 2.8.0 Migrated from OMAPI_RestApi woocommerce_autogenerate
*
* @param WP_REST_Request $request The REST Request.
*
* @return WP_REST_Response The API Response
* @throws Exception If plugin action fails.
*/
public function autogenerate( $request ) {
try {
$auto_generated_keys = $this->save->autogenerate();
if ( is_wp_error( $auto_generated_keys ) ) {
$e = new OMAPI_WpErrorException();
throw $e->setWpError( $auto_generated_keys );
}
if ( empty( $auto_generated_keys ) ) {
throw new Exception( esc_html__( 'WooCommerce REST API keys could not be auto-generated on your behalf. Please try again.', 'optin-monster-api' ), 400 );
}
$data = $this->base->get_option();
// Merge data array, with auto-generated keys array.
$data = array_merge( $data, $auto_generated_keys );
$this->save->connect( $data );
if ( ! empty( $this->save->error ) ) {
throw new Exception( $this->save->error, 400 );
}
return $this->get_key( $request );
} catch ( Exception $e ) {
return $this->exception_to_response( $e );
}
}
/**
* Handles saving the WooCommerce API key/secret.
*
* Route: POST omapp/v1/woocommerce/save
*
* @since 2.0.0
* @since 2.8.0 Migrated from OMAPI_RestApi woocommerce_save
*
* @param WP_REST_Request $request The REST Request.
*
* @return WP_REST_Response The API Response
* @throws Exception If plugin action fails.
*/
public function save( $request ) {
try {
$woo_key = $request->get_param( 'key' );
if ( empty( $woo_key ) ) {
throw new Exception( esc_html__( 'Consumer key missing!', 'optin-monster-api' ), 400 );
}
$woo_secret = $request->get_param( 'secret' );
if ( empty( $woo_secret ) ) {
throw new Exception( esc_html__( 'Consumer secret missing!', 'optin-monster-api' ), 400 );
}
$data = array(
'consumer_key' => $woo_key,
'consumer_secret' => $woo_secret,
);
$this->save->connect( $data );
if ( ! empty( $this->save->error ) ) {
throw new Exception( $this->save->error, 400 );
}
return $this->get_key( $request );
} catch ( Exception $e ) {
return $this->exception_to_response( $e );
}
}
/**
* Handles disconnecting the WooCommerce API key/secret.
*
* Route: POST omapp/v1/woocommerce/disconnect
*
* @since 2.0.0
* @since 2.8.0 Migrated from OMAPI_RestApi woocommerce_disconnect
*
* @param WP_REST_Request $request The REST Request.
*
* @return WP_REST_Response The API Response
* @throws Exception If plugin action fails.
*/
public function disconnect( $request ) {
try {
$this->save->disconnect( array() );
if ( ! empty( $this->save->error ) ) {
throw new Exception( $this->save->error, 400 );
}
return new WP_REST_Response(
array( 'message' => esc_html__( 'OK', 'optin-monster-api' ) ),
200
);
} catch ( Exception $e ) {
return $this->exception_to_response( $e );
}
}
/**
* Gets the associated WooCommerce API key data.
*
* Route: GET omapp/v1/woocommerce/key
*
* @since 2.0.0
* @since 2.8.0 Migrated from OMAPI_RestApi woocommerce_get_key
*
* @param WP_REST_Request $request The REST Request.
*
* @return WP_REST_Response The API Response
* @throws Exception If plugin action fails.
*/
public function get_key( $request ) {
try {
$keys_tab = OMAPI_WooCommerce::version_compare( '3.4.0' ) ? 'advanced' : 'api';
$keys_admin_url = admin_url( "admin.php?page=wc-settings&tab={$keys_tab}§ion=keys" );
if ( ! OMAPI_WooCommerce::is_minimum_version() && OMAPI_WooCommerce::is_connected() ) {
$error = '<p>' . esc_html( sprintf( __( 'OptinMonster requires WooCommerce %s or above.', 'optin-monster-api' ), OMAPI_WooCommerce::MINIMUM_VERSION ) ) . '</p>'
. '<p>' . esc_html_x( 'This site is currently running: ', 'the current version of WooCommerce: "WooCommerce x.y.z"', 'optin-monster-api' )
. '<code>WooCommerce ' . esc_html( OMAPI_WooCommerce::version() ) . '</code>.</p>'
. '<p>' . esc_html__( 'Please upgrade to the latest version of WooCommerce to enjoy deeper integration with OptinMonster.', 'optin-monster-api' ) . '</p>';
throw new Exception( $error, 404 );
}
if ( ! OMAPI_WooCommerce::is_connected() ) {
$error = '<p>' . sprintf( __( 'In order to integrate WooCommerce with the Display Rules in the campaign builder, OptinMonster needs <a href="%s" target="_blank">WooCommerce REST API credentials</a>. OptinMonster only needs Read access permissions to work.', 'optin-monster-api' ), esc_url( $keys_admin_url ) ) . '</p>';
throw new Exception( $error, 404 );
}
// Set some default key details.
$defaults = array(
'key_id' => '',
'description' => esc_html__( 'no description found', 'optin-monster-api' ),
'truncated_key' => esc_html__( 'no truncated key found', 'optin-monster-api' ),
);
// Get the key details.
$key_id = $this->base->get_option( 'woocommerce', 'key_id' );
$details = OMAPI_WooCommerce::get_key_details_by_id( $key_id );
$r = wp_parse_args( array_filter( $details ), $defaults );
return new WP_REST_Response(
array(
'id' => $key_id,
'description' => esc_html( $r['description'] ),
'truncated' => esc_html( $r['truncated_key'] ),
'editUrl' => esc_url_raw( add_query_arg( 'edit-key', $r['key_id'], $keys_admin_url ) ),
),
200
);
} catch ( Exception $e ) {
return $this->exception_to_response( $e );
}
}
/**
* Retrieves the WooCommerce cart data for display rules.
*
* Route: GET omapp/v1/woocommerce/display-rules
*
* @since 2.12.0
*
* @param WP_REST_Request $request The REST Request.
*
* @return WP_REST_Response The API Response
*/
public function get_display_rules_info( $request ) {
return new WP_REST_Response(
$this->base->woocommerce->get_cart(),
200
);
}
}
Rules.php 0000644 00000007663 15154215743 0006372 0 ustar 00 <?php
/**
* WooCommerce Rules class.
*
* @since 2.8.0
*
* @package OMAPI
* @author Gabriel Oliveira
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* WooCommerce_Rules class.
*
* @since 2.8.0
*/
class OMAPI_WooCommerce_Rules extends OMAPI_Rules_Base {
/**
* Holds the meta fields used for checking output statuses.
*
* @since 2.8.0
*
* @var array
*/
protected $fields = array(
'show_on_woocommerce',
'is_wc_shop',
'is_wc_product',
'is_wc_cart',
'is_wc_checkout',
'is_wc_account',
'is_wc_endpoint',
'is_wc_endpoint_order_pay',
'is_wc_endpoint_order_received',
'is_wc_endpoint_view_order',
'is_wc_endpoint_edit_account',
'is_wc_endpoint_edit_address',
'is_wc_endpoint_lost_password',
'is_wc_endpoint_customer_logout',
'is_wc_endpoint_add_payment_method',
'is_wc_product_category',
'is_wc_product_tag',
);
/**
* Check for woocommerce rules.
*
* @since 1.5.0
* @since 2.8.0 Migrated from OMAPI_Rules
*
* @throws OMAPI_Rules_False|OMAPI_Rules_True
* @return void
*/
public function run_checks() {
// If WooCommerce is not connected we can ignore the WooCommerce specific settings.
if ( ! OMAPI_WooCommerce::is_connected() ) {
return;
}
if (
! $this->rules->is_inline_check
// Separate never checks for WooCommerce pages that don't ID match
// No global check on purpose. Global is still true if only this setting is populated.
&& $this->rules->item_in_field( wc_get_page_id( 'shop' ), 'never' )
&& is_shop()
) {
throw new OMAPI_Rules_False( 'never on wc is_shop' );
}
try {
$this->check_fields();
} catch ( OMAPI_Rules_Exception $e ) {
if ( $e instanceof OMAPI_Rules_True ) {
throw new OMAPI_Rules_True( 'include woocommerce', 0, $e );
}
$this->rules->add_reason( $e );
}
}
/**
* Check for woocommerce rule fields.
*
* @since 1.5.0
* @since 2.8.0 Migrated from OMAPI_Rules
*
* @throws OMAPI_Rules_True
* @return void
*/
protected function check_fields() {
$wc_checks = array(
'show_on_woocommerce' => array( 'is_woocommerce' ), // is woocommerce anything
'is_wc_shop' => array( 'is_shop' ),
'is_wc_product' => array( 'is_product' ),
'is_wc_cart' => array( 'is_cart' ),
'is_wc_checkout' => array( 'is_checkout' ),
'is_wc_account' => array( 'is_account_page' ),
'is_wc_endpoint' => array( 'is_wc_endpoint_url' ),
'is_wc_endpoint_order_pay' => array( 'is_wc_endpoint_url', 'order-pay' ),
'is_wc_endpoint_order_received' => array( 'is_wc_endpoint_url', 'order-received' ),
'is_wc_endpoint_view_order' => array( 'is_wc_endpoint_url', 'view-order' ),
'is_wc_endpoint_edit_account' => array( 'is_wc_endpoint_url', 'edit-account' ),
'is_wc_endpoint_edit_address' => array( 'is_wc_endpoint_url', 'edit-address' ),
'is_wc_endpoint_lost_password' => array( 'is_wc_endpoint_url', 'lost-password' ),
'is_wc_endpoint_customer_logout' => array( 'is_wc_endpoint_url', 'customer-logout' ),
'is_wc_endpoint_add_payment_method' => array( 'is_wc_endpoint_url', 'add-payment-method' ),
);
foreach ( $wc_checks as $field => $callback ) {
$this->check_field( $field, $callback );
}
}
/**
* Check for woocommerce rule field.
*
* @since 2.10.0
*
* @param string $field The field to check.
* @param array $callback The callback to check.
*
* @return void
* @throws OMAPI_Rules_True
*/
protected function check_field( $field, $callback ) {
if ( $this->rules->field_empty( $field ) ) {
return;
}
$this->rules
->set_global_override( false )
->set_advanced_settings_field( $field, $this->rules->get_field_value( $field ) );
if ( call_user_func_array( array_shift( $callback ), $callback ) ) {
// If it passes, send it back.
throw new OMAPI_Rules_True( $field );
}
}
}
Save.php 0000644 00000012200 15154215743 0006155 0 ustar 00 <?php
/**
* WooCommerce Save class.
*
* @since 2.8.0
*
* @package OMAPI
* @author Gabriel Oliveira and Eduardo Nakatsuka
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* WooCommerce Save class.
*
* @since 2.8.0
*/
class OMAPI_WooCommerce_Save {
/**
* Holds the class object.
*
* @since 2.8.0
*
* @var OMAPI_WooCommerce_Save
*/
public static $instance;
/**
* Holds save error.
*
* @since 2.8.0
*
* @var mixed
*/
public $error = null;
/**
* Holds the base class object.
*
* @since 2.8.0
*
* @var OMAPI
*/
public $base;
/**
* The OMAPI_WooCommerce instance.
*
* @since 2.13.0
*
* @var OMAPI_WooCommerce
*/
public $woo;
/**
* Primary class constructor.
*
* @since 2.8.0
*
* @param OMAPI_WooCommerce $woo
*/
public function __construct( OMAPI_WooCommerce $woo ) {
$this->woo = $woo;
$this->base = OMAPI::get_instance();
self::$instance = $this;
}
/**
* Handles auto-generating WooCommerce API keys for use with OM.
*
* @since 1.7.0
* @since 2.8.0 Migrated from OMAPI_Save woocommerce_autogenerate
*
* @return array
*/
public function autogenerate() {
$cookies = array();
foreach ( $_COOKIE as $name => $val ) {
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
$cookies[] = "$name=" . rawurlencode( is_array( $val ) ? serialize( $val ) : $val );
}
$cookies = implode( '; ', $cookies );
$request_args = array(
'sslverify' => apply_filters( 'https_local_ssl_verify', true ), // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
'body' => array(
'action' => 'woocommerce_update_api_key',
'description' => esc_html__( 'OptinMonster API Read-Access (Auto-Generated)', 'optin-monster-api' ),
'permissions' => 'read',
'user' => get_current_user_id(),
'security' => wp_create_nonce( 'update-api-key' ),
),
'timeout' => 60,
'headers' => array(
'cookie' => $cookies,
),
);
$response = wp_remote_post( admin_url( 'admin-ajax.php' ), $request_args );
if ( is_wp_error( $response ) ) {
return $response;
}
$code = wp_remote_retrieve_response_code( $response );
$body = json_decode( wp_remote_retrieve_body( $response ) );
if (
200 === intval( $code )
&& ! empty( $body->success )
&& ! empty( $body->data->consumer_key )
&& ! empty( $body->data->consumer_secret )
) {
return (array) $body->data;
}
return array();
}
/**
* Handles connecting WooCommerce when the connect button is clicked.
*
* @since 1.7.0
* @since 2.8.0 Migrated from OMAPI_Save woocommerce_connect
*
* @param array $data The data passed in via POST request.
*
* @return void
*/
public function connect( $data ) {
$keys = $this->woo->validate_keys( $data );
if ( isset( $keys['error'] ) ) {
$this->error = $keys['error'];
} else {
// Get the version of the REST API we should use. The
// `v3` route wasn't added until WooCommerce 3.5.0.
$api_version = $this->woo->version_compare( '3.5.0' )
? 'v3'
: 'v2';
// Get current site url.
$url = esc_url_raw( site_url() );
// Make a connection request.
$response = $this->woo->connect(
array(
'consumerKey' => $keys['consumer_key'],
'consumerSecret' => $keys['consumer_secret'],
'apiVersion' => $api_version,
'shop' => $url,
'name' => esc_html( get_bloginfo( 'name' ) ),
)
);
// Output an error or register a successful connection.
if ( is_wp_error( $response ) ) {
$this->error = isset( $response->message )
? $response->message
: esc_html__( 'WooCommerce could not be connected to OptinMonster. The OptinMonster API returned with the following response: ', 'optin-monster-api' ) . $response->get_error_message();
} else {
// Get the shop hostname.
$site = wp_parse_url( $url );
$host = isset( $site['host'] ) ? $site['host'] : '';
// Set up the connected WooCommerce options.
$option = $this->base->get_option();
$option['woocommerce'] = array(
'api_version' => $api_version,
'key_id' => $keys['key_id'],
'shop' => $host,
);
// Save the option.
$this->base->save->update_option( $option, $data );
}
}
}
/**
* Handles disconnecting WooCommerce when the disconnect button is clicked.
*
* @since 1.7.0
* @since 2.8.0 Migrated from OMAPI_Save woocommerce_disconnect
*
* @param array $data The data passed in via POST request.
*
* @return void
*/
public function disconnect( $data ) {
$response = $this->woo->disconnect();
// Output an error or register a successful disconnection.
if ( is_wp_error( $response ) ) {
$this->error = isset( $response->message )
? $response->message
: esc_html__( 'WooCommerce could not be disconnected from OptinMonster. The OptinMonster API returned with the following response: ', 'optin-monster-api' ) . $response->get_error_message();
} else {
$option = $this->base->get_option();
unset( $option['woocommerce'] );
// Save the option.
$this->base->save->update_option( $option, $data );
}
}
}