Skip to content
Snippets Groups Projects
Select Git revision
  • 1833ed5e9d14642f935a8cfcf90243671ba5eb66
  • master default protected
  • feat/message-handling
  • feat/request-id
  • 1.13.2
  • 1.13.1
  • 1.13.0
  • 1.12.1
  • 1.12.0
  • 1.11.0
  • 1.11.0-beta2
  • 1.11.0-beta1
  • 1.10.2
  • 1.10.1
  • 1.10.0
  • 1.9.0
  • 1.8.0
  • 1.7.4
  • 1.7.3
  • 1.7.2
  • 1.7.1
  • 1.7.0
  • 1.6.2
  • 1.6.2-beta2
24 results

Settings.php

Blame
  • wpinit 1.97 KiB
    #!/usr/bin/env php
    <?php
    
    use WPDesk\Init\Util\PhpFileDumper;
    use WPDesk\Init\Plugin\DefaultHeaderParser;
    
    $autoloadPath = $_composer_autoload_path ?? __DIR__ . '/../vendor/autoload.php';
    if (!file_exists($autoloadPath)) {
        echo "\033[31mError: Autoloader not found. Please run composer install.\033[0m\n";
        exit(1);
    }
    require_once $autoloadPath;
    
    function display_help() {
        echo "Usage: " . basename(__FILE__) . " <cache_dir>\n";
        echo "Initialize a WordPress plugin by parsing the input file and generating output.\n";
        echo "\n";
        echo "Arguments:\n";
        echo "  <cache_dir>   Path to the output file to generate\n";
    }
    
    function get_possible_plugin_files(): array {
    	$plugins_dir = opendir(getcwd());
    	$plugin_files = [];
    	while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
    		if ( str_starts_with( $file, '.' ) || str_ends_with( $file, '.php' ) === false || is_dir( $file ) ) {
    			continue;
    		}
    
    		$plugin_files[] = $file;
    	}
    
    	closedir( $plugins_dir );
    
    	return $plugin_files;
    }
    
    if (in_array('--help', $argv) || in_array('-h', $argv)) {
        display_help();
        exit(0);
    }
    
    if ($argc < 2 || in_array('--help', $argv) || in_array('-h', $argv)) {
        echo "\033[31mError: Missing required arguments.\033[0m\n";
    	display_help();
        exit(1);
    }
    
    $outputDir = rtrim($argv[1], '/\\') . '/';
    
    $parser = new DefaultHeaderParser();
    $plugin_header = [];
    
    foreach (get_possible_plugin_files() as $file) {
    	$plugin_header = $parser->parse($file);
    
    	if (! empty($plugin_header)) {
    		break;
    	}
    }
    
    if (empty($plugin_header)) {
    	echo "\033[31mError: No plugin data found. File has to be accessible from project's root folder and include required WordPress plugin headers.\033[0m\n";
    	exit(1);
    }
    
    try {
        $dumper = new PhpFileDumper();
        $dumper->dump($plugin_header, $outputDir . 'plugin.php');
    
        echo "\033[32mSuccess: Plugin data generated.\033[0m\n";
        exit(0);
    } catch (Exception $e) {
        echo "\033[31mError: " . $e->getMessage() . "\n" . $e->getTraceAsString() . "\033[0m\n";
        exit(1);
    }