| 1 |
<?php |
| 2 |
/** |
| 3 |
* Finds every place that ships help articles: the core, plus each registered |
| 4 |
* addon that has a `help/` directory next to its `src/`. |
| 5 |
* |
| 6 |
* Addons need no code for this. An addon whose layout differs adds itself |
| 7 |
* through the `f12_doi_help_sources` filter. |
| 8 |
* |
| 9 |
* @package Forge12\DoubleOptIn\Help |
| 10 |
* @since 5.11.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
declare( strict_types=1 ); |
| 14 |
|
| 15 |
namespace Forge12\DoubleOptIn\Help; |
| 16 |
|
| 17 |
use Forge12\DoubleOptIn\Addon\AddonInterface; |
| 18 |
use Forge12\DoubleOptIn\Addon\AddonRegistry; |
| 19 |
|
| 20 |
if ( ! defined( 'ABSPATH' ) ) { |
| 21 |
exit; |
| 22 |
} |
| 23 |
|
| 24 |
class HelpSourceLocator { |
| 25 |
|
| 26 |
/** |
| 27 |
* @return HelpSource[] |
| 28 |
*/ |
| 29 |
public function locate(): array { |
| 30 |
$coreDir = dirname( __DIR__, 2 ); |
| 31 |
$sources = array(); |
| 32 |
|
| 33 |
if ( is_dir( $coreDir . '/help' ) ) { |
| 34 |
$sources[] = new HelpSource( |
| 35 |
'core', |
| 36 |
__( 'Double Opt-In', 'double-opt-in' ), |
| 37 |
$coreDir . '/help', |
| 38 |
plugins_url( 'help/', F12_DOUBLEOPTIN_PLUGIN_FILE ), |
| 39 |
HelpSource::GROUP_CORE |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
$addons = AddonRegistry::getInstance()->all(); |
| 44 |
uasort( |
| 45 |
$addons, |
| 46 |
static function ( AddonInterface $a, AddonInterface $b ): int { |
| 47 |
return strcmp( $a->getName(), $b->getName() ); |
| 48 |
} |
| 49 |
); |
| 50 |
foreach ( $addons as $id => $addon ) { |
| 51 |
$source = $this->sourceForAddon( (string) $id, $addon ); |
| 52 |
if ( null !== $source ) { |
| 53 |
$sources[] = $source; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Lets a plugin add or replace help sources. |
| 59 |
* |
| 60 |
* @param HelpSource[] $sources |
| 61 |
*/ |
| 62 |
$filtered = apply_filters( 'f12_doi_help_sources', $sources ); |
| 63 |
if ( ! is_array( $filtered ) ) { |
| 64 |
return $sources; |
| 65 |
} |
| 66 |
return array_values( |
| 67 |
array_filter( |
| 68 |
$filtered, |
| 69 |
static function ( $source ): bool { |
| 70 |
return $source instanceof HelpSource; |
| 71 |
} |
| 72 |
) |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
private function sourceForAddon( string $id, AddonInterface $addon ): ?HelpSource { |
| 77 |
$file = ( new \ReflectionClass( $addon ) )->getFileName(); |
| 78 |
if ( false === $file ) { |
| 79 |
return null; |
| 80 |
} |
| 81 |
// <addon>/src/<Name>Addon.php -> <addon> |
| 82 |
$root = dirname( $file, 2 ); |
| 83 |
$dir = $root . '/help'; |
| 84 |
if ( ! is_dir( $dir ) ) { |
| 85 |
return null; |
| 86 |
} |
| 87 |
|
| 88 |
$group = HelpSource::GROUP_MODULE; |
| 89 |
foreach ( $addon->getCapabilities() as $capability ) { |
| 90 |
if ( 0 === strpos( (string) $capability, 'form.' ) ) { |
| 91 |
$group = HelpSource::GROUP_INTEGRATION; |
| 92 |
break; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
return new HelpSource( |
| 97 |
sanitize_key( $id ), |
| 98 |
$addon->getName(), |
| 99 |
$dir, |
| 100 |
plugins_url( 'help/', $root . '/addon.php' ), |
| 101 |
$group |
| 102 |
); |
| 103 |
} |
| 104 |
} |
| 105 |
|