/app/model/class-ms-model-gateway.php

  1. <?php 
  2. /** 
  3. * Register valid gateways. 
  4. * 
  5. * Gateways are stored in the directory /app/gateway/<gateway_name>/ 
  6. * Each Add-on must provide a file called `gateway-<gateway_name>.php` 
  7. * This file must define class MS_Gateway_<gateway_name>. 
  8. * This object is reponsible to initialize the the gateway logic. 
  9. * 
  10. * @since 1.0.0 
  11. * @package Membership2 
  12. * @subpackage Model 
  13. */ 
  14. class MS_Model_Gateway extends MS_Model_Option { 
  15.  
  16. /** 
  17. * List of gateway files to load when plugin is initialized. 
  18. * 
  19. * @since 1.0.0 
  20. * 
  21. * @var array of file-paths 
  22. */ 
  23. protected $gateway_files = array(); 
  24.  
  25. /** 
  26. * 
  27. * @since 1.0.0 
  28. * @var string $gateways 
  29. */ 
  30. protected static $_gateways = null; 
  31.  
  32. /** 
  33. * Load and get all registered gateways. 
  34. * 
  35. * @since 1.0.0 
  36. * @param bool $only_active Optional. When to return only activated gateways. 
  37. */ 
  38. public static function get_gateways( $only_active = false ) { 
  39. static $Done = false; 
  40. $res = null; 
  41.  
  42. if ( ! $Done ) { 
  43. self::$_gateways = array(); 
  44. $gateways = array(); 
  45. $Done = true; 
  46. self::load_core_gateways(); 
  47.  
  48. /** 
  49. * Register new gateways. 
  50. * 
  51. * @since 1.0.0 
  52. */ 
  53. $gateways = apply_filters( 
  54. 'ms_model_gateway_register',  
  55. $gateways 
  56. ); 
  57.  
  58. foreach ( $gateways as $key => $class ) { 
  59. self::$_gateways[ $key ] = MS_Factory::load( $class ); 
  60.  
  61. $res = self::$_gateways; 
  62.  
  63. if ( $only_active ) { 
  64. foreach ( $res as $id => $gateway ) { 
  65. if ( ! $gateway->active ) { 
  66. unset( $res[ $id ] ); 
  67.  
  68. return apply_filters( 
  69. 'ms_model_gateway_get_gateways',  
  70. $res,  
  71. $only_active 
  72. ); 
  73.  
  74. /** 
  75. * Checks if the specified gateway is active. 
  76. * 
  77. * @since 1.0.0 
  78. * @param string $gateway_id The gateway ID. 
  79. * @return bool True if the gateway is active. 
  80. */ 
  81. static public function is_active( $gateway_id ) { 
  82. $result = false; 
  83. $active_gateways = self::get_gateways( true ); 
  84.  
  85. if ( isset( $active_gateways[ $gateway_id ] ) ) { 
  86. $result = true; 
  87.  
  88. return $result; 
  89.  
  90. /** 
  91. * Checks the /app/gateway directory for a list of all gateways and loads 
  92. * these files. 
  93. * 
  94. * @since 1.0.0 
  95. */ 
  96. static protected function load_core_gateways() { 
  97. $model = MS_Factory::load( 'MS_Model_Gateway' ); 
  98. $content_dir = trailingslashit( dirname( dirname( MS_Plugin::instance()->dir ) ) ); 
  99. $plugin_dir = substr( MS_Plugin::instance()->dir, strlen( $content_dir ) ); 
  100.  
  101. $gateway_dirs = array(); 
  102.  
  103.  
  104. $gateway_dirs[] = $plugin_dir . 'app/gateway/'; 
  105.  
  106. if ( empty( $model->gateway_files ) || is_admin() ) { 
  107. // In Admin dashboard we always refresh the gateway-list... 
  108. $model->gateway_files = array(); 
  109.  
  110. foreach ( $gateway_dirs as $gateway_dir ) { 
  111. $mask = $content_dir . $gateway_dir . '*/class-ms-gateway-*.php'; 
  112. $gateways = glob( $mask ); 
  113.  
  114. foreach ( $gateways as $file ) { 
  115. $gateway = basename( $file ); 
  116. if ( empty( $model->gateway_files[ $gateway ] ) ) { 
  117. $gateway_path = substr( $file, strlen( $content_dir ) ); 
  118. $model->gateway_files[ $gateway ] = $gateway_path; 
  119.  
  120. /** 
  121. * Allow other plugins/themes to register custom gateways 
  122. * 
  123. * @since 1.0.0 
  124. * 
  125. * @var array 
  126. */ 
  127. $model->gateway_files = apply_filters( 
  128. 'ms_model_gateway_files',  
  129. $model->gateway_files 
  130. ); 
  131.  
  132. $model->save(); 
  133.  
  134. // Loop all recignized Gateways and initialize them. 
  135. foreach ( $model->gateway_files as $file ) { 
  136. $gateway_file = $content_dir . $file; 
  137.  
  138. // Get class-name from file-name 
  139. $class = basename( $file ); 
  140. $class = str_replace( '.php', '', $class ); 
  141. $class = implode( '_', array_map( 'ucfirst', explode( '-', $class ) ) ); 
  142. $class = substr( $class, 6 ); // remove 'Class_' prefix 
  143.  
  144. if ( file_exists( $gateway_file ) ) { 
  145. if ( ! class_exists( $class ) ) { 
  146. try { 
  147. include_once $gateway_file; 
  148. } catch ( Exception $ex ) { 
  149.  
  150. if ( class_exists( $class ) ) { 
  151. MS_Factory::load( $class ); 
  152.  
  153. /** 
  154. * Allow custom gateway-initialization code to run 
  155. * 
  156. * @since 1.0.0 
  157. */ 
  158. do_action( 'ms_model_gateway_load' ); 
  159.  
  160. /** 
  161. * Get all registered gateway names. 
  162. * 
  163. * @since 1.0.0 
  164. * @param bool $only_active Optional. False (default) returns only activated gateways. 
  165. * @param bool $include_gateway_free Optional. True (default) includes Gateway Free. 
  166. */ 
  167. public static function get_gateway_names( $only_active = false, $include_gateway_free = false ) { 
  168. $gateways = self::get_gateways( $only_active ); 
  169. $names = array(); 
  170.  
  171. foreach ( $gateways as $gateway ) { 
  172. $names[ $gateway->id ] = $gateway->name; 
  173.  
  174. if ( ! $include_gateway_free ) { 
  175. unset( $names[ MS_Gateway_Free::ID ] ); 
  176. } else { 
  177. $names['admin'] = __( 'None (Admin)', 'membership2' ); 
  178.  
  179. return apply_filters( 
  180. 'ms_model_gateway_get_gateway_names',  
  181. $names 
  182. ); 
  183.  
  184. /** 
  185. * Returns the gateway name for the specified gateway ID 
  186. * 
  187. * @since 1.0.0 
  188. * @api 
  189. * 
  190. * @param string $gateway_id The gateway ID. 
  191. * @param bool $get_short If set to true the word "Gateway" will be removed. 
  192. * @return string The gateway Name. 
  193. */ 
  194. public static function get_name( $gateway_id, $get_short = false ) { 
  195. static $Short_names = array(); 
  196. $known_names = self::get_gateway_names(); 
  197. $the_name = '-'; 
  198.  
  199. if ( isset( $known_names[ $gateway_id ] ) ) { 
  200. $the_name = $known_names[ $gateway_id ]; 
  201.  
  202. if ( $get_short ) { 
  203. if ( ! isset( $Short_names[ $gateway_id ] ) ) { 
  204. $Short_names[ $gateway_id ] = trim( 
  205. str_replace( 
  206. __( 'Gateway', 'membership2' ),  
  207. '',  
  208. $the_name 
  209. ); 
  210. $the_name = $Short_names[ $gateway_id ]; 
  211.  
  212. return $the_name; 
  213.  
  214. /** 
  215. * Validate gateway. 
  216. * 
  217. * @since 1.0.0 
  218. * @param string $gateway_id The gateway ID to validate. 
  219. */ 
  220. public static function is_valid_gateway( $gateway_id ) { 
  221. $valid = array_key_exists( $gateway_id, self::get_gateways() ); 
  222.  
  223. return apply_filters( 
  224. 'ms_model_gateway_is_valid_gateway',  
  225. $valid 
  226. ); 
  227.  
  228. /** 
  229. * Gateway factory. 
  230. * 
  231. * @since 1.0.0 
  232. * @param string $gateway_id The gateway ID to create. 
  233. */ 
  234. public static function factory( $gateway_id ) { 
  235. $gateway = null; 
  236.  
  237. if ( 'admin' == $gateway_id || empty( $gateway_id ) || 'gateway' == $gateway_id ) { 
  238. $gateway = MS_Factory::create( 'MS_Gateway' ); 
  239. } elseif ( self::is_valid_gateway( $gateway_id ) ) { 
  240. $gateways = self::get_gateways(); 
  241. $gateway = $gateways[ $gateway_id ]; 
  242.  
  243. return apply_filters( 
  244. 'ms_model_gateway_factory',  
  245. $gateway,  
  246. $gateway_id 
  247. ); 
.