PluginProbe
Convertful – Your Ultimate On-Site Conversion Tool / 2.1
Convertful – Your Ultimate On-Site Conversion Tool v2.1
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.1, at convertful.php

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