PluginProbe
BetterLinks – Link Shortener, Link Cloaking, Redirects, Affiliate Link Manager & MCP / 2.4.12
BetterLinks – Link Shortener, Link Cloaking, Redirects, Affiliate Link Manager & MCP v2.4.12
3.1.2 3.1.1 3.1.0 3.0.1 3.0.0 2.4.13 2.4.12 2.4.11 2.4.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 All 109 releases
betterlinks / betterlinks.php

betterlinks.php in BetterLinks – Link Shortener, Link Cloaking, Redirects, Affiliate Link Manager & MCP 2.4.12, at betterlinks.php

228 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * Plugin Name: BetterLinks
4 * Plugin URI: https://betterlinks.io/
5 * Description: Ultimate plugin to create, shorten, track and manage any URL. Gather analytics reports and run successfully marketing campaigns easily.
6 * Version: 2.4.12
7 * Author: WPDeveloper
8 * Author URI: https://wpdeveloper.com
9 * License: GPL-3.0+
10 * License URI: http://www.gnu.org/licenses/gpl-3.0.txt
11 * Author URI: https://wpdeveloper.com
12 * Text Domain: betterlinks
13 * Domain Path: /languages
14 */
15
16 use BetterLinks\Admin\Cache;
17
18 if (!defined('ABSPATH')) {
19 exit();
20 }
21
22 if (file_exists(dirname(__FILE__) . '/vendor/autoload.php')) {
23 require_once dirname(__FILE__) . '/vendor/autoload.php';
24 }
25
26 if (!class_exists('BetterLinks')) {
27 final class BetterLinks
28 {
29 private $Installer;
30 private $upload_dir;
31 private function __construct()
32 {
33 $this->upload_dir_path();
34 $this->define_constants();
35 $this->set_global_settings();
36 $this->Installer = new BetterLinks\Installer();
37 register_activation_hook(__FILE__, [$this, 'activate']);
38 register_deactivation_hook(__FILE__, [$this, 'deactivate']);
39 add_action('plugins_loaded', [$this, 'on_plugins_loaded']);
40 add_action('betterlinks_loaded', [$this, 'init_plugin']);
41 add_action('admin_init', [$this, 'run_migrator']);
42 add_action('admin_init', [$this, 'do_the_works_if_failed_during_activation'], 100);
43 add_action('admin_init', [$this, 'quick_setup']);
44 $this->dispatch_hook();
45 add_action( 'wp_enqueue_scripts', [$this, 'frontend_scripts'] );
46 }
47
48 public function do_the_works_if_failed_during_activation()
49 {
50 $betterlinks_activation_flag = BetterLinks\Helper::btl_get_option("betterlinks_activation_flag");
51 if(isset($betterlinks_activation_flag["last_activation_background_processes_firing_timestamp"]) && isset($betterlinks_activation_flag["last_activation_timestamp"])) {
52 if($betterlinks_activation_flag["last_activation_background_processes_firing_timestamp"]){
53 return false;
54 }
55 $waiting_time_in_seconds = 5;
56 if((absInt($betterlinks_activation_flag["last_activation_timestamp"]) + $waiting_time_in_seconds) > time()){
57 // don't go any further and return false here if,
58 // $waiting_time_in_seconds (in this case 5 seconds) haven't passed yet since the activation flag was setted
59 return false;
60 }
61 $all_tasks = array_merge(
62 $this->Installer->activation,
63 $this->Installer->migration
64 );
65 foreach ($all_tasks as $task) {
66 $this->Installer->$task();
67 }
68 }
69 }
70
71 public static function init()
72 {
73 static $instance = false;
74
75 if (!$instance) {
76 $instance = new self();
77 }
78
79 return $instance;
80 }
81 public function define_constants()
82 {
83 /**
84 * Defines CONSTANTS for Whole plugins.
85 */
86 define('BETTERLINKS_VERSION', '2.4.12');
87 define('BETTERLINKS_DB_VERSION', '1.6.11');
88 define('BETTERLINKS_MENU_NOTICE', '9');
89 define('BETTERLINKS_SETTINGS_NAME', 'betterlinks_settings');
90 define('BETTERLINKS_PLUGIN_FILE', __FILE__);
91 define('BETTERLINKS_PLUGIN_BASENAME', plugin_basename(__FILE__));
92 define('BETTERLINKS_PLUGIN_SLUG', 'betterlinks');
93 define('BETTERLINKS_PLUGIN_ROOT_URI', plugins_url('/', __FILE__));
94 define('BETTERLINKS_ROOT_DIR_PATH', plugin_dir_path(__FILE__));
95 define('BETTERLINKS_ASSETS_DIR_PATH', BETTERLINKS_ROOT_DIR_PATH . 'assets/');
96 define('BETTERLINKS_ASSETS_URI', BETTERLINKS_PLUGIN_ROOT_URI . 'assets/');
97 define('BETTERLINKS_UPLOAD_DIR_PATH', $this->upload_dir['basedir'] . '/betterlinks_uploads');
98 define('BETTERLINKS_EXISTS_LINKS_JSON', defined('BETTERLINKS_ALLOW_JSON_REDIRECT') ? file_exists(BETTERLINKS_UPLOAD_DIR_PATH . '/links.json') && BETTERLINKS_ALLOW_JSON_REDIRECT : file_exists(BETTERLINKS_UPLOAD_DIR_PATH . '/links.json'));
99 define('BETTERLINKS_EXISTS_CLICKS_JSON', file_exists(BETTERLINKS_UPLOAD_DIR_PATH . '/clicks.json'));
100 define('BETTERLINKS_EXISTS_SETTINGS_JSON', defined('BETTERLINKS_ALLOW_JSON_REDIRECT') ? file_exists(BETTERLINKS_UPLOAD_DIR_PATH . '/settings.json') && BETTERLINKS_ALLOW_JSON_REDIRECT : file_exists(BETTERLINKS_UPLOAD_DIR_PATH . '/settings.json'));
101 define('BETTERLINKS_LINKS_OPTION_NAME', 'betterlinks_links');
102 define('BETTERLINKS_CACHE_LINKS_NAME', 'betterlinks_cache_links_data');
103 define('BETTERLINKS_DB_ALTER_OPTIONS', 'betterlinks_db_alter_options');
104 define('BETTERLINKS_CUSTOM_DOMAIN_MENU', 'betterlinks_custom_domain_menu');
105 define('BETTERLINKS_AI_API_KEYS_OPTION_NAME', 'betterlinks_ai_api_keys');
106 }
107
108 public function upload_dir_path()
109 {
110 $this->upload_dir = wp_get_upload_dir();
111 }
112
113
114 public function on_plugins_loaded()
115 {
116 do_action('betterlinks_loaded');
117 }
118
119 /**
120 * Initialize the plugin
121 *
122 * @return void
123 */
124 public function init_plugin()
125 {
126 BetterLinks\API::init();
127 if (is_admin()) {
128 new BetterLinks\Admin();
129 }
130 BetterLinks\Integration::init();
131 new BetterLinks\Link();
132 new BetterLinks\Tools();
133 new BetterLinks\Frontend;
134 new BetterLinks\Elementor();
135 }
136
137 public function dispatch_hook()
138 {
139 BetterLinks\API::dispatch_hook();
140 BetterLinks\Cron::init();
141 }
142
143 public function set_global_settings()
144 {
145 $GLOBALS['betterlinks'] = BetterLinks\Helper::get_links();
146 $settings = Cache::get_json_settings();
147 $auto_create_link_settings = defined('BETTERLINKS_PRO_AUTO_LINK_CREATE_OPTION_NAME') ? get_option( BETTERLINKS_PRO_AUTO_LINK_CREATE_OPTION_NAME, array() ) : array();
148 if ( is_string( $auto_create_link_settings ) ) {
149 $auto_create_link_settings = json_decode( $auto_create_link_settings, true );
150 }
151 $settings = is_array($settings) ? $settings : array();
152 $auto_create_link_settings = is_array( $auto_create_link_settings ) ? $auto_create_link_settings : array();
153 $GLOBALS['betterlinks_settings'] = array_merge( $settings, $auto_create_link_settings );
154 }
155
156 public function run_migrator()
157 {
158 $btl_version = BetterLinks\Helper::btl_get_option('betterlinks_version');
159 $should_insert = $btl_version===false;
160 if ($btl_version != BETTERLINKS_VERSION && BetterLinks\Helper::btl_update_option('betterlinks_version', BETTERLINKS_VERSION, $should_insert, !$should_insert)) {
161 $this->Installer->data($this->Installer->migration)->save()->dispatch();
162 BetterLinks\Helper::btl_update_option('betterlinks_activation_flag', [
163 "last_activation_timestamp" => time(),
164 "last_activation_background_processes_firing_timestamp" => false,
165 ]);
166 }
167 }
168
169 public function activate()
170 {
171 $this->Installer->data($this->Installer->activation)->save()->dispatch();
172 BetterLinks\Helper::btl_update_option('betterlinks_activation_flag', [
173 "last_activation_timestamp" => time(),
174 "last_activation_background_processes_firing_timestamp" => false,
175 ]);
176 add_option('betterlinks_quick_setup', true);
177 }
178
179 public function quick_setup() {
180 if( 'complete' === get_option('betterlinks_quick_setup_step') ) return;
181 if( get_option( 'betterlinks_quick_setup' ) && is_admin() ) {
182 delete_option( 'betterlinks_quick_setup' );
183
184 $redirect_url = admin_url('admin.php?page=betterlinks-quick-setup');
185 wp_safe_redirect($redirect_url);
186 exit;
187 }
188 }
189
190 public function deactivate()
191 {
192 new BetterLinks\Uninstall();
193 }
194
195 public function frontend_scripts() {
196 $dependencies = include_once BETTERLINKS_ASSETS_DIR_PATH . 'js/betterlinks.app.core.min.asset.php';
197
198 // Enqueue main app script (geolocation logic is bundled inside)
199 wp_enqueue_script( 'betterlinks-app', BETTERLINKS_ASSETS_URI . 'js/betterlinks.app.core.min.js', [ 'jquery' ], $dependencies['version'], true );
200
201 wp_localize_script('betterlinks-app', 'betterLinksApp', [
202 'betterlinks_nonce' => wp_create_nonce('betterlinks_admin_nonce'),
203 'ajaxurl' => admin_url('admin-ajax.php'),
204 'site_url' => apply_filters('betterlinks/site_url', site_url()),
205 'rest_url' => rest_url(),
206 'nonce' => wp_create_nonce('wp_rest'),
207 'betterlinkspro_version' => defined('BETTERLINKS_PRO_VERSION') ? BETTERLINKS_PRO_VERSION : null,
208 ]);
209 }
210 }
211 }
212
213 /**
214 * Initializes the main plugin
215 *
216 * @return \BetterLinks
217 */
218 if (!function_exists('BetterLinks_Start')) {
219 function BetterLinks_Start()
220 {
221 return BetterLinks::init();
222
223 }
224 }
225
226 // Plugin Start
227 BetterLinks_Start();
228