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