/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 
  60. [$key] = MS_Factory::load( $class ); 
  61.  
  62. $res = self::$_gateways; 
  63.  
  64. if ( $only_active ) { 
  65. foreach ( $res as $id => $gateway ) { 
  66. if ( ! $gateway->active ) { 
  67. unset( $res[ $id ] ); 
  68.  
  69. return apply_filters( 
  70. 'ms_model_gateway_get_gateways',  
  71. $res,  
  72. $only_active 
  73. ); 
  74.  
  75. /** 
  76. * Checks if the specified gateway is active. 
  77. * 
  78. * @since 1.0.0 
  79. * @param string $gateway_id The gateway ID. 
  80. * @return bool True if the gateway is active. 
  81. */ 
  82. static public function is_active( $gateway_id ) { 
  83. $result = false; 
  84. $active_gateways = self::get_gateways( true ); 
  85.  
  86. if ( isset( $active_gateways[ $gateway_id ] ) ) { 
  87. $result = true; 
  88.  
  89. return $result; 
  90.  
  91. /** 
  92. * Checks the /app/gateway directory for a list of all gateways and loads 
  93. * these files. 
  94. * 
  95. * @since 1.0.0 
  96. */ 
  97. static protected function load_core_gateways() { 
  98. $model = MS_Factory::load( 'MS_Model_Gateway' ); 
  99. $root_path = trailingslashit( dirname( dirname( MS_Plugin::instance()->dir ) ) ); 
  100. $plugin_dir = substr( MS_Plugin::instance()->dir, strlen( $root_path ) ); 
  101. $gateway_dir = $plugin_dir . 'app/gateway/'; 
  102.  
  103. if ( empty( $model->gateway_files ) || is_admin() ) { 
  104. // In Admin dashboard we always refresh the gateway-list... 
  105.  
  106. $mask = $root_path . $gateway_dir . '*/class-ms-gateway-*.php'; 
  107. $gateways = glob( $mask ); 
  108.  
  109. $model->gateway_files = array(); 
  110. foreach ( $gateways as $file ) { 
  111. $model->gateway_files[] = substr( $file, strlen( $root_path ) ); 
  112.  
  113. /** 
  114. * Allow other plugins/themes to register custom gateways 
  115. * 
  116. * @since 1.0.0 
  117. * 
  118. * @var array 
  119. */ 
  120. $model->gateway_files = apply_filters( 
  121. 'ms_model_gateway_files',  
  122. $model->gateway_files 
  123. ); 
  124.  
  125. $model->save(); 
  126.  
  127. // Loop all recignized Gateways and initialize them. 
  128. foreach ( $model->gateway_files as $file ) { 
  129. $gateway_file = $root_path . $file; 
  130.  
  131. // Get class-name from file-name 
  132. $class = basename( $file ); 
  133. $class = str_replace( '.php', '', $class ); 
  134. $class = implode( '_', array_map( 'ucfirst', explode( '-', $class ) ) ); 
  135. $class = substr( $class, 6 ); // remove 'Class_' prefix 
  136.  
  137. if ( file_exists( $gateway_file ) ) { 
  138. if ( ! class_exists( $class ) ) { 
  139. try { 
  140. include_once $gateway_file; 
  141. } catch ( Exception $ex ) { 
  142.  
  143. if ( class_exists( $class ) ) { 
  144. MS_Factory::load( $class ); 
  145.  
  146. /** 
  147. * Allow custom gateway-initialization code to run 
  148. * 
  149. * @since 1.0.0 
  150. */ 
  151. do_action( 'ms_model_gateway_load' ); 
  152.  
  153. /** 
  154. * Get all registered gateway names. 
  155. * 
  156. * @since 1.0.0 
  157. * @param bool $only_active Optional. False (default) returns only activated gateways. 
  158. * @param bool $include_gateway_free Optional. True (default) includes Gateway Free. 
  159. */ 
  160. public static function get_gateway_names( $only_active = false, $include_gateway_free = false ) { 
  161. $gateways = self::get_gateways( $only_active ); 
  162. $names = array(); 
  163.  
  164. foreach ( $gateways as $gateway ) { 
  165. $names[ $gateway->id ] = $gateway->name; 
  166.  
  167. if ( ! $include_gateway_free ) { 
  168. unset( $names[ MS_Gateway_Free::ID ] ); 
  169. } else { 
  170. $names['admin'] = __( 'None (Admin)', 'membership2' ); 
  171.  
  172. return apply_filters( 
  173. 'ms_model_gateway_get_gateway_names',  
  174. $names 
  175. ); 
  176.  
  177. /** 
  178. * Returns the gateway name for the specified gateway ID 
  179. * 
  180. * @since 1.0.0 
  181. * @api 
  182. * 
  183. * @param string $gateway_id The gateway ID. 
  184. * @param bool $get_short If set to true the word "Gateway" will be removed. 
  185. * @return string The gateway Name. 
  186. */ 
  187. public static function get_name( $gateway_id, $get_short = false ) { 
  188. static $Short_names = array(); 
  189. $known_names = self::get_gateway_names(); 
  190. $the_name = '-'; 
  191.  
  192. if ( isset( $known_names[$gateway_id] ) ) { 
  193. $the_name = $known_names[$gateway_id]; 
  194.  
  195. if ( $get_short ) { 
  196. if ( ! isset( $Short_names[$gateway_id] ) ) { 
  197. $Short_names[$gateway_id] = trim( 
  198. str_replace( 
  199. __( 'Gateway', 'membership2' ),  
  200. '',  
  201. $the_name 
  202. ); 
  203. $the_name = $Short_names[$gateway_id]; 
  204.  
  205. return $the_name; 
  206.  
  207. /** 
  208. * Validate gateway. 
  209. * 
  210. * @since 1.0.0 
  211. * @param string $gateway_id The gateway ID to validate. 
  212. */ 
  213. public static function is_valid_gateway( $gateway_id ) { 
  214. $valid = array_key_exists( $gateway_id, self::get_gateways() ); 
  215.  
  216. return apply_filters( 
  217. 'ms_model_gateway_is_valid_gateway',  
  218. $valid 
  219. ); 
  220.  
  221. /** 
  222. * Gateway factory. 
  223. * 
  224. * @since 1.0.0 
  225. * @param string $gateway_id The gateway ID to create. 
  226. */ 
  227. public static function factory( $gateway_id ) { 
  228. $gateway = null; 
  229.  
  230. if ( 'admin' == $gateway_id || empty( $gateway_id ) || 'gateway' == $gateway_id ) { 
  231. $gateway = MS_Factory::create( 'MS_Gateway' ); 
  232. } elseif ( self::is_valid_gateway( $gateway_id ) ) { 
  233. $gateways = self::get_gateways(); 
  234. $gateway = $gateways[ $gateway_id ]; 
  235.  
  236. return apply_filters( 
  237. 'ms_model_gateway_factory',  
  238. $gateway,  
  239. $gateway_id 
  240. ); 
  241.  
.