| 1 |
<?php defined('ABSPATH') OR die('This script cannot be accessed directly.'); |
| 2 |
|
| 3 |
/** |
| 4 |
* Plugin Name: Convertful - Your Ultimate On-Site Conversion Tool |
| 5 |
* Version: 2.0 |
| 6 |
* Plugin URI: https://convertful.com/ |
| 7 |
* Description: All the modern on-site conversion solutions, natively integrates with all modern Email Marketing Platforms. |
| 8 |
* Author: Convertful |
| 9 |
* License: GPLv2 or later |
| 10 |
* License URI: http://www.gnu.org/licenses/gpl-2.0.html |
| 11 |
* Text Domain: convertful |
| 12 |
*/ |
| 13 |
|
| 14 |
// Global variables for plugin usage (global declaration is needed here for WP CLI compatibility) |
| 15 |
global $conv_file, $conv_dir, $conv_uri, $conv_version, $conv_config; |
| 16 |
$conv_file = __FILE__; |
| 17 |
$conv_dir = plugin_dir_path(__FILE__); |
| 18 |
$conv_uri = plugins_url('', __FILE__); |
| 19 |
$conv_version = preg_match('~Version\: ([^\n]+)~', file_get_contents(__FILE__, NULL, NULL, 82, 150), $conv_matches) ? $conv_matches[1] : FALSE; |
| 20 |
unset($conv_matches); |
| 21 |
|
| 22 |
if (file_exists($conv_dir.'config.php')) |
| 23 |
{ |
| 24 |
$conv_config = require $conv_dir.'config.php'; |
| 25 |
} |
| 26 |
|
| 27 |
add_action('init', 'conv_init'); |
| 28 |
if (is_admin()) |
| 29 |
{ |
| 30 |
require $conv_dir.'functions/admin_pages.php'; |
| 31 |
} |
| 32 |
|
| 33 |
add_filter('plugin_action_links_'.plugin_basename(__FILE__), 'conv_plugin_action_links'); |
| 34 |
|
| 35 |
// Shortcodes |
| 36 |
require $conv_dir.'functions/shortcodes.php'; |
| 37 |
register_uninstall_hook($conv_file, 'conv_uninstall'); |
| 38 |
|
| 39 |
add_action('rest_api_init', function () { |
| 40 |
|
| 41 |
register_rest_route('convertful/v2', '/complete_authorization/', [ |
| 42 |
'methods' => 'POST', |
| 43 |
'callback' => 'conv_complete_authorization', |
| 44 |
]); |
| 45 |
|
| 46 |
register_rest_route('convertful/v2', '/get_info/', [ |
| 47 |
'methods' => 'POST', |
| 48 |
'callback' => 'conv_get_info', |
| 49 |
]); |
| 50 |
}); |
| 51 |
|
| 52 |
/** |
| 53 |
* Get script id |
| 54 |
* @return string |
| 55 |
*/ |
| 56 |
function conv_get_script_id() |
| 57 |
{ |
| 58 |
global $conv_config; |
| 59 |
$url = wp_parse_url($conv_config['host']); |
| 60 |
if ( ! preg_match('/^(.*?)(convertful|devcf)\.[a-z]{3,5}$/', $url['host'])) |
| 61 |
{ |
| 62 |
return 'optin-api'; |
| 63 |
} |
| 64 |
|
| 65 |
return 'convertful-api'; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Get script file name |
| 70 |
* @return string |
| 71 |
*/ |
| 72 |
function conv_get_script_filename() |
| 73 |
{ |
| 74 |
return conv_get_script_id() === 'convertful-api' |
| 75 |
? 'Convertful.js' |
| 76 |
: 'optin.js'; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
function conv_init() |
| 81 |
{ |
| 82 |
if (get_option('optinguru_owner_id')) |
| 83 |
{ |
| 84 |
update_option('conv_owner_id', get_option('optinguru_owner_id'), TRUE); |
| 85 |
update_option('conv_site_id', get_option('optinguru_site_id', get_option('optinguru_website_id')), FALSE); |
| 86 |
update_option('conv_token', get_option('optinguru_token'), FALSE); |
| 87 |
} |
| 88 |
if (get_option('convertful_owner_id')) |
| 89 |
{ |
| 90 |
update_option('conv_owner_id', get_option('convertful_owner_id'), TRUE); |
| 91 |
update_option('conv_site_id', get_option('convertful_site_id'), FALSE); |
| 92 |
update_option('conv_token', get_option('convertful_token'), FALSE); |
| 93 |
} |
| 94 |
$owner_id = get_option('conv_owner_id'); |
| 95 |
if ( ! is_admin() AND $owner_id !== FALSE) |
| 96 |
{ |
| 97 |
add_action('wp_enqueue_scripts', 'conv_enqueue_scripts'); |
| 98 |
add_filter('script_loader_tag', 'conv_script_loader_tag', 10, 2); |
| 99 |
add_filter('the_content', 'conv_after_post_content'); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
function conv_enqueue_scripts() |
| 104 |
{ |
| 105 |
global $conv_config, $conv_version; |
| 106 |
$script_id = conv_get_script_id(); |
| 107 |
wp_enqueue_script($script_id, $conv_config['host'].'/'.conv_get_script_filename(), [], $conv_version, TRUE); |
| 108 |
|
| 109 |
$tags = []; |
| 110 |
$the_tags = get_the_tags(); |
| 111 |
if (is_array($the_tags)) |
| 112 |
{ |
| 113 |
foreach ($the_tags as $tag) |
| 114 |
{ |
| 115 |
$tags[] = $tag->slug; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
$categories = []; |
| 120 |
$the_categories = get_the_category(); |
| 121 |
if (is_array($the_categories)) |
| 122 |
{ |
| 123 |
foreach ($the_categories as $category) |
| 124 |
{ |
| 125 |
$categories[] = $category->slug; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
$user_meta = wp_get_current_user(); |
| 130 |
|
| 131 |
wp_localize_script($script_id, 'convPlatformVars', [ |
| 132 |
'postType' => get_post_type(), |
| 133 |
'categories' => $categories, |
| 134 |
'tags' => $tags, |
| 135 |
'userRoles' => ($user_meta instanceof WP_User AND ! empty($user_meta->roles)) ? $user_meta->roles : ['guest'], |
| 136 |
]); |
| 137 |
} |
| 138 |
|
| 139 |
function conv_script_loader_tag($tag, $handle) |
| 140 |
{ |
| 141 |
global $conv_config; |
| 142 |
$script_id = conv_get_script_id(); |
| 143 |
if ($handle !== $script_id) |
| 144 |
{ |
| 145 |
return $tag; |
| 146 |
} |
| 147 |
$script = sprintf('%s/%s?owner=%s', $conv_config['host'], conv_get_script_filename(), get_option('conv_owner_id')); |
| 148 |
|
| 149 |
return sprintf( |
| 150 |
'<script type="text/javascript" id="%s" src="%s" async="async"></script>', |
| 151 |
$script_id, |
| 152 |
$script |
| 153 |
); |
| 154 |
} |
| 155 |
|
| 156 |
function conv_uninstall() |
| 157 |
{ |
| 158 |
// Options cleanup |
| 159 |
foreach (['owner_id', 'site_id', 'website_id', 'token', 'ref'] as $option_name) |
| 160 |
{ |
| 161 |
delete_option('optinguru_'.$option_name); |
| 162 |
delete_option('convertful_'.$option_name); |
| 163 |
delete_option('conv_'.$option_name); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
function conv_complete_authorization(WP_REST_Request $request) |
| 168 |
{ |
| 169 |
$access_token = $request->get_param('access_token'); |
| 170 |
$owner_id = (int) $request->get_param('owner_id'); |
| 171 |
$site_id = (int) $request->get_param('site_id'); |
| 172 |
|
| 173 |
// send wrong token option if something goes wrong |
| 174 |
if ($access_token !== get_option('conv_token')) |
| 175 |
{ |
| 176 |
wp_send_json_error(['access_token' => 'Wrong access token']); |
| 177 |
} |
| 178 |
|
| 179 |
if (empty($owner_id)) |
| 180 |
{ |
| 181 |
wp_send_json_error(['owner_id' => 'Wrong parameters for authorization (owner_id is missing)']); |
| 182 |
} |
| 183 |
|
| 184 |
if (empty($site_id)) |
| 185 |
{ |
| 186 |
wp_send_json_error(['site_id' => 'Wrong parameters for authorization (site_id is missing)']); |
| 187 |
} |
| 188 |
|
| 189 |
update_option('conv_owner_id', (int) $owner_id, TRUE); |
| 190 |
update_option('conv_site_id', (int) $site_id, FALSE); |
| 191 |
|
| 192 |
wp_send_json_success(); |
| 193 |
} |
| 194 |
|
| 195 |
function conv_get_info(WP_REST_Request $request) |
| 196 |
{ |
| 197 |
|
| 198 |
$access_token = $request->get_param('access_token'); |
| 199 |
|
| 200 |
if ($access_token !== get_option('conv_token')) |
| 201 |
{ |
| 202 |
wp_send_json_error(['access_token' => 'Wrong access token']); |
| 203 |
} |
| 204 |
|
| 205 |
$tags = []; |
| 206 |
foreach (get_tags() as $tag) |
| 207 |
{ |
| 208 |
$tags[$tag->slug] = $tag->name; |
| 209 |
} |
| 210 |
|
| 211 |
$categories = []; |
| 212 |
foreach (get_categories() as $category) |
| 213 |
{ |
| 214 |
$categories[$category->slug] = $category->name; |
| 215 |
} |
| 216 |
|
| 217 |
$post_types = []; |
| 218 |
foreach (get_post_types(['public' => TRUE], 'objects') as $post_type) |
| 219 |
{ |
| 220 |
$post_type_name = isset($post_type->name) ? $post_type->name : NULL; |
| 221 |
$post_type_title = (isset($post_type->labels) AND isset($post_type->labels->singular_name)) |
| 222 |
? $post_type->labels->singular_name |
| 223 |
: $post_type['name']; |
| 224 |
if ($post_type_name AND $post_type_title) |
| 225 |
{ |
| 226 |
$post_types[$post_type_name] = $post_type_title; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
global $wp_roles; |
| 231 |
$user_roles = array(); |
| 232 |
foreach (apply_filters('editable_roles', $wp_roles->roles) as $user_role_name => $user_role) |
| 233 |
{ |
| 234 |
$user_roles[$user_role_name] = isset($user_role['name']) ? $user_role['name'] : $user_role_name; |
| 235 |
} |
| 236 |
$user_roles['guest'] = 'Guest (Unauthenticated)'; |
| 237 |
|
| 238 |
$result = [ |
| 239 |
'tags' => $tags, |
| 240 |
'categories' => $categories, |
| 241 |
'post_types' => $post_types, |
| 242 |
'user_roles' => $user_roles, |
| 243 |
]; |
| 244 |
|
| 245 |
wp_send_json_success($result); |
| 246 |
} |
| 247 |
|
| 248 |
function conv_after_post_content($content) |
| 249 |
{ |
| 250 |
if (is_single()) |
| 251 |
{ |
| 252 |
$content .= '<div class="conv-place conv-place_after_post"></div>'; |
| 253 |
} |
| 254 |
|
| 255 |
return $content; |
| 256 |
} |
| 257 |
|
| 258 |
function conv_plugin_action_links($links) |
| 259 |
{ |
| 260 |
return array_merge(['<a href="'.admin_url('tools.php?page=conv-settings').'">'.__('Settings').'</a>'], $links); |
| 261 |
} |
| 262 |
|