Set_Up_Automator::read_directory( $directory, bool $recursive = true )


Parameters Parameters

$directory

(Required)

$recursive

(bool) (Optional)

Default value: true


Top ↑

Return Return

(array|false)


Source Source

File: src/core/classes/class-set-up-automator.php

	public static function read_directory( $directory, $recursive = true ) {
		if ( is_dir( $directory ) === false ) {
			return false;
		}

		try {
			$resource          = opendir( $directory );
			$integration_files = array();
			while ( false !== ( $item = readdir( $resource ) ) ) { // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
				if ( '.' === (string) $item || '..' === (string) $item || 'index.php' === (string) $item ) {
					continue;
				}

				/**
				 * Ignore vendor folder in Integrations directory
				 */
				if ( 'vendor' === (string) $item && is_dir( $directory . DIRECTORY_SEPARATOR . $item ) ) {
					continue;
				}

				if ( true === $recursive && is_dir( $directory . DIRECTORY_SEPARATOR . $item ) ) {
					$dir                       = basename( $directory . DIRECTORY_SEPARATOR . $item );
					$integration_files[ $dir ] = self::read_directory( $directory . DIRECTORY_SEPARATOR . $item );
				} else {
					// only include files that have .php extension
					$ext = pathinfo( $item, PATHINFO_EXTENSION );
					if ( 'php' !== (string) $ext ) {
						continue;
					}
					if ( preg_match( '/(add-)/', $item ) ) {
						$integration_files['main'] = $directory . DIRECTORY_SEPARATOR . $item;
					} else {
						// Avoid Integromat fatal error if Pro < 3.0 and Free is >= 3.0
						if ( class_exists( '\Uncanny_Automator_Pro\InitializePlugin' ) ) {
							$version = \Uncanny_Automator_Pro\InitializePlugin::PLUGIN_VERSION;
							if ( version_compare( $version, '3.0', '<' ) ) {
								if ( 'integromat-pro-helpers.php' === (string) $item || strpos( $item, 'integromat-pro-helpers' ) ) {
									continue;
								}
							}
						}
						$integration_files[] = $directory . DIRECTORY_SEPARATOR . $item;
					}
				}
			}
		} catch ( Exception $e ) {
			throw new Automator_Exception( $e->getTraceAsString() );
		}

		return $integration_files;
	}