Skip to content

Commit

Permalink
change save data approach and minor improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
davodsaraei committed May 18, 2022
1 parent e028cca commit 785c9c6
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 216 deletions.
41 changes: 41 additions & 0 deletions affili-wc-tracker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* @link https://affili.ir
* @since 1.0.0
* @package AffiliWCTracker
*
* Plugin Name: Affili Plugin
* Plugin URI: https://github.com/affili-ir/wordpress
* Description: The WordPress plugin for Affili's merchants.
* Version: 2.0.0
* Author: Affili IR
* Author URI: https://affili.ir
* License: GPLv2 or later
* License URI: https://github.com/affili-ir/wordpress/blob/master/LICENSE
* Text Domain: affili_wc_tracker
* Domain Path: /languages
*/

if( !defined('ABSPATH') ) {
exit;
}

if (!defined('AFFILI_WC_TRACKER_FILE_DIR')) {
define('AFFILI_WC_TRACKER_FILE_DIR', __FILE__);
}

if (!defined('AFFILI_WC_TRACKER_PLUGIN_DIR')) {
define('AFFILI_WC_TRACKER_PLUGIN_DIR', plugin_dir_path(__FILE__));
}

if (!defined('AFFILI_WC_TRACKER_BASENAME')) {
define('AFFILI_WC_TRACKER_BASENAME', plugin_basename(AFFILI_WC_TRACKER_PLUGIN_DIR));
}

include_once __DIR__.'/classes/Action.php';
include_once __DIR__.'/classes/Installer.php';

register_activation_hook(AFFILI_WC_TRACKER_FILE_DIR, ['AffiliWCTracker\Installer', 'activation']);
register_deactivation_hook(AFFILI_WC_TRACKER_FILE_DIR, ['AffiliWCTracker\Installer', 'deactivation']);

AffiliWCTracker\Action::factory();
41 changes: 0 additions & 41 deletions affili_ir.php

This file was deleted.

7 changes: 7 additions & 0 deletions assets/css/admin-style-main.css
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,11 @@
.affili-header-inform {
font-family: inherit!important;
margin-bottom: 10px!important;
}

.w-100 {
width: 100%;
}
.min-w-100 {
min-width: 100%!important;
}
136 changes: 31 additions & 105 deletions classes/Action.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
<?php

namespace AffiliIR;
namespace AffiliWCTracker;


require_once 'Woocommerce.php';
require_once 'Installer.php';


use AffiliIR\Woocommerce as AffiliIR_Woocommerce;
use AffiliIR\Installer as AffiliIR_Installer;
use AffiliWCTracker\Woocommerce as AffiliWCTracker_Woocommerce;

class Action
{
protected $plugin_name = 'affili_ir';

private $table_name;
private $wpdb;
protected $plugin_name = 'affili_wc_tracker';

public function __construct()
{
global $wpdb;

$this->wpdb = $wpdb;
$this->table_name = $wpdb->prefix . 'affili';
//
}

public function init()
Expand Down Expand Up @@ -62,81 +55,31 @@ public function renderPage()

public function loadAdminStyles()
{
wp_enqueue_style( 'affili-ir-admin-style', plugins_url('assets/css/admin-style-main.css',__DIR__), false, '2.0.0' );
wp_enqueue_style( 'affili-wc-tracker-admin-style', plugins_url('assets/css/admin-style-main.css',__DIR__), false, '2.0.0' );

wp_enqueue_script('affili-ir-admin-script', plugins_url('assets/js/admin-script-main.js', __DIR__), array( 'jquery') );
wp_enqueue_script('affili-wc-tracker-admin-script', plugins_url('assets/js/admin-script-main.js', __DIR__), array( 'jquery') );
}

public function setAccountId()
public function setSettings()
{
$nonce = wp_verify_nonce($_POST['affili_set_account_id'], 'eadkf#adk$fawlkaawwlRRe');
$condition = isset($_POST['affili_set_account_id']) && $nonce;
$nonce = wp_verify_nonce($_POST['affili_set_settings'], 'eadkf#adk$fawlkaawwlRRe');
$condition = isset($_POST['affili_set_settings']) && $nonce;

if($condition) {
$this->createTableIfNotExists();

$account_id = sanitize_text_field($_POST['account_id']);
$data = [
'name' => 'account_id',
'value' => $account_id,
];

$account_id_model = $this->getAccountId();

if(empty($account_id_model)) {
$this->wpdb->insert($this->table_name, $data, '%s');
}else {
$this->wpdb->update($this->table_name, $data, [
'id' => $account_id_model->id
]);
}

$admin_notice = "success";
$message = __('Data saved successful.', $this->plugin_name);

$this->customRedirect($message, $admin_notice);
exit;
}
else {
wp_die(
__( 'Invalid nonce specified', $this->plugin_name ),
__( 'Error', $this->plugin_name ),
[
'response' => 403,
'back_link' => 'admin.php?page=' . $this->plugin_name,
]
);
}
}

public function setCustomCode()
{
$nonce = wp_verify_nonce($_POST['affili_custom_code'], 'eadkf#adk$fawlkrrt2RRe');
$condition = isset($_POST['affili_custom_code']) && $nonce;

if($condition) {
$custom_code = $_POST['custom_code'];

$data = [
'name' => 'custom_code',
'value' => $custom_code,
];

$custom_code_model = $this->getCustomCode();
if(empty($custom_code_model)) {
$this->wpdb->insert($this->table_name, $data, '%s');
}else {
$this->wpdb->update($this->table_name, $data, [
'id' => $custom_code_model->id
]);
}

update_option($this->plugin_name.'_account_id', $account_id);
update_option($this->plugin_name.'_custom_code', $custom_code);

$admin_notice = "success";
$message = __('Data saved successful.', $this->plugin_name);

$this->customRedirect($message, $admin_notice);
exit;
}else {
}
else {
wp_die(
__( 'Invalid nonce specified', $this->plugin_name ),
__( 'Error', $this->plugin_name ),
Expand All @@ -149,7 +92,7 @@ public function setCustomCode()
}

public function displayFlashNotices() {
$notices = get_option('affili_flash_notices', []);
$notices = get_option($this->plugin_name.'_flash_notices', []);

// Iterate through our notices to be displayed and print them.
foreach ($notices as $notice) {
Expand All @@ -162,32 +105,32 @@ public function displayFlashNotices() {

// We reset our options to prevent notices being displayed forever.
if(!empty($notices)) {
delete_option('affili_flash_notices', []);
delete_option($this->plugin_name.'_flash_notices', []);
}
}

public function setAffiliJs()
{
$script = $this->createInlineScript();

wp_enqueue_script("affili-ir-script", "https://analytics.affili.ir/scripts/affili-v2.js");
wp_add_inline_script("affili-ir-script", $script);
wp_enqueue_script("affili-wc-tracker-script", "https://analytics.affili.ir/scripts/affili-v2.js");
wp_add_inline_script("affili-wc-tracker-script", $script);
}

public function createInlineScript()
{
$model = $this->getAccountId();
$account_id = $this->getAccountId();

$script = '';

if($model) {
if($account_id) {
$script .= 'window.affiliData = window.affiliData || [];function affili(){affiliData.push(arguments);}'.PHP_EOL;
$script .= 'affili("create", "'.$model->value.'");'.PHP_EOL;
$script .= 'affili("create", "'.$account_id.'");'.PHP_EOL;
$script .= 'affili("detect");'.PHP_EOL;

$custom_code = $this->getCustomCode();
if($custom_code) {
$script .= $custom_code->value;
$script .= $custom_code;
}
}

Expand All @@ -198,7 +141,7 @@ public function trackOrders($order_id)
{
$order_id = apply_filters('woocommerce_thankyou_order_id', absint($GLOBALS['order-received']));
$order_key = apply_filters('woocommerce_thankyou_order_key', empty($_GET['key']) ? '' : wc_clean($_GET['key']));
$woocommerce = new AffiliIR_Woocommerce;
$woocommerce = new AffiliWCTracker_Woocommerce;
$order = wc_get_order($order_id);

if ($order_id <= 0) return;
Expand All @@ -219,12 +162,12 @@ public function trackOrders($order_id)

$script = "affili('conversion', '{$order_id}', '{$amount}', {$options})";

wp_add_inline_script("affili-ir-script", $script);
wp_add_inline_script("affili-wc-tracker-script", $script);
}

public function loadTextDomain()
{
$lang_dir = AFFILI_BASENAME.'/languages/';
$lang_dir = AFFILI_WC_TRACKER_BASENAME.'/languages/';
load_plugin_textdomain($this->plugin_name, false, $lang_dir);
}

Expand All @@ -235,8 +178,7 @@ public function setup()
add_action('admin_menu', [$this, 'menu']);
add_action('init', [$this, 'init']);
add_action('admin_enqueue_scripts', [$this, 'loadAdminStyles']);
add_action('admin_post_set_account_id', [$this, 'setAccountId']);
add_action('admin_post_set_custom_code', [$this, 'setCustomCode']);
add_action('admin_post_set_settings', [$this, 'setSettings']);

add_action('admin_notices', [$this, 'displayFlashNotices'], 12);
add_action('wp_head', [$this, 'setAffiliJs'] );
Expand All @@ -259,23 +201,15 @@ public static function factory()

protected function getAccountId()
{
$result = $this->wpdb->get_results(
"SELECT * FROM {$this->table_name} WHERE name = 'account_id' limit 1"
);
$result = is_array($result) ? array_pop($result) : [];

return $result;
return get_option($this->plugin_name.'_account_id');
}

protected function getCustomCode()
{
$result = $this->wpdb->get_results(
"SELECT * FROM {$this->table_name} WHERE name = 'custom_code' limit 1"
);
$result = is_array($result) ? array_pop($result) : [];
$result = get_option($this->plugin_name.'_custom_code');

if($result) {
$result->value = stripslashes($result->value);
$result = stripslashes($result);
}

return $result;
Expand All @@ -291,7 +225,7 @@ protected function customRedirect($message, $admin_notice = 'success')
}

protected function addFlashNotice($notice = '', $type = 'success', $dismissible = true ) {
$notices = get_option('affili_flash_notices', []);
$notices = get_option($this->plugin_name.'_flash_notices', []);

$dismissible_text = $dismissible ? 'is-dismissible' : '';

Expand All @@ -302,14 +236,6 @@ protected function addFlashNotice($notice = '', $type = 'success', $dismissible
]);

// We update the option with our notices array
update_option('affili_flash_notices', $notices );
}

private function createTableIfNotExists()
{
$sql = AffiliIR_Installer::sqlString();
$table_name = $this->wpdb->prefix.AffiliIR_Installer::$table;

maybe_create_table($table_name, $sql);
update_option($this->plugin_name.'_flash_notices', $notices );
}
}
Loading

0 comments on commit 785c9c6

Please sign in to comment.