PluginProbe
Custom Related Posts / 1.7.0
Custom Related Posts v1.7.0
trunk 1.0 1.4 1.5.0 1.6.0 1.7.0 1.7.3 1.7.4 1.7.5 1.8.0 1.8.1 1.8.2
custom-related-posts / custom-related-posts.php

custom-related-posts.php in Custom Related Posts 1.7.0, at custom-related-posts.php

218 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Custom Related Posts
4 Plugin URI: http://bootstrapped.ventures
5 Description: Manually define related posts for any custom post type
6 Version: 1.7.0
7 Author: Bootstrapped Ventures
8 Author URI: http://bootstrapped.ventures
9 Text Domain: custom-related-posts
10 License: GPLv3
11 */
12 define( 'CRP_VERSION', '1.7.0' );
13
14 class CustomRelatedPosts {
15
16 private static $instance;
17 private static $instantiated_by_premium;
18 private static $addons = array();
19
20 /**
21 * Return instance of self
22 */
23 public static function get( $instantiated_by_premium = false )
24 {
25 // Instantiate self only once
26 if( is_null( self::$instance ) ) {
27 self::$instantiated_by_premium = $instantiated_by_premium;
28 self::$instance = new self;
29 self::$instance->init();
30 }
31
32 return self::$instance;
33 }
34
35 /**
36 * Returns true if we are using the Premium version
37 */
38 public static function is_premium_active()
39 {
40 return self::$instantiated_by_premium;
41 }
42
43 /**
44 * Add loaded addon to array of loaded addons
45 */
46 public static function loaded_addon( $addon, $instance )
47 {
48 if( !array_key_exists( $addon, self::$addons ) ) {
49 self::$addons[$addon] = $instance;
50 }
51 }
52
53 /**
54 * Returns true if the specified addon has been loaded
55 */
56 public static function is_addon_active( $addon )
57 {
58 return array_key_exists( $addon, self::$addons );
59 }
60
61 public static function addon( $addon )
62 {
63 if( isset( self::$addons[$addon] ) ) {
64 return self::$addons[$addon];
65 }
66
67 return false;
68 }
69
70 public static function setting( $setting )
71 {
72 return self::get()->helper( 'settings' )->get( $setting );
73 }
74
75 public $pluginName = 'custom-related-posts';
76 public $coreDir;
77 public $corePath;
78 public $coreUrl;
79 public $pluginFile;
80
81 protected $helper_dirs = array();
82 protected $helpers = array();
83
84 /**
85 * Initialize
86 */
87 public function init()
88 {
89 // Update plugin version
90 update_option( $this->pluginName . '_version', CRP_VERSION );
91
92 // Set core directory, URL and main plugin file
93 $this->corePath = str_replace( '/custom-related-posts.php', '', plugin_basename( __FILE__ ) );
94 $this->coreDir = apply_filters( 'crp_core_dir', WP_PLUGIN_DIR . '/' . $this->corePath );
95 $this->coreUrl = apply_filters( 'crp_core_url', plugins_url() . '/' . $this->corePath );
96 $this->pluginFile = apply_filters( 'crp_plugin_file', __FILE__ );
97
98 // Load textdomain
99 if( !self::is_premium_active() ) {
100 $domain = 'custom-related-posts';
101 $locale = apply_filters( 'plugin_locale', get_locale(), $domain );
102
103 load_textdomain( $domain, WP_LANG_DIR.'/'.$domain.'/'.$domain.'-'.$locale.'.mo' );
104 load_plugin_textdomain( $domain, false, $this->corePath . '/lang/' );
105 }
106
107 // Add core helper directory
108 $this->add_helper_directory( $this->coreDir . '/helpers' );
109
110 // Migrate first if needed, then load settings.
111 $this->helper( 'migration' );
112 $this->helper( 'settings' );
113
114 // Load required helpers
115 $this->helper( 'activate' );
116 $this->helper( 'ajax' );
117 $this->helper( 'api' );
118 $this->helper( 'blocks' );
119 $this->helper( 'cache' );
120 $this->helper( 'css' );
121 $this->helper( 'giveaway' );
122 $this->helper( 'meta_box' );
123 $this->helper( 'output' );
124 $this->helper( 'overview_page' );
125 $this->helper( 'plugin_action_link' );
126 $this->helper( 'relations' );
127 $this->helper( 'shortcode' );
128 $this->helper( 'support_tab' );
129 $this->helper( 'widget' );
130
131 // Include required helpers but don't instantiate
132 $this->include_helper( 'addons/addon' );
133 $this->include_helper( 'addons/premium_addon' );
134
135 // Load core addons
136 $this->helper( 'addon_loader' )->load_addons( $this->coreDir . '/addons' );
137
138 // Load default assets
139 $this->helper( 'assets' );
140 }
141
142 /**
143 * Access a helper. Will instantiate if helper hasn't been loaded before.
144 */
145 public function helper( $helper )
146 {
147 // Lazy instantiate helper
148 if( !isset( $this->helpers[$helper] ) ) {
149 $this->include_helper( $helper );
150
151 // Get class name from filename
152 $class_name = 'CRP';
153
154 $dirs = explode( '/', $helper );
155 $file = end( $dirs );
156 $name_parts = explode( '_', $file );
157 foreach( $name_parts as $name_part ) {
158 $class_name .= '_' . ucfirst( $name_part );
159 }
160
161 // Instantiate class if exists
162 if( class_exists( $class_name ) ) {
163 $this->helpers[$helper] = new $class_name();
164 }
165 }
166
167 // Return helper instance
168 return $this->helpers[$helper];
169 }
170
171 /**
172 * Include a helper. Looks through all helper directories that have been added.
173 */
174 public function include_helper( $helper )
175 {
176 foreach( $this->helper_dirs as $dir )
177 {
178 $file = $dir . '/'.$helper.'.php';
179
180 if( file_exists( $file ) ) {
181 require_once( $file );
182 }
183 }
184 }
185
186 /**
187 * Add a directory to look for helpers.
188 */
189 public function add_helper_directory( $dir )
190 {
191 if( is_dir( $dir ) ) {
192 $this->helper_dirs[] = $dir;
193 }
194 }
195
196 /*
197 * Quick access functions
198 */
199 public function relation_data( $post )
200 {
201 return $this->helper( 'relations' )->get_data( $post );
202 }
203
204 public function relations_from( $post_id )
205 {
206 return $this->helper( 'relations' )->get_from( $post_id );
207 }
208
209 public function relations_to( $post_id )
210 {
211 return $this->helper( 'relations' )->get_to( $post_id );
212 }
213 }
214
215 // Premium version is responsible for instantiating if available
216 if( !class_exists( 'CustomRelatedPostsPremium' ) ) {
217 CustomRelatedPosts::get();
218 }