Completed
Pull Request — master (#26)
by Leon
03:10
created

cmb2-admin-extension.php (4 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.3
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.3';
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
		$this->check_for_cmb2();
92
93
		add_action( 'init', array( $this, 'load_textdomain' ), 9 );
94
	}
95
96
	/**
97
	 * Return an instance of this class.
98
	 *
99
	 * @return object A single instance of this class.
100
	 */
101
	public static function get_instance() {
102
		// If the single instance hasn't been set, set it now.
103
		if ( null === self::$instance ) {
104
			self::$instance = new self;
105
		}
106
107
		return self::$instance;
108
	}
109
110
	/**
111
	 * Check for the CMB2 plugin.
112
	 *
113
	 * @since 0.0.1
114
	 */
115
	private function check_for_cmb2() {
116
		if ( defined( 'CMB2_LOADED' ) && CMB2_LOADED !== false ) {
0 ignored issues
show
Found "!== false". Use Yoda Condition checks, you must
Loading history...
117
118
			require_once dirname( __FILE__ ) . '/includes/class-meta-box.php';
119
			require_once dirname( __FILE__ ) . '/includes/class-meta-box-post-type.php';
120
			require_once dirname( __FILE__ ) . '/includes/class-meta-box-settings.php';
121
			cmb2ae_metabox();
122
			return;
123
		} elseif ( file_exists( WP_PLUGIN_DIR . '/' . CMB2AE_CMB2_PLUGIN_FILE ) ) {
124
125
			add_action( 'admin_notices', array( $this, 'cmb2_not_activated' ) );
126
			return;
127
		}
128
		add_action( 'admin_notices', array( $this, 'missing_cmb2' ) );
129
	}
130
131
	/**
132
	 * Load plugin textdomain.
133
	 *
134
	 * @return void
135
	 */
136
	public function load_textdomain() {
137
		$lang_path = plugin_basename( dirname( __FILE__ ) ) . '/languages';
138
		$loaded    = load_muplugin_textdomain( 'cmb2-admin-extension', $lang_path );
139
		if ( strpos( __FILE__, basename( WPMU_PLUGIN_DIR ) ) === false ) {
0 ignored issues
show
Found "=== false". Use Yoda Condition checks, you must
Loading history...
140
			$loaded = load_plugin_textdomain( 'cmb2-admin-extension', false, $lang_path );
141
		}
142
143
		if ( ! $loaded ) {
144
			$loaded = load_theme_textdomain( 'cmb2-admin-extension', get_stylesheet_directory() . '/languages' );
145
		}
146
147
		if ( ! $loaded ) {
148
			$locale = apply_filters( 'plugin_locale', get_locale(), 'cmb2-admin-extension' );
149
			$mofile = dirname( __FILE__ ) . '/languages/cmb2-admin-extension-' . $locale . '.mo';
150
			load_textdomain( 'cmb2-admin-extension', $mofile );
151
		}
152
	}
153
154
	/**
155
	 * Add an error notice if the CMB2 plugin is missing.
156
	 *
157
	 * @return void
158
	 */
159
	public function missing_cmb2() {
160
		?>
161
			<div class="error">
162
				<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>
163
			</div>
164
		<?php
165
	}
166
167
	/**
168
	 * Add an error notice if the CMB2 plugin isn't activated.
169
	 *
170
	 * @return void
171
	 */
172
	public function cmb2_not_activated() {
173
		?>
174
			<div class="error">
175
				<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>
176
			</div>
177
		<?php
178
	}
179
180
}
181
182
add_action( 'plugins_loaded', array( 'CMB2_Admin_Extension_Class', 'get_instance' ), 20 );
183
184
if ( ! function_exists( 'cmbf' ) ) {
185
186
	/**
187
	 * This function needs documentation.
188
	 *
189
	 * @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...
190
	 *
191
	 * @param int    $id    Post ID.
192
	 * @param string $field The meta key to retrieve.
193
	 */
194
	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...
195
		return get_post_meta( $id, $field, true );
196
	}
197
198
}
199