File: /var/www/vhosts/uyarreklam.com.tr/httpdocs/EasyDigitalDownloads.tar
Output.php 0000644 00000004514 15154042645 0006567 0 ustar 00 <?php
/**
* EasyDigitalDownloads Output class.
*
* @since 2.8.0
*
* @package OMAPI
* @author Gabriel Oliveira
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* EasyDigitalDownloads Output class.
*
* @since 2.8.0
*/
class OMAPI_EasyDigitalDownloads_Output {
/**
* The OMAPI_EasyDigitalDownloads instance.
*
* @since 2.8.0
*
* @var OMAPI_EasyDigitalDownloads
*/
public $edd;
/**
* Constructor
*
* @since 2.13.0
*
* @param OMAPI_EasyDigitalDownloads $edd
*/
public function __construct( OMAPI_EasyDigitalDownloads $edd ) {
$this->edd = $edd;
}
/**
* Returns the payload EDD needs to use in its Display Rules.
*
* @since 2.8.0
*
* @return array The
*/
public function display_rules_data() {
$cart = $this->get_cart();
$user_id = get_current_user_id();
$purchased_products = edd_get_users_purchased_products( $user_id );
$cart['customer'] = null;
if ( ! empty( $purchased_products ) ) {
$customer_products = array_map(
function ( $product ) {
return $product->ID;
},
$purchased_products
);
$cart['customer'] = array(
'products' => $customer_products,
'stats' => edd_get_purchase_stats_by_user( $user_id ),
);
}
return $cart;
}
/**
* Retrieve the cart from EDD
*
* @since 2.8.0.
*
* @return array An array of EDD cart data.
*/
public function get_cart() {
// Bail if EDD isn't currently active.
if ( ! $this->edd->is_active() ) {
return array();
}
// Check if EDD is the minimum version.
if ( ! $this->edd->is_minimum_version() ) {
return array();
}
$edd_cart = EDD()->cart;
$cart = array();
$cart['discounts'] = $edd_cart->get_discounts();
$cart['quantity'] = $edd_cart->get_quantity();
$cart['subtotal'] = $edd_cart->get_subtotal();
$cart['total'] = $edd_cart->get_total();
// Filter out items by leaving only necessary fields
$cart['items'] = array_map(
function ( $edd_item ) {
return array(
'id' => $edd_item['id'],
'quantity' => $edd_item['quantity'],
'discount' => $edd_item['discount'],
'subtotal' => $edd_item['subtotal'],
'price' => $edd_item['price'],
'item_price' => $edd_item['item_price'],
);
},
$edd_cart->get_contents_details()
);
return $cart;
}
}
RestApi.php 0000644 00000015314 15154042645 0006636 0 ustar 00 <?php
/**
* EasyDigitalDownloads API routes for usage in WP's RestApi.
*
* @since 2.8.0
*
* @author Gabriel Oliveira
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Rest Api class.
*
* @since 2.8.0
*/
class OMAPI_EasyDigitalDownloads_RestApi extends OMAPI_BaseRestApi {
/**
* The OMAPI_EasyDigitalDownloads instance.
*
* @since 2.8.0
*
* @var OMAPI_EasyDigitalDownloads
*/
public $edd;
/**
* Constructor
*
* @since 2.13.0
*
* @param OMAPI_EasyDigitalDownloads $edd
*/
public function __construct( OMAPI_EasyDigitalDownloads $edd ) {
$this->edd = $edd;
parent::__construct();
}
/**
* Registers the Rest API routes for EasyDigitalDownloads
*
* @since 2.8.0
*
* @return void
*/
public function register_rest_routes() {
register_rest_route(
$this->namespace,
'edd/autogenerate',
array(
'methods' => WP_REST_Server::CREATABLE,
'permission_callback' => array( $this, 'can_manage_shop' ),
'callback' => array( $this, 'autogenerate' ),
)
);
register_rest_route(
$this->namespace,
'edd/save',
array(
'methods' => WP_REST_Server::CREATABLE,
'permission_callback' => array( $this, 'can_update_settings' ),
'callback' => array( $this, 'save' ),
)
);
register_rest_route(
$this->namespace,
'edd/disconnect',
array(
'methods' => WP_REST_Server::CREATABLE,
'permission_callback' => array( $this, 'can_update_settings' ),
'callback' => array( $this, 'disconnect' ),
)
);
register_rest_route(
$this->namespace,
'edd/settings',
array(
'methods' => WP_REST_Server::READABLE,
'permission_callback' => array( $this, 'logged_in_and_can_access_route' ),
'callback' => array( $this, 'get_settings' ),
)
);
register_rest_route(
$this->namespace,
'edd/display-rules',
array(
'methods' => WP_REST_Server::READABLE,
'permission_callback' => '__return_true',
'callback' => array( $this, 'get_display_rules_info' ),
)
);
}
/**
* Determine if logged in user can manage the shop
*
* @since 2.8.0
*
* @param WP_REST_Request $request The REST Request.
*
* @return bool
*/
public function can_manage_shop( $request ) {
return $this->can_update_settings( $request ) && $this->edd->can_manage_shop();
}
/**
* Handles autogenerating and connecting EDD plugin with our app.
*
* Route: POST omapp/v1/edd/autogenerate
*
* @since 2.8.0
*
* @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 {
$connected = $this->edd->save->autogenerate();
if ( is_wp_error( $connected ) ) {
$e = new OMAPI_WpErrorException();
throw $e->setWpError( $connected );
}
return $this->get_settings( $request );
} catch ( Exception $e ) {
return $this->exception_to_response( $e );
}
}
/**
* Handles connect the EDD API key/token to our app.
*
* Route: POST omapp/v1/edd/save
*
* @since 2.8.0
*
* @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 {
$public_key = $request->get_param( 'publicKey' );
if ( empty( $public_key ) ) {
throw new Exception( esc_html__( 'Public Key is missing!', 'optin-monster-api' ), 400 );
}
$token = $request->get_param( 'token' );
if ( empty( $token ) ) {
throw new Exception( esc_html__( 'Token is missing!', 'optin-monster-api' ), 400 );
}
$connected = $this->edd->save->connect( $public_key, $token );
if ( is_wp_error( $connected ) ) {
$e = new OMAPI_WpErrorException();
throw $e->setWpError( $connected );
}
return $this->get_settings( $request );
} catch ( Exception $e ) {
return $this->exception_to_response( $e );
}
}
/**
* Handles disconnecting the EDD API key/token.
*
* Route: POST omapp/v1/edd/disconnect
*
* @since 2.8.0
*
* @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 {
$disconnected = $this->edd->save->disconnect();
if ( is_wp_error( $disconnected ) ) {
$e = new OMAPI_WpErrorException();
throw $e->setWpError( $disconnected );
}
return $this->get_settings( $request );
} catch ( Exception $e ) {
return $this->exception_to_response( $e );
}
}
/**
* Gets the associated EDD settings, such as is connected, key, token, etc.
*
* Route: GET omapp/v1/edd/settings
*
* @since 2.8.0
*
* @return WP_REST_Response The API Response
* @throws Exception If plugin action fails.
*/
public function get_settings() {
try {
$truncated_key = substr( $this->base->get_option( 'edd', 'key' ), 0, 8 );
$truncated_token = substr( $this->base->get_option( 'edd', 'token' ), 0, 8 );
$user_id = absint( $this->base->get_option( 'edd', 'user', 0 ) );
$path = ! $user_id || $user_id === get_current_user_id()
? 'profile.php#publickey'
: sprintf( 'user-edit.php?user_id=%d#publickey', $user_id );
$description = __( 'token', 'optin-monster-api' );
if ( $user_id ) {
$user = get_user_by( 'ID', $user_id );
if ( ! empty( $user->user_login ) ) {
$description = sprintf(
esc_html__( '%s -', 'optin-monster-api' ),
$user->user_login
);
}
}
return new WP_REST_Response(
array(
'key' => $truncated_key,
'token' => $truncated_token,
'editUrl' => esc_url_raw( admin_url( $path ) ),
'description' => $description,
'isEddConnected' => OMAPI_EasyDigitalDownloads::is_connected(),
'isEddMinimumVersion' => OMAPI_EasyDigitalDownloads::is_minimum_version(),
'currentVersion' => OMAPI_EasyDigitalDownloads::version(),
'minimumVersion' => OMAPI_EasyDigitalDownloads::MINIMUM_VERSION,
),
200
);
} catch ( Exception $e ) {
return $this->exception_to_response( $e );
}
}
/**
* Gets the necessary information for Display Rules.
* This is used when there's an event on cart page to update information in the frontend.
*
* Route: GET omapp/v1/edd/display-rules
*
* @since 2.8.0
*
* @return WP_REST_Response The API Response
* @throws Exception If plugin action fails.
*/
public function get_display_rules_info() {
$edd_output = new OMAPI_EasyDigitalDownloads_Output( $this->edd );
return new WP_REST_Response(
$edd_output->display_rules_data(),
200
);
}
}
Rules.php 0000644 00000006122 15154042645 0006356 0 ustar 00 <?php
/**
* EasyDigitalDownloads Rules class.
*
* @since 2.8.0
*
* @package OMAPI
* @author Gabriel Oliveira
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* EasyDigitalDownloads_Rules class.
*
* @since 2.8.0
*/
class OMAPI_EasyDigitalDownloads_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_edd',
'is_edd_download',
'is_edd_checkout',
'is_edd_success_page',
'is_edd_failed_transaction_page',
'is_edd_purchase_history_page',
'is_edd_download_category',
'is_edd_download_tag',
);
/**
* Check for edd rules.
*
* @since 2.8.0
*
* @throws OMAPI_Rules_False If rule doesn't match.
* @throws OMAPI_Rules_True If rule matches.
* @return void
*/
public function run_checks() {
// If EDD is not connected we can ignore the EDD specific settings.
if ( ! OMAPI_EasyDigitalDownloads::is_connected() ) {
return;
}
try {
$edd_checks = array(
'is_edd_download' => array( $this, 'is_edd_download' ),
'is_edd_checkout' => 'edd_is_checkout',
'is_edd_success_page' => 'edd_is_success_page',
'is_edd_failed_transaction_page' => 'edd_is_failed_transaction_page',
'is_edd_purchase_history_page' => 'edd_is_purchase_history_page',
);
// If show_on_edd is selected, then we'll override the field_empty check for each page.
$show_on_all_edd_pages = ! $this->rules->field_empty( 'show_on_edd' );
if ( $show_on_all_edd_pages ) {
$this->rules
->set_global_override( false )
->set_advanced_settings_field( 'show_on_edd', $this->rules->get_field_value( 'show_on_edd' ) );
}
foreach ( $edd_checks as $field => $callback ) {
// If show on all edd pages is not selected, and field is empty, then we don't need to check this.
if ( ! $show_on_all_edd_pages && $this->rules->field_empty( $field ) ) {
continue;
}
$rule_value = $this->rules->get_field_value( $field );
if ( $rule_value ) {
$this->rules
->set_global_override( false )
->set_advanced_settings_field( $field, $rule_value );
}
if ( call_user_func( $callback ) ) {
// If it passes, send it back.
throw new OMAPI_Rules_True( $field );
}
}
} catch ( OMAPI_Rules_Exception $e ) {
if ( $e instanceof OMAPI_Rules_True ) {
throw new OMAPI_Rules_True( 'include edd', 0, $e );
}
$this->rules->add_reason( $e );
}
}
/**
* Check if the current page is a valid EDD Download page.
*
* @since 2.8.0
*
* @return boolean True if current page is EDD Download, false otherwise or if it was not able to determine.
*/
public function is_edd_download() {
// Get the current page/post id.
$post_id = get_the_ID();
if ( ! $post_id || ! function_exists( 'edd_get_download' ) ) {
return false;
}
// This method only returns the download if the post id passed through
// is a valid EDD download object. Null otherwhise.
$download = edd_get_download( $post_id );
return ! empty( $download );
}
}
Save.php 0000644 00000007327 15154042645 0006172 0 ustar 00 <?php
/**
* EasyDigitalDownloads Save class.
*
* @since 2.8.0
*
* @package OMAPI
* @author Gabriel Oliveira
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* EasyDigitalDownloads Save class.
*
* @since 2.8.0
*/
class OMAPI_EasyDigitalDownloads_Save {
/**
* Holds the class object.
*
* @since 2.8.0
*
* @var OMAPI_EasyDigitalDownloads_Save
*/
public static $instance;
/**
* Holds the base class object.
*
* @since 2.8.0
*
* @var OMAPI
*/
public $base;
/**
* The OMAPI_EasyDigitalDownloads instance.
*
* @since 2.8.0
*
* @var OMAPI_EasyDigitalDownloads
*/
public $edd;
/**
* Constructor
*
* @since 2.8.0
*
* @param OMAPI_EasyDigitalDownloads $edd
*/
public function __construct( OMAPI_EasyDigitalDownloads $edd ) {
$this->edd = $edd;
$this->base = OMAPI::get_instance();
self::$instance = $this;
}
/**
* Handles auto-connecting the EDD plugin with our app. First, get public key. If not present, then generate it, and then connect.
*
* @since 2.8.0
*
* @return WP_Error|array Array with public_key, private_key and token, or WP_Error if something happened.
*/
public function autogenerate() {
if ( ! $this->edd->can_manage_shop() ) {
return new WP_Error(
'omapi-error-user-permission',
esc_html__( 'You don\'t have the required permissions to retrieve or generate API Keys for EDD.', 'optin-monster-api' )
);
}
$user_id = get_current_user_id();
// We first try retrieving the public keys for the current user
$public_key = EDD()->api->get_user_public_key( $user_id );
// If it doesn't have, then let's generate it
if ( empty( $public_key ) ) {
EDD()->api->generate_api_key( $user_id );
// If we can't retrieve for the second time, then that's an error
$public_key = EDD()->api->get_user_public_key( $user_id );
if ( empty( $public_key ) ) {
return new WP_Error(
'omapi-error-generate-edd-keys',
esc_html__( 'An error happened while generating the keys for EDD user. Try again.', 'optin-monster-api' )
);
}
}
$token = EDD()->api->get_token( $user_id );
return $this->connect( $public_key, $token, $user_id );
}
/**
* Handles connecting the EDD plugin with our app.
*
* @since 2.8.0
*
* @param string $public_key The user public key
* @param string $token The user token
* @param string $user_id The user ID
*
* @return WP_Error|array Array with public_key, private_key and token, or WP_Error if something happened.
*/
public function connect( $public_key, $token, $user_id = 0 ) {
$url = esc_url_raw( site_url() );
$payload = array(
'public_key' => $public_key,
'token' => $token,
'url' => $url,
);
$response = $this->edd->connect( $payload );
if ( is_wp_error( $response ) ) {
return $response;
}
// Set up the connected EDD options.
// We only need to save the truncated public_key, so we can output to the user.
$option = $this->base->get_option();
$option['edd'] = array(
'key' => $public_key,
'token' => $token,
'shop' => $response->id,
'user' => $user_id,
);
// Save the option.
$this->base->save->update_option( $option );
return true;
}
/**
* Handles disconnecting EDD when the disconnect button is clicked.
*
* @since 2.8.0
*
* @return WP_Error|bool True if successful, or WP_Error if something happened.
*/
public function disconnect() {
$response = $this->edd->disconnect();
// Output an error or register a successful disconnection.
if ( is_wp_error( $response ) ) {
return $response;
}
$option = $this->base->get_option();
unset( $option['edd'] );
// Save the option.
$this->base->save->update_option( $option );
return true;
}
}