| 1 |
<?php |
| 2 |
/** |
| 3 |
* Public Class |
| 4 |
* |
| 5 |
* @package Wow_Plugin |
| 6 |
* @subpackage Public |
| 7 |
* @author Wow-Company <yoda@wow-company.com> |
| 8 |
* @copyright 2019 Wow-Company |
| 9 |
* @license GNU Public License |
| 10 |
* @version 1.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace bubble_menu_lite; |
| 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 |
// Add plugin style in header |
| 47 |
add_shortcode( $this->plugin['shortcode'], array( $this, 'shortcode' ) ); |
| 48 |
|
| 49 |
// old shortcode |
| 50 |
add_shortcode( 'Bubble-Menu-Pro', array( $this, 'shortcode' ) ); |
| 51 |
|
| 52 |
// Display on the site |
| 53 |
add_action( 'wp_footer', array( $this, 'display' ) ); |
| 54 |
|
| 55 |
// icons in menu |
| 56 |
add_filter( 'nav_menu_css_class', array( $this, 'nav_menu_css_class' ) ); |
| 57 |
add_filter( 'walker_nav_menu_start_el', array( $this, 'walker_nav_menu_start_el' ), 10, 4 ); |
| 58 |
|
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
/** |
| 63 |
* Display a shortcode |
| 64 |
* |
| 65 |
* @param $atts |
| 66 |
* |
| 67 |
* @return false|string |
| 68 |
*/ |
| 69 |
public function shortcode( $atts ) { |
| 70 |
extract( shortcode_atts( array( 'id' => "" ), $atts ) ); |
| 71 |
$id = absint( $atts['id'] ); |
| 72 |
|
| 73 |
global $wpdb; |
| 74 |
$table = $wpdb->prefix . 'wow_' . $this->plugin['prefix']; |
| 75 |
$sSQL = $wpdb->prepare( "select * from $table WHERE id = %d", $id ); |
| 76 |
$result = $wpdb->get_results( $sSQL, 'OBJECT_K' ); |
| 77 |
|
| 78 |
if ( empty( $result ) ) { |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
$param = unserialize( $result[ $id ]->param ); |
| 83 |
$check = $this->check( $param, $id ); |
| 84 |
|
| 85 |
if ( $check === false ) { |
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
ob_start(); |
| 90 |
include( 'partials/public.php' ); |
| 91 |
$menu = ob_get_clean(); |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
$this->include_style_script( $param, $id ); |
| 96 |
|
| 97 |
return $menu; |
| 98 |
} |
| 99 |
|
| 100 |
private function include_style_script( $param, $id ) { |
| 101 |
$slug = $this->plugin['slug']; |
| 102 |
$version = $this->plugin['version']; |
| 103 |
|
| 104 |
if ( empty( $param['disable_fontawesome'] ) ) { |
| 105 |
$url_icons = $this->plugin['url'] . 'vendors/fontawesome/css/fontawesome-all.min.css'; |
| 106 |
wp_enqueue_style( $slug . '-fontawesome', $url_icons, null, '5.15.3' ); |
| 107 |
} |
| 108 |
|
| 109 |
$pre_suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 110 |
|
| 111 |
$url_style = plugin_dir_url( __FILE__ ) . 'assets/css/style' . $pre_suffix . '.css'; |
| 112 |
wp_enqueue_style( $slug, $url_style, null, $version ); |
| 113 |
|
| 114 |
$inline_style = $this->style( $param, $id ); |
| 115 |
wp_add_inline_style( $slug, $inline_style ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Create Inline style for elements |
| 120 |
*/ |
| 121 |
public function style( $param, $id ) { |
| 122 |
$css = ''; |
| 123 |
require 'generator-style.php'; |
| 124 |
|
| 125 |
return $css; |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Display the Item on the specific pages, not via the Shortcode |
| 131 |
*/ |
| 132 |
public function go_shortcode( $id ) { |
| 133 |
echo do_shortcode( '[' . esc_attr( $this->plugin['shortcode'] ) . ' id=' . absint( $id ) . ']' ); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
/** |
| 138 |
* Display the Item on the specific pages, not via the Shortcode |
| 139 |
*/ |
| 140 |
public function display() { |
| 141 |
global $wpdb; |
| 142 |
$table = $wpdb->prefix . "wow_" . $this->plugin['prefix']; |
| 143 |
$result = $wpdb->get_results( "SELECT * FROM " . $table . " order by id asc" ); |
| 144 |
if ( count( $result ) < 1 ) { |
| 145 |
return false; |
| 146 |
} |
| 147 |
|
| 148 |
foreach ( $result as $key => $val ) { |
| 149 |
$param = unserialize( $val->param ); |
| 150 |
$display = ! empty( $param['show'] ) ? $param['show'] : 'all'; |
| 151 |
$id = $val->id; |
| 152 |
switch ( $display ) { |
| 153 |
case 'all': |
| 154 |
$this->go_shortcode( $id ); |
| 155 |
break; |
| 156 |
} |
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
private function check_status( $param ) { |
| 163 |
if ( ! empty( $param['status'] ) ) { |
| 164 |
return false; |
| 165 |
} |
| 166 |
|
| 167 |
return true; |
| 168 |
} |
| 169 |
|
| 170 |
private function check_test_mode( $param ) { |
| 171 |
if ( ! empty( $param['test_mode'] ) && ! current_user_can( 'administrator' ) ) { |
| 172 |
return false; |
| 173 |
} |
| 174 |
|
| 175 |
return true; |
| 176 |
} |
| 177 |
|
| 178 |
private function check( $param, $id ) { |
| 179 |
$check = true; |
| 180 |
$check_arr = array( |
| 181 |
'status' => $this->check_status( $param ), |
| 182 |
'test_mode' => $this->check_test_mode( $param ), |
| 183 |
); |
| 184 |
|
| 185 |
foreach ( $check_arr as $value ) { |
| 186 |
if ( $value === false ) { |
| 187 |
$check = false; |
| 188 |
break; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
return $check; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
// Icons in menu |
| 197 |
|
| 198 |
function nav_menu_css_class( $classes ) { |
| 199 |
if ( is_array( $classes ) ) { |
| 200 |
$tmp_classes = preg_grep( '/^(fa)(-\S+)?$/i', $classes ); |
| 201 |
if ( ! empty( $tmp_classes ) ) { |
| 202 |
$classes = array_values( array_diff( $classes, $tmp_classes ) ); |
| 203 |
$url_icons = $this->plugin['url'] . 'vendors/fontawesome/css/fontawesome-all.min.css'; |
| 204 |
wp_enqueue_style( $this->plugin['slug'] . '-fontawesome', $url_icons, null, '5.15.3' ); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
return $classes; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
function walker_nav_menu_start_el( $item_output, $item, $depth, $args ) { |
| 213 |
if ( is_array( $item->classes ) ) { |
| 214 |
$classes = preg_grep( '/^(fa)(-\S+)?$/i', $item->classes ); |
| 215 |
if ( ! empty( $classes ) ) { |
| 216 |
$item_output = self::replace_item( $item_output, $classes ); |
| 217 |
|
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
return $item_output; |
| 222 |
} |
| 223 |
|
| 224 |
function replace_item( $item_output, $classes ) { |
| 225 |
|
| 226 |
|
| 227 |
if ( ! in_array( 'fa', $classes ) ) { |
| 228 |
array_unshift( $classes, 'fa' ); |
| 229 |
} |
| 230 |
|
| 231 |
$before = true; |
| 232 |
if ( in_array( 'fa-after', $classes ) ) { |
| 233 |
$classes = array_values( array_diff( $classes, array( 'fa-after' ) ) ); |
| 234 |
$before = false; |
| 235 |
} |
| 236 |
|
| 237 |
$icon = '<span class="' . implode( ' ', $classes ) . ' "></span>'; |
| 238 |
|
| 239 |
preg_match( '/(<a.+>)(.+)(<\/a>)/i', $item_output, $matches ); |
| 240 |
if ( 4 === count( $matches ) ) { |
| 241 |
$item_output = $matches[1]; |
| 242 |
if ( $before ) { |
| 243 |
$item_output .= $icon . ' <span class="wow-bmp-text">' . $matches[2] . '</span>'; |
| 244 |
} else { |
| 245 |
$item_output .= '<span class="wow-bmp-text">' . $matches[2] . '</span> ' . $icon; |
| 246 |
} |
| 247 |
$item_output .= $matches[3]; |
| 248 |
} |
| 249 |
|
| 250 |
return $item_output; |
| 251 |
} |
| 252 |
} |
| 253 |
|