| 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.8.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.8.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 |
add_action( 'init', array( $this, 'load_textdomain' ) ); |
| 101 |
} |
| 102 |
|
| 103 |
// Add core helper directory |
| 104 |
$this->add_helper_directory( $this->coreDir . '/helpers' ); |
| 105 |
|
| 106 |
// Migrate first if needed, then load settings. |
| 107 |
$this->helper( 'migration' ); |
| 108 |
$this->helper( 'settings' ); |
| 109 |
|
| 110 |
// Load required helpers |
| 111 |
$this->helper( 'activate' ); |
| 112 |
$this->helper( 'ajax' ); |
| 113 |
$this->helper( 'api' ); |
| 114 |
$this->helper( 'blocks' ); |
| 115 |
$this->helper( 'cache' ); |
| 116 |
$this->helper( 'css' ); |
| 117 |
$this->helper( 'giveaway' ); |
| 118 |
$this->helper( 'meta_box' ); |
| 119 |
$this->helper( 'output' ); |
| 120 |
$this->helper( 'overview_page' ); |
| 121 |
$this->helper( 'plugin_action_link' ); |
| 122 |
$this->helper( 'relations' ); |
| 123 |
$this->helper( 'shortcode' ); |
| 124 |
$this->helper( 'support_tab' ); |
| 125 |
|
| 126 |
// Include required helpers but don't instantiate |
| 127 |
$this->include_helper( 'widget' ); |
| 128 |
$this->include_helper( 'addons/addon' ); |
| 129 |
$this->include_helper( 'addons/premium_addon' ); |
| 130 |
|
| 131 |
// Load core addons |
| 132 |
$this->helper( 'addon_loader' )->load_addons( $this->coreDir . '/addons' ); |
| 133 |
|
| 134 |
// Load default assets |
| 135 |
$this->helper( 'assets' ); |
| 136 |
} |
| 137 |
|
| 138 |
public function load_textdomain() |
| 139 |
{ |
| 140 |
load_plugin_textdomain( 'custom-related-posts', false, $this->corePath . '/lang/' ); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Access a helper. Will instantiate if helper hasn't been loaded before. |
| 145 |
*/ |
| 146 |
public function helper( $helper ) |
| 147 |
{ |
| 148 |
// Lazy instantiate helper |
| 149 |
if( !isset( $this->helpers[$helper] ) ) { |
| 150 |
$this->include_helper( $helper ); |
| 151 |
|
| 152 |
// Get class name from filename |
| 153 |
$class_name = 'CRP'; |
| 154 |
|
| 155 |
$dirs = explode( '/', $helper ); |
| 156 |
$file = end( $dirs ); |
| 157 |
$name_parts = explode( '_', $file ); |
| 158 |
foreach( $name_parts as $name_part ) { |
| 159 |
$class_name .= '_' . ucfirst( $name_part ); |
| 160 |
} |
| 161 |
|
| 162 |
// Instantiate class if exists |
| 163 |
if( class_exists( $class_name ) ) { |
| 164 |
$this->helpers[$helper] = new $class_name(); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
// Return helper instance |
| 169 |
return $this->helpers[$helper]; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Include a helper. Looks through all helper directories that have been added. |
| 174 |
*/ |
| 175 |
public function include_helper( $helper ) |
| 176 |
{ |
| 177 |
foreach( $this->helper_dirs as $dir ) |
| 178 |
{ |
| 179 |
$file = $dir . '/'.$helper.'.php'; |
| 180 |
|
| 181 |
if( file_exists( $file ) ) { |
| 182 |
require_once( $file ); |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Add a directory to look for helpers. |
| 189 |
*/ |
| 190 |
public function add_helper_directory( $dir ) |
| 191 |
{ |
| 192 |
if( is_dir( $dir ) ) { |
| 193 |
$this->helper_dirs[] = $dir; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
/* |
| 198 |
* Quick access functions |
| 199 |
*/ |
| 200 |
public function relation_data( $post ) |
| 201 |
{ |
| 202 |
return $this->helper( 'relations' )->get_data( $post ); |
| 203 |
} |
| 204 |
|
| 205 |
public function relations_from( $post_id ) |
| 206 |
{ |
| 207 |
return $this->helper( 'relations' )->get_from( $post_id ); |
| 208 |
} |
| 209 |
|
| 210 |
public function relations_to( $post_id ) |
| 211 |
{ |
| 212 |
return $this->helper( 'relations' )->get_to( $post_id ); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
// Premium version is responsible for instantiating if available |
| 217 |
if( !class_exists( 'CustomRelatedPostsPremium' ) ) { |
| 218 |
CustomRelatedPosts::get(); |
| 219 |
} |