| 1 |
<?php |
| 2 |
namespace LWS\WOOREWARDS\PointsFlow; |
| 3 |
|
| 4 |
// don't call the file directly |
| 5 |
if( !defined( 'ABSPATH' ) ) exit(); |
| 6 |
|
| 7 |
/** Point export method extends this abstract and are placed in methods/ subdir. */ |
| 8 |
class ExportMethods |
| 9 |
{ |
| 10 |
private $methods = false; |
| 11 |
|
| 12 |
static function get($metaKey) |
| 13 |
{ |
| 14 |
$me = new self(); |
| 15 |
return $me->getMethod($metaKey); |
| 16 |
} |
| 17 |
|
| 18 |
function getMethod($metaKey) |
| 19 |
{ |
| 20 |
$methods = $this->getFiles(); |
| 21 |
if( isset($methods[$metaKey]) ) |
| 22 |
return $methods[$metaKey]; |
| 23 |
else if( isset($methods['lws_woorewards_pro_pointsflow_methods_metakey']) ) |
| 24 |
return $methods['lws_woorewards_pro_pointsflow_methods_metakey']; |
| 25 |
else |
| 26 |
return false; |
| 27 |
} |
| 28 |
|
| 29 |
function getArguments() |
| 30 |
{ |
| 31 |
$methods = array(array( |
| 32 |
'value' => '—', |
| 33 |
'label' => '—', |
| 34 |
)); |
| 35 |
foreach( $this->getFiles() as $method ) |
| 36 |
{ |
| 37 |
if( $method->instance->isVisible() && ($args = $method->instance->getArgs()) ) |
| 38 |
{ |
| 39 |
$sub = array(); |
| 40 |
foreach( $args as $value => $label ) |
| 41 |
$sub[] = array('value' => $value, 'label' => $label); |
| 42 |
|
| 43 |
$methods[] = array( |
| 44 |
'value' => $method->instance->getKey(), |
| 45 |
'label' => $method->instance->getTitle(), |
| 46 |
'group' => $sub, |
| 47 |
); |
| 48 |
} |
| 49 |
} |
| 50 |
return $methods; |
| 51 |
} |
| 52 |
|
| 53 |
function getMethods() |
| 54 |
{ |
| 55 |
$methods = array(array( |
| 56 |
'value' => '—', |
| 57 |
'label' => '—', |
| 58 |
)); |
| 59 |
foreach( $this->getFiles() as $method ) |
| 60 |
{ |
| 61 |
if( $method->instance->isVisible() ) |
| 62 |
{ |
| 63 |
$methods[] = array( |
| 64 |
'value' => $method->instance->getKey(), |
| 65 |
'label' => $method->instance->getTitle(), |
| 66 |
); |
| 67 |
} |
| 68 |
} |
| 69 |
return $methods; |
| 70 |
} |
| 71 |
|
| 72 |
private function getFiles() |
| 73 |
{ |
| 74 |
if (false === $this->methods) |
| 75 |
{ |
| 76 |
$path = LWS_WOOREWARDS_INCLUDES . '/pointsflow'; |
| 77 |
require_once $path.'/exportmethod.php'; |
| 78 |
$this->methods = array(); |
| 79 |
|
| 80 |
foreach( glob($path.'/methods/*.php') as $file ) |
| 81 |
{ |
| 82 |
$basename = basename($file); |
| 83 |
if( !($basename != 'index.php' && false === strpos($basename, '..') && is_file($file)) ) |
| 84 |
continue; |
| 85 |
|
| 86 |
$match = array(); |
| 87 |
if( !preg_match('/^class (\w+)\s+/im', file_get_contents($file), $match) ) |
| 88 |
continue; |
| 89 |
|
| 90 |
@include_once $file; |
| 91 |
$classname = '\LWS\WOOREWARDS\PointsFlow\Methods\\'.$match[1]; |
| 92 |
if( class_exists($classname) ) |
| 93 |
{ |
| 94 |
$method = new $classname(); |
| 95 |
if( \is_a($method, '\LWS\WOOREWARDS\PointsFlow\ExportMethod') ) |
| 96 |
{ |
| 97 |
$key = $method->getKey(); |
| 98 |
$this->methods[$key] = (object)array( |
| 99 |
'classname' => $classname, |
| 100 |
'file' => $file, |
| 101 |
'instance' => $method, |
| 102 |
); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
return $this->methods; |
| 108 |
} |
| 109 |
} |
| 110 |
|