| 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 floatingbutton; |
| 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 |
// icons in menu |
| 68 |
add_filter( 'nav_menu_css_class', array($this, 'nav_menu_css_class') ); |
| 69 |
add_filter( 'walker_nav_menu_start_el', array($this, 'walker_nav_menu_start_el'), 10, 4 ); |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
public function plugin_scripts() { |
| 74 |
$url_style = $this->plugin['url'] . 'assets/css/style.min.css'; |
| 75 |
wp_enqueue_style( $this->plugin['slug'], $url_style, array(), $this->plugin['version'] ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Display a shortcode |
| 80 |
* |
| 81 |
* @param $atts |
| 82 |
* |
| 83 |
* @return false|string |
| 84 |
*/ |
| 85 |
public function shortcode( $atts ) { |
| 86 |
ob_start(); |
| 87 |
require plugin_dir_path( __FILE__ ) . 'shortcode.php'; |
| 88 |
$shortcode = ob_get_contents(); |
| 89 |
ob_end_clean(); |
| 90 |
|
| 91 |
return $shortcode; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Display the Item on the specific pages, not via the Shortcode |
| 96 |
*/ |
| 97 |
public function display() { |
| 98 |
require plugin_dir_path( __FILE__ ) . 'display.php'; |
| 99 |
} |
| 100 |
|
| 101 |
/* |
| 102 |
* Return the array of the submenu |
| 103 |
*/ |
| 104 |
static function sub_menu_array( $var ) { |
| 105 |
return(!empty( $var) ); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Counting the number of views of the item |
| 110 |
* |
| 111 |
* @param integer $id - ID of the Item |
| 112 |
*/ |
| 113 |
public function view_counter( $id ) { |
| 114 |
$prefix = $this->plugin['prefix']; |
| 115 |
$should_count = true; |
| 116 |
$useragent = $_SERVER['HTTP_USER_AGENT']; |
| 117 |
$notbot = "Mozilla|Opera"; |
| 118 |
$bot = "Bot/|robot|Slurp/|yahoo"; |
| 119 |
if ( ! preg_match( "/$notbot/i", $useragent ) || preg_match( "!$bot!i", $useragent ) ) { |
| 120 |
$should_count = false; |
| 121 |
} |
| 122 |
if ( $should_count == true ) { |
| 123 |
$option_name = '_' . $prefix . '_view_counter_' . $id; |
| 124 |
$tool_view = get_option( $option_name, '0' ); |
| 125 |
update_option( $option_name, ( $tool_view + 1 ) ); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Reset counter and counting the action (click by button) |
| 131 |
*/ |
| 132 |
public function btg_count() { |
| 133 |
$prefix = $this->plugin['prefix']; |
| 134 |
$type = sanitize_text_field( $_POST['count_type'] ); |
| 135 |
$id = absint( $_POST['tool_id'] ); |
| 136 |
if ( $type == 'reset' ) { |
| 137 |
$option_name_view = '_' . $prefix . '_view_counter_' . $id; |
| 138 |
$option_name_action = '_' . $prefix . '_action_counter_' . $id; |
| 139 |
$delete_view = delete_option( $option_name_view ); |
| 140 |
$delete_action = delete_option( $option_name_action ); |
| 141 |
if ( $delete_view == true ) { |
| 142 |
$response = array( |
| 143 |
"result" => 'OK', |
| 144 |
); |
| 145 |
wp_send_json( $response ); |
| 146 |
} |
| 147 |
exit(); |
| 148 |
} |
| 149 |
|
| 150 |
$option_name = '_' . $prefix . '_' . $type . '_counter_' . $id; |
| 151 |
$tool_view = get_option( $option_name, '0' ); |
| 152 |
$updated = update_option( $option_name, ( $tool_view + 1 ) ); |
| 153 |
if ( true == $updated ) { |
| 154 |
$response = 'OK'; |
| 155 |
wp_send_json( $response ); |
| 156 |
} |
| 157 |
exit(); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
// Icons in menu |
| 162 |
|
| 163 |
function nav_menu_css_class( $classes ){ |
| 164 |
if( is_array( $classes ) ){ |
| 165 |
$tmp_classes = preg_grep( '/^(fa)(-\S+)?$/i', $classes ); |
| 166 |
if( !empty( $tmp_classes ) ){ |
| 167 |
$classes = array_values( array_diff( $classes, $tmp_classes ) ); |
| 168 |
} |
| 169 |
} |
| 170 |
return $classes; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
function walker_nav_menu_start_el( $item_output, $item, $depth, $args ){ |
| 175 |
if( is_array( $item->classes ) ){ |
| 176 |
$classes = preg_grep( '/^(fa)(-\S+)?$/i', $item->classes ); |
| 177 |
if( !empty( $classes ) ){ |
| 178 |
$item_output = self::replace_item( $item_output, $classes ); |
| 179 |
} |
| 180 |
} |
| 181 |
return $item_output; |
| 182 |
} |
| 183 |
|
| 184 |
function replace_item( $item_output, $classes ){ |
| 185 |
|
| 186 |
|
| 187 |
if( !in_array( 'fa', $classes ) ){ |
| 188 |
array_unshift( $classes, 'fa' ); |
| 189 |
} |
| 190 |
|
| 191 |
$before = true; |
| 192 |
if( in_array( 'fa-after', $classes ) ){ |
| 193 |
$classes = array_values( array_diff( $classes, array( 'fa-after' ) ) ); |
| 194 |
$before = false; |
| 195 |
} |
| 196 |
|
| 197 |
$icon = '<span class="' . implode( ' ', $classes ) . ' "></span>'; |
| 198 |
|
| 199 |
preg_match( '/(<a.+>)(.+)(<\/a>)/i', $item_output, $matches ); |
| 200 |
if( 4 === count( $matches ) ){ |
| 201 |
$item_output = $matches[1]; |
| 202 |
if( $before ){ |
| 203 |
$item_output .= $icon . ' <span class="wow-bmp-text">' . $matches[2] . '</span>'; |
| 204 |
} else { |
| 205 |
$item_output .= '<span class="wow-bmp-text">' . $matches[2] . '</span> ' . $icon; |
| 206 |
} |
| 207 |
$item_output .= $matches[3]; |
| 208 |
} |
| 209 |
return $item_output; |
| 210 |
} |
| 211 |
} |
| 212 |
|