Completed
Pull Request — master (#23)
by Juliette
02:33
created

cmb2-admin-extension.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * CMB2 Admin Extension - a WordPress plugin.
4
 *
5
 * @category     WordPress_Plugin
6
 * @package      CMB2-Admin-Extension
7
 * @author       twoelevenjay
8
 * @license      GPL-2.0+
9
 * @link         http://211j.com
10
 *
11
 * @wordpress-plugin
12
 * Plugin Name:  CMB2 Admin Extension
13
 * Plugin URI:   https://github.com/twoelevenjay/CMB2-Admin-Extension
14
 * Description:  CMB2 Admin Extension add a user interface for admins to create CMB2 meta boxes from the WordPress admin.
15
 * Author:       twoelevenjay
16
 * Author URI:   http://211j.com
17
 * Contributors:
18
 * Version:      0.1.2
19
 * Text Domain:  cmb2-admin-extension
20
 * Domain Path:  /languages
21
 *
22
 *
23
 * Released under the GPL license
24
 * http://www.opensource.org/licenses/gpl-license.php
25
 *
26
 * This is an add-on for WordPress
27
 * http://wordpress.org/
28
 *
29
 * **********************************************************************
30
 * This program is free software; you can redistribute it and/or modify
31
 * it under the terms of the GNU General Public License as published by
32
 * the Free Software Foundation; either version 2 of the License, or
33
 * (at your option) any later version.
34
 *
35
 * This program is distributed in the hope that it will be useful,
36
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38
 * GNU General Public License for more details.
39
 * **********************************************************************
40
 */
41
42
/**
43
 * Silence is golden; exit if accessed directly.
44
 */
45
if ( ! defined( 'ABSPATH' ) ) {
46
	exit;
47
}
48
49
50
/**
51
 * Define plugin constant.
52
 */
53
if ( ! defined( 'CMB2AE_CMB2_PLUGIN_FILE' ) ) {
54
	define( 'CMB2AE_CMB2_PLUGIN_FILE', 'cmb2/init.php' );
55
}
56
57
if ( ! defined( 'CMB2AE_URI' ) ) {
58
	define( 'CMB2AE_URI', plugins_url( '', __FILE__ ) );
59
}
60
61
if ( ! defined( 'CMB2AE_PATH' ) ) {
62
	define( 'CMB2AE_PATH', plugin_dir_path( __FILE__ ) );
63
}
64
65
66
/**
67
 * CMB2 Admin Extension main class.
68
 */
69
class CMB2_Admin_Extension_Class {
70
71
	/**
72
	 * Plugin version.
73
	 *
74
	 * @var string
75
	 */
76
	const VERSION = '0.1.2';
77
78
	/**
79
	 * Instance of this class.
80
	 *
81
	 * @var object
82
	 */
83
	protected static $instance;
84
85
	/**
86
	 * Initiate CMB2 Admin Extension.
87
	 *
88
	 * @since 0.0.1
89
	 */
90
	public function __construct() {
91
92
		$this->check_for_cmb2();
93
94
		add_action( 'init', array( $this, 'load_textdomain' ), 9 );
95
	}
96
97
	/**
98
	 * Return an instance of this class.
99
	 *
100
	 * @return object A single instance of this class.
101
	 */
102
	public static function get_instance() {
103
		// If the single instance hasn't been set, set it now.
104
		if ( null == self::$instance ) {
105
			self::$instance = new self;
106
		}
107
108
		return self::$instance;
109
	}
110
111
	/**
112
	 * Check for the CMB2 plugin.
113
	 *
114
	 * @since 0.0.1
115
	 */
116
	private function check_for_cmb2() {
117
118
		if ( defined( 'CMB2_LOADED' ) && CMB2_LOADED !== false ) {
0 ignored issues
show
Found "!== false". Use Yoda Condition checks, you must
Loading history...
119
120
			require_once dirname( __FILE__ ) . '/includes/class-meta-box.php';
121
			require_once dirname( __FILE__ ) . '/includes/class-meta-box-post-type.php';
122
			require_once dirname( __FILE__ ) . '/includes/class-meta-box-settings.php';
123
124
		} elseif ( file_exists( WP_PLUGIN_DIR . '/' . CMB2AE_CMB2_PLUGIN_FILE ) ) {
125
126
			add_action( 'admin_notices', array( $this, 'cmb2_not_activated' ) );
127
128
		} else {
129
130
			add_action( 'admin_notices', array( $this, 'missing_cmb2' ) );
131
		}
132
	}
133
134
	/**
135
	 * Load plugin textdomain.
136
	 *
137
	 * @return void
138
	 */
139
	public function load_textdomain() {
140
		$lang_path = plugin_basename( dirname( __FILE__ ) ) . '/languages';
141
142
		if ( false === strpos( __FILE__, basename( WPMU_PLUGIN_DIR ) ) ) {
143
			$loaded = load_plugin_textdomain( 'cmb2-admin-extension', false, $lang_path );
144
		} else {
145
			$loaded = load_muplugin_textdomain( 'cmb2-admin-extension', $lang_path );
146
		}
147
148
		if ( ! $loaded ) {
149
			$loaded = load_theme_textdomain( 'cmb2-admin-extension', get_stylesheet_directory() . '/languages' );
150
		}
151
152
		if ( ! $loaded ) {
153
			$locale = apply_filters( 'plugin_locale', get_locale(), 'cmb2-admin-extension' );
154
			$mofile = dirname( __FILE__ ) . '/languages/cmb2-admin-extension-' . $locale . '.mo';
155
			load_textdomain( 'cmb2-admin-extension', $mofile );
156
		}
157
	}
158
159
	/**
160
	 * Add an error notice if the CMB2 plugin is missing.
161
	 *
162
	 * @return void
163
	 */
164
	public function missing_cmb2() {
165
166
		?>
167
			<div class="error">
168
				<p><?php printf( esc_html__( 'CMB2 Admin Extension depends on the last version of %s the CMB2 plugin %s to work!', 'cmb2-admin-extension' ), '<a href="https://wordpress.org/plugins/cmb2/">', '</a>' ); ?></p>
169
			</div>
170
		<?php
171
172
	}
173
174
	/**
175
	 * Add an error notice if the CMB2 plugin isn't activated.
176
	 *
177
	 * @return void
178
	 */
179
	public function cmb2_not_activated() {
180
181
		?>
182
			<div class="error">
183
				<p><?php printf( esc_html__( 'The CMB2 plugin is installed but has not been activated. Please %s activate %s it to use the CMB2 Admin Extension', 'cmb2-admin-extension' ), '<a href="' . esc_url( admin_url( 'plugins.php' ) ) . '">', '</a>' ); ?></p>
184
			</div>
185
		<?php
186
187
	}
188
}
189
190
add_action( 'plugins_loaded', array( 'CMB2_Admin_Extension_Class', 'get_instance' ), 20 );
191
192
if ( ! function_exists( 'cmbf' ) ) {
193
194
	/**
195
	 * This function needs documentation.
196
	 *
197
	 * @todo
0 ignored issues
show
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
198
	 *
199
	 * @param int    $id    Post ID.
200
	 * @param string $field The meta key to retrieve.
201
	 */
202
	function cmbf( $id, $field ) {
0 ignored issues
show
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
203
204
		return CMB2_Meta_Box_Post_Type::cmbf( $id, $field );
205
206
	}
207
}
208