Skip to content

Commit

Permalink
Merge pull request #23 from newfold-labs/enhance/data-plugin-install-…
Browse files Browse the repository at this point in the history
…script

Event Listener in Installer Module for Premium Plugin Installation
  • Loading branch information
arunshenoy99 authored Oct 8, 2024
2 parents 125e82b + e8d1f5b commit e932c7d
Show file tree
Hide file tree
Showing 12 changed files with 18,598 additions and 6 deletions.
74 changes: 74 additions & 0 deletions .github/workflows/lint-check-spa.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: "JS Lint Checker: Installer Module"
on:
workflow_dispatch:
push:
paths:
- "src/**/*.js"
- "src/**/*.scss"
pull_request:
types: [opened, edited, reopened, ready_for_review]
paths:
- "src/**/*.js"
- "src/**/*.scss"

concurrency:
group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.sha }}
cancel-in-progress: true

jobs:
lint-check-spa:
name: Run Lint Checks
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v3

# Install Node and npm
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 16.x
cache: 'npm'

# Checks if node_modules exists in the cache.
- name: Cache node_modules directory
id: cache
uses: actions/cache@v3
with:
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: ${{ runner.os }}-node-

- name: Setup Registry
run: printf "@newfold-labs:registry=https://npm.pkg.github.com/\n//npm.pkg.github.com/:_authToken=${{ secrets.NPM_TOKEN }}" > .npmrc
if: steps.cache.outputs.cache-hit != 'true'

# Installs @wordpress/scripts for lint checks if it does not exist in the cache.
- name: Install dependencies
run: npm i @wordpress/[email protected] --legacy-peer-deps
if: steps.cache.outputs.cache-hit != 'true'

# Gets the files changed wrt to trunk and filters out the js files.
- uses: technote-space/get-diff-action@v6
with:
PATTERNS: |
+(src)/**/*.js
# Runs wp-scripts for checking JS coding issues.
- name: Run JS Lint
id: js-lint
run: npx wp-scripts lint-js ${{ env.GIT_DIFF }}
if: "!! env.GIT_DIFF"

# Gets the files changed wrt to trunk and filters out the SASS files.
- uses: technote-space/get-diff-action@v6
with:
PATTERNS: |
+(src)/**/*.scss
if: ${{ success() || steps.js-lint.conclusion == 'failure' }}

# Runs wp-scripts for checking SASS coding issues.
- name: Run SASS Lint
id: sass-lint
run: npx wp-scripts lint-style ${{ env.GIT_DIFF }}
if: ${{ (!! env.GIT_DIFF) && (success() || steps.js-lint.conclusion == 'failure') }}
10 changes: 4 additions & 6 deletions bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

use NewfoldLabs\WP\Module\Installer\Installer;
use NewfoldLabs\WP\ModuleLoader\Container;
use NewfoldLabs\WP\Module\Installer\Installer;
use NewfoldLabs\WP\Module\Installer\Data\Constants;

use function NewfoldLabs\WP\ModuleLoader\register;

if ( function_exists( 'add_action' ) ) {
Expand All @@ -15,11 +17,7 @@ function () {
'name' => 'installer',
'label' => __( 'Installer', 'wp-module-installer' ),
'callback' => function ( Container $container ) {

if ( ! defined( 'NFD_INSTALLER_VERSION' ) ) {
define( 'NFD_INSTALLER_VERSION', '1.1.5' );
}

new Constants( $container );
new Installer( $container );
},
'isActive' => true,
Expand Down
27 changes: 27 additions & 0 deletions includes/Data/Constants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace NewfoldLabs\WP\Module\Installer\Data;

use NewfoldLabs\WP\ModuleLoader\Container;

/**
* Manages all the constants for the module.
*/
class Constants {
/**
* Constructor for the Constants class.
*
* @param Container $container The module container.
*/
public function __construct( $container ) {
if ( ! defined( 'NFD_INSTALLER_VERSION' ) ) {
define( 'NFD_INSTALLER_VERSION', '1.1.5' );
}
if ( ! defined( 'NFD_INSTALLER_BUILD_DIR' ) && defined( 'NFD_INSTALLER_VERSION' ) ) {
define( 'NFD_INSTALLER_BUILD_DIR', dirname( __DIR__, 2 ) . '/build/' . NFD_INSTALLER_VERSION );
}
if ( ! defined( 'NFD_INSTALLER_BUILD_URL' && defined( 'NFD_INSTALLER_VERSION' ) ) ) {
define( 'NFD_INSTALLER_BUILD_URL', $container->plugin()->url . '/vendor/newfold-labs/wp-module-installer/build/' . NFD_INSTALLER_VERSION );
}
}
}
5 changes: 5 additions & 0 deletions includes/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace NewfoldLabs\WP\Module\Installer;

use NewfoldLabs\WP\Module\Installer\RestApi\RestApi;
use NewfoldLabs\WP\Module\Installer\WPAdmin\WPAdmin;
use NewfoldLabs\WP\Module\Installer\TaskManagers\TaskManager;
use NewfoldLabs\WP\Module\Installer\WPCLI\WPCLI;
use NewfoldLabs\WP\ModuleLoader\Container;
Expand Down Expand Up @@ -35,5 +36,9 @@ public function __construct( Container $container ) {
new TaskManager();

new WPCLI();

if ( Permissions::rest_is_authorized_admin() ) {
new WPAdmin();
}
}
}
10 changes: 10 additions & 0 deletions includes/RestApi/PluginsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ public function get_install_plugin_args() {
'type' => 'integer',
'default' => 0,
),
'premium' => array(
'type' => 'boolean',
'default' => false,
),
);
}

Expand Down Expand Up @@ -178,6 +182,12 @@ public function install( \WP_REST_Request $request ) {
$activate = $request->get_param( 'activate' );
$queue = $request->get_param( 'queue' );
$priority = $request->get_param( 'priority' );
$premium = $request->get_param( 'premium' );

// Checks if the plugin is premium and uses the corresponding function for it.
if ( true === $premium ) {
return PluginInstaller::install_premium_plugin( $plugin, $activate );
}

// Checks if a plugin with the given slug and activation criteria already exists.
if ( PluginInstaller::exists( $plugin, $activate ) ) {
Expand Down
51 changes: 51 additions & 0 deletions includes/WPAdmin/Listeners/DataAttrListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace NewfoldLabs\WP\Module\Installer\WPAdmin\Listeners;

use NewfoldLabs\WP\Module\Installer\Services\PluginInstaller;

/**
* Manages all the data-* listening related functionalities for the module.
*/
class DataAttrListener {
/**
* Constructor for the DataAttrListener class.
*/
public function __construct() {
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_data_attr_listener' ) );
}

/**
* Enqueues the data-* attribute listener script.
*
* @return void
*/
public function enqueue_data_attr_listener() {
$asset_file = NFD_INSTALLER_BUILD_DIR . '/dataAttrListener.asset.php';

if ( is_readable( $asset_file ) ) {
$asset = include $asset_file;

wp_register_script(
'nfd-installer-data-attr-listener',
NFD_INSTALLER_BUILD_URL . '/dataAttrListener.js',
array_merge( $asset['dependencies'], array() ),
$asset['version'],
true
);

wp_add_inline_script(
'nfd-installer-data-attr-listener',
'var nfdInstaller =' . wp_json_encode(
value: array(
'restUrl' => \get_home_url() . '/index.php?rest_route=',
'pluginInstallHash' => PluginInstaller::rest_get_plugin_install_hash(),
)
) . ';',
'before'
);

wp_enqueue_script( 'nfd-installer-data-attr-listener' );
}
}
}
17 changes: 17 additions & 0 deletions includes/WPAdmin/WPAdmin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace NewfoldLabs\WP\Module\Installer\WPAdmin;

use NewfoldLabs\WP\Module\Installer\WPAdmin\Listeners\DataAttrListener;

/**
* Manages all the wp-admin related functionalities for the module.
*/
class WPAdmin {
/**
* Constructor for the WPAdmin class.
*/
public function __construct() {
new DataAttrListener();
}
}
Loading

0 comments on commit e932c7d

Please sign in to comment.