| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* SOFTWARE LICENSE INFORMATION |
| 5 |
* |
| 6 |
* Copyright (c) 2017 Buttonizer, all rights reserved. |
| 7 |
* |
| 8 |
* This file is part of Buttonizer |
| 9 |
* |
| 10 |
* For detailed information regarding to the licensing of |
| 11 |
* this software, please review the license.txt or visit: |
| 12 |
* https://buttonizer.pro/license/ |
| 13 |
*/ |
| 14 |
namespace Buttonizer\Legacy\Frontend; |
| 15 |
|
| 16 |
use Buttonizer\Core\Utils\PermissionCheck; |
| 17 |
class Ajax { |
| 18 |
private $in_preview = false; |
| 19 |
|
| 20 |
private $is_admin = false; |
| 21 |
|
| 22 |
private $page_data = []; |
| 23 |
|
| 24 |
private $settings = []; |
| 25 |
|
| 26 |
/** |
| 27 |
* Ajax constructor. |
| 28 |
*/ |
| 29 |
public function __construct() { |
| 30 |
add_action( 'wp_enqueue_scripts', [$this, 'frontend'] ); |
| 31 |
add_action( "wp_ajax_buttonizer", [$this, 'frontendJson'] ); |
| 32 |
add_action( "wp_ajax_nopriv_buttonizer", [$this, 'frontendJson'] ); |
| 33 |
add_filter( |
| 34 |
'style_loader_tag', |
| 35 |
[$this, 'fixPremiumIconLibraries'], |
| 36 |
10, |
| 37 |
2 |
| 38 |
); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Start registering |
| 43 |
*/ |
| 44 |
public function frontend() { |
| 45 |
// Buttonizer settings |
| 46 |
$this->loadSettings(); |
| 47 |
// Add frontend assets |
| 48 |
$this->frontendAssets(); |
| 49 |
// Into preview |
| 50 |
if ( PermissionCheck::hasPermission() && isset( $_GET['buttonizer-preview'] ) ) { |
| 51 |
$this->goIntoPreview(); |
| 52 |
// Load page data |
| 53 |
$this->pageData(); |
| 54 |
} else { |
| 55 |
// Show the button without delay |
| 56 |
if ( isset( $this->settings['no_ajax'] ) && $this->settings['no_ajax'] === true ) { |
| 57 |
wp_localize_script( "buttonizer_frontend_javascript", "buttonizer_data", ( new \Buttonizer\Legacy\Api\Buttons\ApiButtons() )->get( true ) ); |
| 58 |
} else { |
| 59 |
// Load page data |
| 60 |
$this->pageData(); |
| 61 |
} |
| 62 |
} |
| 63 |
// Add some information |
| 64 |
wp_localize_script( 'buttonizer_frontend_javascript', 'buttonizer_ajax', [ |
| 65 |
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 66 |
'version' => BUTTONIZER_VERSION, |
| 67 |
'buttonizer_path' => plugins_url( '', BUTTONIZER_PLUGIN_DIR ), |
| 68 |
'buttonizer_assets' => plugins_url( '/assets/legacy/', BUTTONIZER_PLUGIN_DIR ), |
| 69 |
'base_url' => get_site_url( '/' ), |
| 70 |
'current' => $this->page_data, |
| 71 |
'in_preview' => $this->in_preview, |
| 72 |
'is_admin' => PermissionCheck::hasPermission(), |
| 73 |
'cache' => ( isset( $this->settings['cache_code'] ) ? $this->settings['cache_code'] : md5( 'buzzing_the_cache_code' ) ), |
| 74 |
'enable_ga_clicks' => ( isset( $this->settings['google_analytics_enabled'] ) ? filter_var( $this->settings['google_analytics_enabled'], FILTER_VALIDATE_BOOLEAN, [ |
| 75 |
'options' => [ |
| 76 |
'default' => true, |
| 77 |
], |
| 78 |
] ) : true ), |
| 79 |
] ); |
| 80 |
// Add Google Analytics |
| 81 |
if ( isset( $this->settings['google_analytics'] ) && !empty( $this->settings['google_analytics'] ) ) { |
| 82 |
wp_register_script( |
| 83 |
'google_analytics', |
| 84 |
'https://www.googletagmanager.com/gtag/js?id=' . $this->settings['google_analytics'], |
| 85 |
array(), |
| 86 |
false, |
| 87 |
true |
| 88 |
); |
| 89 |
wp_enqueue_script( 'google_analytics' ); |
| 90 |
wp_add_inline_script( 'google_analytics', "\n window.dataLayer = window.dataLayer || [];\n function gtag(){dataLayer.push(arguments);}\n gtag('js', new Date());\n\n gtag('config', '" . esc_js( $this->settings['google_analytics'] ) . "');" ); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Go into preview mode. Add to all links the 'buttonizer-preview' link |
| 96 |
*/ |
| 97 |
private function goIntoPreview() { |
| 98 |
// Buttonizer in preview |
| 99 |
$this->in_preview = true; |
| 100 |
// Hide admin panel |
| 101 |
show_admin_bar( false ); |
| 102 |
// Filters |
| 103 |
$filters = [ |
| 104 |
'post_link', |
| 105 |
'page_link', |
| 106 |
'author_link', |
| 107 |
'archive_link', |
| 108 |
'category_link', |
| 109 |
'category_feed_link', |
| 110 |
'attachment_link', |
| 111 |
'post_type_link', |
| 112 |
'day_link', |
| 113 |
'month_link', |
| 114 |
'year_link', |
| 115 |
'post_type_link' |
| 116 |
]; |
| 117 |
foreach ( $filters as $filter ) { |
| 118 |
add_filter( |
| 119 |
$filter, |
| 120 |
[$this, 'updatePreviewLinks'], |
| 121 |
10, |
| 122 |
3 |
| 123 |
); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Load Buttonizer settings |
| 129 |
*/ |
| 130 |
private function loadSettings() { |
| 131 |
// Buttonizer settings |
| 132 |
$this->settings = get_option( "buttonizer_settings" ); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Update link |
| 137 |
* |
| 138 |
* @param $permalink |
| 139 |
* @return mixed |
| 140 |
*/ |
| 141 |
public function updatePreviewLinks( $permalink ) { |
| 142 |
return esc_url( add_query_arg( [ |
| 143 |
'buttonizer-preview' => 1, |
| 144 |
], $permalink ) ); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Set page data |
| 149 |
*/ |
| 150 |
private function pageData() { |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Get all categories |
| 155 |
* |
| 156 |
* @return array |
| 157 |
*/ |
| 158 |
private function getCategories() { |
| 159 |
$categories = []; |
| 160 |
return $categories; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Import frontend style and script |
| 165 |
*/ |
| 166 |
private function frontendAssets() { |
| 167 |
// Import Polyfills |
| 168 |
wp_register_script( |
| 169 |
'buttonizer_frontend_polyfills', |
| 170 |
plugins_url( '/assets/legacy/polyfills.min.js?v=' . md5( BUTTONIZER_VERSION ), BUTTONIZER_PLUGIN_DIR ), |
| 171 |
[], |
| 172 |
false, |
| 173 |
true |
| 174 |
); |
| 175 |
wp_register_script( |
| 176 |
'buttonizer_frontend_javascript', |
| 177 |
plugins_url( '/assets/legacy/frontend.min.js?v=' . md5( BUTTONIZER_VERSION ), BUTTONIZER_PLUGIN_DIR ), |
| 178 |
[], |
| 179 |
false, |
| 180 |
true |
| 181 |
); |
| 182 |
wp_register_style( |
| 183 |
'buttonizer_frontend_style', |
| 184 |
plugins_url( '/assets/legacy/frontend.css', BUTTONIZER_PLUGIN_DIR ) . '?v=' . md5( BUTTONIZER_VERSION ), |
| 185 |
array(), |
| 186 |
false, |
| 187 |
'all' |
| 188 |
); |
| 189 |
//is IE 11 or below |
| 190 |
if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && (preg_match( '~MSIE|Internet Explorer~i', $_SERVER['HTTP_USER_AGENT'] ) || strpos( $_SERVER['HTTP_USER_AGENT'], 'Trident/7.0; rv:11.0' ) !== false) ) { |
| 191 |
wp_enqueue_script( 'buttonizer_frontend_polyfills' ); |
| 192 |
} |
| 193 |
// Import script & style |
| 194 |
wp_enqueue_script( 'buttonizer_frontend_javascript' ); |
| 195 |
wp_enqueue_style( 'buttonizer_frontend_style' ); |
| 196 |
// Import icon library |
| 197 |
$this->importIconLibrary(); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Icon library import |
| 202 |
*/ |
| 203 |
private function importIconLibrary() { |
| 204 |
if ( !isset( $this->settings["import_icon_library"] ) ) { |
| 205 |
$this->settings["import_icon_library"] = false; |
| 206 |
} |
| 207 |
// False by default |
| 208 |
if ( !isset( $this->settings["icon_library"] ) ) { |
| 209 |
$this->settings["icon_library"] = 'fontawesome'; |
| 210 |
} |
| 211 |
if ( !isset( $this->settings["icon_library_version"] ) ) { |
| 212 |
$this->settings["icon_library_version"] = '5.free'; |
| 213 |
} |
| 214 |
if ( filter_var( $this->settings['import_icon_library'], FILTER_VALIDATE_BOOLEAN ) === true ) { |
| 215 |
if ( $this->settings["icon_library_version"] === '5.free' ) { |
| 216 |
wp_register_style( |
| 217 |
'buttonizer-icon-library', |
| 218 |
'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/' . FONTAWESOME_CURRENT_VERSION . '/css/all.min.css', |
| 219 |
[], |
| 220 |
false, |
| 221 |
'all' |
| 222 |
); |
| 223 |
} elseif ( $this->settings["icon_library_version"] === '5.paid' ) { |
| 224 |
wp_register_style( |
| 225 |
'buttonizer-icon-library', |
| 226 |
'https://pro.fontawesome.com/releases/v' . FONTAWESOME_CURRENT_VERSION . '/css/all.css', |
| 227 |
[], |
| 228 |
false, |
| 229 |
'all' |
| 230 |
); |
| 231 |
} elseif ( $this->settings["icon_library_version"] === '6.free' ) { |
| 232 |
wp_register_style( |
| 233 |
'buttonizer-icon-library', |
| 234 |
'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/' . FONTAWESOME_6_CURRENT_VERSION . '/css/all.min.css', |
| 235 |
[], |
| 236 |
false, |
| 237 |
'all' |
| 238 |
); |
| 239 |
} elseif ( $this->settings["icon_library_version"] === '4.7.0' ) { |
| 240 |
wp_register_style( |
| 241 |
'buttonizer-icon-library', |
| 242 |
'https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css', |
| 243 |
[], |
| 244 |
false, |
| 245 |
'all' |
| 246 |
); |
| 247 |
} |
| 248 |
wp_enqueue_style( 'buttonizer-icon-library' ); |
| 249 |
$wp_styles = wp_styles(); |
| 250 |
$wp_styles->add_data( 'buttonizer-icon-library', 'integrity', 'test' ); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
public function fixPremiumIconLibraries( $html, $handle ) { |
| 255 |
if ( $handle === 'buttonizer-icon-library' && $this->settings["icon_library_version"] === '5.paid' ) { |
| 256 |
return str_replace( "media='all'", "media='all' integrity='" . $this->settings["icon_library_code"] . "' crossorigin='anonymous'", $html ); |
| 257 |
} else { |
| 258 |
if ( $handle === 'buttonizer-icon-library' && (!isset( $this->settings["icon_library_version"] ) || $this->settings["icon_library_version"] === '5.free') ) { |
| 259 |
return str_replace( "media='all'", "media='all' integrity='" . FONTAWESOME_CURRENT_INTEGRITY . "' crossorigin='anonymous'", $html ); |
| 260 |
} else { |
| 261 |
if ( $handle === 'buttonizer-icon-library' && (!isset( $this->settings["icon_library_version"] ) || $this->settings["icon_library_version"] === '6.free') ) { |
| 262 |
return str_replace( "media='all'", "media='all' integrity='" . FONTAWESOME_CURRENT_INTEGRITY . "' crossorigin='anonymous'", $html ); |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
return $html; |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Backup for if the API is disabled |
| 271 |
*/ |
| 272 |
public function frontendJson() { |
| 273 |
wp_send_json( ( new \Buttonizer\Legacy\Api\Buttons\ApiButtons() )->get() ); |
| 274 |
} |
| 275 |
|
| 276 |
} |
| 277 |
|