Skip to content

Commit

Permalink
Merge pull request #3 from zngly/feat/dev-env
Browse files Browse the repository at this point in the history
Feat/dev env
  • Loading branch information
zngly-vlad authored Jul 21, 2022
2 parents 6a7e3f5 + 53df52a commit 4b7b04c
Show file tree
Hide file tree
Showing 8 changed files with 315 additions and 17 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ composer.lock
# OS specific
.DS_Store/

wordpress
wordpress

node_modules
package-lock.json
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ If an acf type needs a specific graphql type such as a custom enum then specify

Currently the mutations will not be set for objects with more than one child deep.

### Future Work
### Dev Usage

All mutations will be aggregated under a custom input type
- clone the repository
- make sure you have composer and npm installed on your system
- make sure you have XAMPP or something similar installed on your system
- this will install wordpress along with the plugin

- `composer install:dev`
- `composer watch`

- goto your wordpress installation in your localhost browser
- finish the wordpress installation
- activate all plugins
29 changes: 16 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
"installer-paths": {
"wordpress/wp-content/plugins/{$name}/": [
"type:wordpress-plugin"
],
"wordpress/wp-content/themes/{$name}/": [
"type:wordpress-theme"
]
}
},
Expand All @@ -54,28 +57,28 @@
"johnpbloch/wordpress": ">=6.0",
"wp-graphql/wp-graphql": "^1.8",
"wp-graphql/wp-graphql-acf": "^0.5.3",
"wpackagist-plugin/advanced-custom-fields": "dev-trunk"
"wordpress-plugin/advanced-custom-fields-pro": "^5.9",
"wpackagist-theme/twentytwentytwo": "*",
"spatie/file-system-watcher": "^1.1"
},
"autoload": {
"psr-4": {
"WPGraphQL\\ACF\\Mutations\\": "src/"
"WPGraphQL\\ACF\\Mutations\\": "src/",
"Zngly\\ACF\\Scripts\\": "scripts/",
"\\": "wordpress/vendor/"
},
"classmap": [
"src/"
"src",
"scripts",
"wordpress/vendor"
]
},
"scripts": {
"test": [
"@php vendor/bin/phpunit"
"install:dev": [
"Zngly\\ACF\\Scripts\\Install::run"
],
"psr2check": [
"@php vendor/bin/phpcs --standard=PSR2 src/"
],
"psr2autofix": [
"@php vendor/bin/phpcbf --standard=PSR2 src/"
],
"docs": [
"@php vendor/bin/phpdoc -d \"src\" -t \"docs\""
"watch": [
"Zngly\\ACF\\Scripts\\Watcher::run"
]
}
}
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "wp-graphql-acf-mutations",
"version": "1.0.0",
"description": "wp-graphql-acf-mutations",
"homepage": "https://github.com/zngly/wp-graphql-acf-mutations#readme",
"author": "Vlad-Anton Medves",
"license": "ISC",
"bugs": {
"url": "https://github.com/zngly/wp-graphql-acf-mutations/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/zngly/wp-graphql-acf-mutations.git"
},
"scripts": {},
"devDependencies": {
"chokidar": "^3.5.3"
}
}
89 changes: 89 additions & 0 deletions scripts/Install.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace Zngly\ACF\Scripts;

use Zngly\ACF\Scripts\Utils;


class Install
{

// create root_dir variable
private string $root_dir;

// singleton
private static $instance = null;

// construct
public function __construct()
{
$this->root_dir = dirname(dirname(__FILE__));
}

public static function getInstance()
{
if (self::$instance == null)
self::$instance = new Install();
return self::$instance;
}

public static function run()
{

self::getInstance()->wordpress();
self::getInstance()->npm();
}

private function wordpress()
{
self::print("Installing Wordpress...\n");

// install wordpress
$command = "cd {$this->root_dir} && composer install --prefer-dist --no-interaction";
echo "running: " . $command . "\n";
$output = shell_exec($command);
echo $output;

self::$instance->modify_wordpress();

self::print("Wordpress finished.\n");
}

private function modify_wordpress()
{
self::print("Modifying Wordpress...\n");

$wordpress_dir = $this->root_dir . '/wordpress';
$wp_config_path = $wordpress_dir . '/wp-config-sample.php';

Utils::add_to_file($wp_config_path, "if(!defined('ABSPATH'))", 'require ABSPATH . "vendor/autoload.php";');

// delete all themes except twentytwentytwo
$themes_dir = $wordpress_dir . '/wp-content/themes';
$themes = scandir($themes_dir);
foreach ($themes as $theme) {
if ($theme != '.' && $theme != '..' && $theme != 'twentytwentytwo') {
$theme_path = $themes_dir . '/' . $theme;
Utils::delete_dir($theme_path);
}
}

Utils::copy_plugin();
}

private function npm()
{
self::print("Installing NPM...\n");

// install npm
$command = "cd {$this->root_dir} && npm install --no-save";
echo "running: " . $command . "\n";
$output = shell_exec($command);
echo $output;
}

public static function print(string $message)
{
echo "\n" . $message . "\n";
}
}
112 changes: 112 additions & 0 deletions scripts/Utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace Zngly\ACF\Scripts;

class Utils
{
/**
* Modifies a file by adding a string if a match is found.
* The string added is added two lines before the match.
*
* @param string $file_path
* @param string $match_string - the match will be compared without any spaces
* @param string $replace_string
*/
public static function add_to_file(string $file_path, string $match_str, string $replace_str)
{
// update file
$lines = file($file_path);

// check for error, throw error if file is not found
if (!$lines)
throw new \Exception("File not found: " . $file_path);


$new_config = [];

foreach ($lines as $key => $value) {
$trim_val = preg_replace('/\s+/', '', $value);
$trim_replace = preg_replace('/\s+/', '', $replace_str);

// if the replace string exists, exit gracefully
if (strpos($trim_val, $trim_replace) !== false)
return;

// we add our code before this line
if (strpos($trim_val, $match_str) !== false)
$new_config[] = "{$replace_str}\n\n";

// add the new line
$new_config[] = $value;
}

$new_file_content = implode("", $new_config);
file_put_contents($file_path, $new_file_content);
}

// delete a directory and all its contents
public static function delete_dir(string $dir)
{
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (filetype($dir . "/" . $object) == "dir")
self::delete_dir($dir . "/" . $object);
else
unlink($dir . "/" . $object);
}
}
reset($objects);
rmdir($dir);
}
}

public static function copy_plugin()
{
// get the directory to watch
$src_folder = dirname(dirname(__FILE__)) . '/src';

// get all the files in the directory recursively
$plugin_name = self::get_plugin_name();
$plugin_folder = self::get_root_dir() . "/wordpress/wp-content/plugins/{$plugin_name}";

// if the plugin folder doesn't exist, create it
if (!file_exists("{$plugin_folder}"))
mkdir("{$plugin_folder}/", 0777, true);

// copy plugin_idx to the plugins directory
copy(self::get_root_dir() . "/{$plugin_name}.php", "{$plugin_folder}/{$plugin_name}.php");

// copy the src folder and the contents recursively to the plugins directory
self::copy_folder($src_folder . '/', $plugin_folder . '/src');
}

// function to copy a folder recursively
public static function copy_folder($src, $dest)
{
$dir = opendir($src);
@mkdir($dest);
while (false !== ($file = readdir($dir)))
if (($file != '.') && ($file != '..'))
if (is_dir($src . '/' . $file)) self::copy_folder($src . '/' . $file, $dest . '/' . $file);
else copy($src . '/' . $file, $dest . '/' . $file);
closedir($dir);
}


public static function get_root_dir()
{
return dirname(dirname(__FILE__));
}

public static function get_root_name()
{
return basename(dirname(dirname(__FILE__)));
}

public static function get_plugin_name()
{
return basename(dirname(dirname(__FILE__)));
}
}
62 changes: 62 additions & 0 deletions scripts/Watcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Zngly\ACF\Scripts;

require dirname(dirname(__FILE__)) . '/wordpress/vendor/autoload.php';

use Spatie\Watcher\Watch;


// class will watch a directory for changes and run a command when a file is added or modified
class Watcher
{

public static function run()
{
// get the directory to watch
$src_folder = dirname(dirname(__FILE__)) . '/src';

// get all the files in the directory recursively
$plugin_name = Utils::get_plugin_name();
$plugin_folder = Utils::get_root_dir() . "/wordpress/wp-content/plugins/{$plugin_name}";

// watch for any file changes in the directory
self::watch_dirs([$src_folder, "{$plugin_folder}/{$plugin_name}.php"]);
}



// function to watch a directory for changes
public static function watch_dirs(array $dirs)
{
$root_dir = Utils::get_root_dir();
$plugins_folder = $root_dir . "/wordpress/wp-content/plugins/" . Utils::get_plugin_name();

Watch::paths(...$dirs)
->onAnyChange(function (string $type, string $path) use ($root_dir, $plugins_folder) {
$path_parts = explode($root_dir, $path);
$new_path = $plugins_folder . $path_parts[1];

switch ($type) {
case Watch::EVENT_TYPE_DIRECTORY_CREATED:
mkdir($new_path, 0777, true);
break;
case Watch::EVENT_TYPE_DIRECTORY_DELETED:
rmdir($new_path);
break;
case Watch::EVENT_TYPE_FILE_DELETED:
unlink($new_path);
break;
case Watch::EVENT_TYPE_FILE_CREATED:
case Watch::EVENT_TYPE_FILE_UPDATED:
copy($path, $new_path);
break;

default:
echo "Event Happened: {$type} - from {$path} - to {$new_path}\n";
break;
}
})
->start();
}
}
2 changes: 1 addition & 1 deletion wp-graphql-acf-mutations.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* Plugin Name: WPGraphQL for Advanced Custom Fields - ACF Mutations
* Plugin Name: WPGraphQL ACF Mutations
* Description: Adds Advanced Custom Fields Mutations to the WPGraphQL Schema
* Author: Vlad-Anton Medves
* Text Domain: wp-graphql-acf
Expand Down

0 comments on commit 4b7b04c

Please sign in to comment.