-
Notifications
You must be signed in to change notification settings - Fork 2
/
ayecode-connect.php
executable file
·174 lines (147 loc) · 5.46 KB
/
ayecode-connect.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/**
* Plugin Name: AyeCode Connect
* Plugin URI: https://ayecode.io/
* Description: A service plugin letting users connect AyeCode Services to their site.
* Version: 1.3.8
* Author: AyeCode
* Author URI: https://ayecode.io
* Requires at least: 4.7
* Tested up to: 6.7
*
* Text Domain: ayecode-connect
* Domain Path: /languages/
*
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
if ( !defined( 'AYECODE_CONNECT_VERSION' ) ) {
define( 'AYECODE_CONNECT_VERSION', '1.3.8' );
}
if ( !defined( 'AYECODE_CONNECT_SSL_VERIFY' ) ) {
define( 'AYECODE_CONNECT_SSL_VERIFY', true );
}
if ( !defined( 'AYECODE_CONNECT_PLUGIN_DIR' ) ) {
define( 'AYECODE_CONNECT_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
}
add_action( 'plugins_loaded', 'ayecode_connect' );
/**
* Sets up the client
*/
function ayecode_connect() {
global $ayecode_connect;
/**
* The libraries required.
*/
require_once plugin_dir_path( __FILE__ ) . '/vendor/autoload.php';
//Include the client connection class
require_once plugin_dir_path( __FILE__ ) . 'includes/class-ayecode-connect.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/class-ayecode-connect-settings.php';
//Prepare client args
$args = ayecode_connect_args();
$ayecode_connect = new AyeCode_Connect( $args );
//Call the init method to register routes. This should be called exactly once per client (Preferably before the init hook).
$ayecode_connect->init();
// Determines the current locale.
if ( function_exists( 'determine_locale' ) ) {
$locale = determine_locale();
} else if ( function_exists( 'get_user_locale' ) ) {
$locale = get_user_locale();
} else {
$locale = get_locale();
}
$locale = apply_filters( 'plugin_locale', $locale, 'ayecode-connect' );
unload_textdomain( 'ayecode-connect' );
load_textdomain( 'ayecode-connect', WP_LANG_DIR . '/ayecode-connect/ayecode-connect-' . $locale . '.mo' );
load_plugin_textdomain( 'ayecode-connect', false, basename( dirname( __FILE__ ) ) . '/languages/' );
}
/**
* The AyeCode Connect arguments.
*
* @return array
*/
function ayecode_connect_args() {
$base_url = 'https://ayecode.io';
return array(
'remote_url' => $base_url, //URL to the WP site containing the WP_Service_Provider class
'connection_url' => $base_url . '/connect', //This should be a custom page the authinticates a user the calls the WP_Service_Provider::connect_site() method
'api_url' => $base_url . '/wp-json/', //Might be different for you
'api_namespace' => 'ayecode/v1',
'local_api_namespace' => 'ayecode-connect/v1', //Should be unique for each client implementation
'prefix' => 'ayecode_connect', //A unique prefix for things (accepts alphanumerics and underscores). Each client on a given site should have it's own unique prefix
'textdomain' => 'ayecode-connect',
'version' => AYECODE_CONNECT_VERSION,
);
}
/**
* Add settings link to plugins page.
*
* @param $links
*
* @return mixed
*/
function ayecode_connect_settings_link( $links ) {
$settings_link = '<a href="admin.php?page=ayecode-connect">' . __( 'Settings','ayecode-connect' ) . '</a>';
array_push( $links, $settings_link );
return $links;
}
$plugin = plugin_basename( __FILE__ );
add_filter( "plugin_action_links_{$plugin}", 'ayecode_connect_settings_link' );
/**
* Remove wp cron on deactivation if set.
*/
register_deactivation_hook( __FILE__, 'ayecode_connect_deactivation' );
function ayecode_connect_deactivation() {
$args = ayecode_connect_args();
$prefix = $args['prefix'];
wp_clear_scheduled_hook( $prefix.'_callback' );
// destroy support user
$support_user = get_user_by( 'login', 'ayecode_connect_support_user' );
if ( ! empty( $support_user ) && isset( $support_user->ID ) && ! empty( $support_user->ID ) ) {
require_once(ABSPATH.'wp-admin/includes/user.php');
$user_id = absint($support_user->ID);
// get all sessions for user with ID $user_id
$sessions = WP_Session_Tokens::get_instance($user_id);
// we have got the sessions, destroy them all!
$sessions->destroy_all();
$reassign = user_can( 1, 'manage_options' ) ? 1 : null;
wp_delete_user( $user_id, $reassign );
if ( is_multisite() ) {
if ( ! function_exists( 'wpmu_delete_user' ) ) {
require_once( ABSPATH . 'wp-admin/includes/ms.php' );
}
revoke_super_admin( $user_id );
wpmu_delete_user( $user_id );
}
}
// Try to remove the must use plugin. This should fail silently even if file is missing.
wp_delete_file( WPMU_PLUGIN_DIR."/ayecode-connect-filter-fix.php" );
}
/**
* Sync licenses if connected.
*/
function ayecode_connect_sync_licenses() {
global $ayecode_connect;
if(method_exists($ayecode_connect,'sync_licences')){
$ayecode_connect->sync_licences();
}
}
add_action( 'ayecode_connect_sync_licenses', 'ayecode_connect_sync_licenses' );
function ayecode_connect_demo_import_redirect( $plugin ){
if ( $plugin == plugin_basename( __FILE__ ) && !empty( $_SERVER['HTTP_REFERER'] ) ) {
$parts = parse_url($_SERVER['HTTP_REFERER']);
if ( ! empty( $parts['query'] ) ) {
parse_str( $parts['query'], $query );
} else {
$query = array();
}
if(!empty($query['ac-demo-import'])){
$demo = sanitize_title_with_dashes($query['ac-demo-import']);
wp_redirect(admin_url( "admin.php?page=ayecode-demo-content&ac-demo-import=".esc_attr($demo) ));
exit;
}
}
}
//register_activation_hook( __FILE__, 'ayecode_connect_demo_import_redirect' );
add_action( 'activated_plugin', 'ayecode_connect_demo_import_redirect' );