| 1 |
<?php |
| 2 |
/** |
| 3 |
* Public Class |
| 4 |
* |
| 5 |
* @package Wow_Plugin |
| 6 |
* @subpackage Public |
| 7 |
* @author Dmytro Lobov <i@wpbiker.com> |
| 8 |
* @copyright 2019 Wow-Company |
| 9 |
* @license GNU Public License |
| 10 |
* @version 1.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace button_generator; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Class Wow_Plugin_Public |
| 21 |
* |
| 22 |
* @package wow_plugin |
| 23 |
* |
| 24 |
* @property array plugin - base information about the plugin |
| 25 |
* @property array url - home, pro and other URL for plugin |
| 26 |
* @property array rating - website and link for rating |
| 27 |
* @property string basedir - filesystem directory path for the plugin |
| 28 |
* @property string baseurl - URL directory path for the plugin |
| 29 |
*/ |
| 30 |
class Wow_Plugin_Public { |
| 31 |
|
| 32 |
/** |
| 33 |
* Setup to frontend of the plugin |
| 34 |
* |
| 35 |
* @param array $info general information about the plugin |
| 36 |
* |
| 37 |
* @since 1.0 |
| 38 |
*/ |
| 39 |
|
| 40 |
public function __construct( $info ) { |
| 41 |
|
| 42 |
$this->plugin = $info['plugin']; |
| 43 |
$this->url = $info['url']; |
| 44 |
$this->rating = $info['rating']; |
| 45 |
|
| 46 |
$upload = wp_upload_dir(); |
| 47 |
$this->basedir = $upload['basedir'] . '/' . $this->plugin['slug'] . '/'; |
| 48 |
$this->baseurl = $upload['baseurl'] . '/' . $this->plugin['slug'] . '/'; |
| 49 |
|
| 50 |
// Add plugin style in header |
| 51 |
//add_action( 'wp_enqueue_scripts', array( $this, 'plugin_scripts' ) ); |
| 52 |
|
| 53 |
add_shortcode( $this->plugin['shortcode'], array( $this, 'shortcode' ) ); |
| 54 |
|
| 55 |
// Display on the site |
| 56 |
add_action( 'wp_footer', array( $this, 'display' ) ); |
| 57 |
|
| 58 |
// Counter |
| 59 |
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 60 |
add_action( 'wp_ajax_btg_count', array( $this, 'btg_count' ) ); |
| 61 |
add_action( 'wp_ajax_nopriv_btg_count', array( $this, 'btg_count' ) ); |
| 62 |
} |
| 63 |
|
| 64 |
// The button view counter |
| 65 |
add_action( 'btg_counter', array( $this, 'view_counter' ) ); |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
public function plugin_scripts() { |
| 70 |
$url_style = $this->plugin['url'] . 'assets/css/style.min.css'; |
| 71 |
wp_enqueue_style( $this->plugin['slug'], $url_style, array(), $this->plugin['version'] ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Display a shortcode |
| 76 |
* |
| 77 |
* @param $atts |
| 78 |
* |
| 79 |
* @return false|string |
| 80 |
*/ |
| 81 |
public function shortcode( $atts ) { |
| 82 |
ob_start(); |
| 83 |
require plugin_dir_path( __FILE__ ) . 'shortcode.php'; |
| 84 |
$shortcode = ob_get_contents(); |
| 85 |
ob_end_clean(); |
| 86 |
|
| 87 |
return $shortcode; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Display the Item on the specific pages, not via the Shortcode |
| 92 |
*/ |
| 93 |
public function display() { |
| 94 |
require plugin_dir_path( __FILE__ ) . 'display.php'; |
| 95 |
} |
| 96 |
|
| 97 |
public function inline_style( $param, $id ) { |
| 98 |
$css = ''; |
| 99 |
require 'generator/style.php'; |
| 100 |
return $css; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Counting the number of views of the item |
| 105 |
* |
| 106 |
* @param integer $id - ID of the Item |
| 107 |
*/ |
| 108 |
public function view_counter( $id ) { |
| 109 |
$prefix = $this->plugin['prefix']; |
| 110 |
$should_count = true; |
| 111 |
$useragent = $_SERVER['HTTP_USER_AGENT']; |
| 112 |
$notbot = "Mozilla|Opera"; |
| 113 |
$bot = "Bot/|robot|Slurp/|yahoo"; |
| 114 |
if ( ! preg_match( "/$notbot/i", $useragent ) || preg_match( "!$bot!i", $useragent ) ) { |
| 115 |
$should_count = false; |
| 116 |
} |
| 117 |
if ( $should_count == true ) { |
| 118 |
$option_name = '_' . $prefix . '_view_counter_' . $id; |
| 119 |
$tool_view = get_option( $option_name, '0' ); |
| 120 |
update_option( $option_name, ( $tool_view + 1 ) ); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Reset counter and counting the action (click by button) |
| 126 |
*/ |
| 127 |
public function btg_count() { |
| 128 |
$prefix = $this->plugin['prefix']; |
| 129 |
$type = sanitize_text_field( $_POST['count_type'] ); |
| 130 |
$id = absint( $_POST['tool_id'] ); |
| 131 |
if ( $type == 'reset' ) { |
| 132 |
$option_name_view = '_' . $prefix . '_view_counter_' . $id; |
| 133 |
$option_name_action = '_' . $prefix . '_action_counter_' . $id; |
| 134 |
$delete_view = delete_option( $option_name_view ); |
| 135 |
$delete_action = delete_option( $option_name_action ); |
| 136 |
if ( $delete_view == true ) { |
| 137 |
$response = array( |
| 138 |
"result" => 'OK', |
| 139 |
); |
| 140 |
wp_send_json( $response ); |
| 141 |
} |
| 142 |
exit(); |
| 143 |
} |
| 144 |
|
| 145 |
$option_name = '_' . $prefix . '_' . $type . '_counter_' . $id; |
| 146 |
$tool_view = get_option( $option_name, '0' ); |
| 147 |
$updated = update_option( $option_name, ( $tool_view + 1 ) ); |
| 148 |
if ( true == $updated ) { |
| 149 |
$response = 'OK'; |
| 150 |
wp_send_json( $response ); |
| 151 |
} |
| 152 |
exit(); |
| 153 |
} |
| 154 |
} |
| 155 |
|