-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdrive.php
124 lines (104 loc) · 3.21 KB
/
gdrive.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
<?php
/*
Plugin Name: Media Explorer - Google Drive
Version: 0.1.1-alpha
Description: Allows users to select files from their own Google Drive to insert into posts. Requires the Media Explorer and Google Docs Shortcode plugins.
Author: r-a-y
Text Domain: gdrive
Domain Path: /languages
*/
defined( 'ABSPATH' ) or die();
add_action( 'mexp_init', array( 'MEXP_GDrive', 'init' ) );
/**
* Google Drive integration with Media Explorer.
*
* @package MEXP_GDrive
*
* @link https://github.com/automattic/media-explorer Get the Media Explorer plugin here.
*/
class MEXP_GDrive {
/**
* Absolute path to this directory.
*
* @var string
*/
public static $PATH = '';
/**
* URL to this directory.
*
* @var string
*/
public static $URL = '';
/**
* Static initializer.
*/
public static function init() {
return new self();
}
/**
* Constructor.
*/
protected function __construct() {
// make sure our Google constants are defined before proceeding
if ( false === defined( 'MEXP_GDRIVE_CLIENT_ID' ) || false === defined( 'MEXP_GDRIVE_CLIENT_SECRET' ) ) {
add_action( 'admin_head', array( $this, 'show_constants_notice' ) );
return;
}
// make sure Google Docs Shortcode v0.4 is installed before proceeding
if ( false === function_exists( 'ray_gdoc_shortcode_init' ) ) {
add_action( 'admin_head', array( $this, 'show_shortcode_notice' ) );
return;
}
// properties
$this->properties();
// admin loader
add_filter( 'mexp_services', array( $this, 'register_mexp_service' ) );
}
/**
* Properties.
*/
protected function properties() {
self::$PATH = dirname( __FILE__ );
self::$URL = plugins_url( basename( self::$PATH ) );
}
/**
* Registers our 'gdrive' service with Media Explorer.
*
* @param array $services Current registered services.
* @return array
*/
public function register_mexp_service( array $services ) {
if ( false === class_exists( 'MEXP_GDrive_Service' ) ) {
require dirname( __FILE__ ) . '/includes/mexp-service.php';
}
$services['gdrive'] = new MEXP_GDrive_Service;
return $services;
}
/**
* Helper function to add a notice.
*/
protected function add_notice( $notice = '' ) {
// only show notice for admins
if ( false === current_user_can( 'install_plugins' ) ) {
return;
}
$hook = is_network_admin() ? 'network_admin_notices' : 'admin_notices';
add_action( $hook, function() use ( $notice ) {
printf( '<div class="error"><p>%s</p></div>', $notice );
} );
}
/**
* Displays an admin notice if our Google constants are not defined yet.
*/
public function show_constants_notice() {
$notice = sprintf( __( '<strong>Media Explorer - Google Drive</strong> requires the %s and %s constants to be defined. Please view the readme.md file for more information.', 'gdrive' ), '<code>MEXP_GDRIVE_CLIENT_ID</code>', '<code>MEXP_GDRIVE_CLIENT_SECRET</code>' );
$this->add_notice( $notice );
}
/**
* Displays an admin notice if Google Docs Shortcode v0.4 isn't installed.
*/
public function show_shortcode_notice() {
$notice = __( '<strong>Media Explorer - Google Drive</strong> requires the latest version of the Google Docs Shortcode plugin. Please install and activate it.', 'gdrive' );
$this->add_notice( $notice );
}
}