| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ability: list the available NotificationX data sources (extensions). |
| 4 |
* |
| 5 |
* @package NotificationX\Abilities\Read |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace NotificationX\Abilities\Read; |
| 9 |
|
| 10 |
use NotificationX\Abilities\AbilityBase; |
| 11 |
use NotificationX\Extensions\ExtensionFactory; |
| 12 |
use NotificationX\Core\Modules; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Returns the data sources/integrations and whether their module is active. |
| 20 |
*/ |
| 21 |
class ListSources extends AbilityBase { |
| 22 |
|
| 23 |
protected $id = 'notificationx/list-sources'; |
| 24 |
protected $label = 'List data sources'; |
| 25 |
protected $description = 'List the data sources/integrations (e.g. WooCommerce, Comments, Contact Form) that can power a notification, which notification type each belongs to, whether it is Pro-only, and whether its module is currently enabled.'; |
| 26 |
|
| 27 |
public function input_schema() { |
| 28 |
return array( |
| 29 |
'type' => 'object', |
| 30 |
'properties' => (object) array(), |
| 31 |
); |
| 32 |
} |
| 33 |
|
| 34 |
public function output_schema() { |
| 35 |
return array( |
| 36 |
'type' => 'object', |
| 37 |
'properties' => array( |
| 38 |
'count' => array( 'type' => 'integer' ), |
| 39 |
'sources' => array( 'type' => 'array' ), |
| 40 |
), |
| 41 |
); |
| 42 |
} |
| 43 |
|
| 44 |
public function execute( $input ) { |
| 45 |
$extensions = ExtensionFactory::get_instance()->get_all(); |
| 46 |
$modules = Modules::get_instance(); |
| 47 |
$result = array(); |
| 48 |
|
| 49 |
if ( is_array( $extensions ) ) { |
| 50 |
foreach ( $extensions as $key => $ext ) { |
| 51 |
if ( ! is_object( $ext ) ) { |
| 52 |
continue; |
| 53 |
} |
| 54 |
$module = isset( $ext->module ) ? $ext->module : ''; |
| 55 |
$result[] = array( |
| 56 |
'id' => isset( $ext->id ) ? $ext->id : (string) $key, |
| 57 |
'title' => isset( $ext->title ) ? $ext->title : '', |
| 58 |
'type' => isset( $ext->types ) ? $ext->types : '', |
| 59 |
'is_pro' => ! empty( $ext->is_pro ), |
| 60 |
'module' => $module, |
| 61 |
'module_enabled' => $module ? (bool) $modules->is_enabled( $module ) : true, |
| 62 |
); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
return array( |
| 67 |
'count' => count( $result ), |
| 68 |
'sources' => $result, |
| 69 |
); |
| 70 |
} |
| 71 |
} |
| 72 |
|