PluginProbe
Convertful – Your Ultimate On-Site Conversion Tool / 2.6
Convertful – Your Ultimate On-Site Conversion Tool v2.6
trunk 1.0 1.1 1.3 1.4 1.5 1.6 1.7 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8
convertful / convertful.php

convertful.php in Convertful – Your Ultimate On-Site Conversion Tool 2.6, at convertful.php

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