MS_Model_Gateway

Register valid gateways.

Defined (1)

The class is defined in the following location(s).

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