Skip to content

Commit

Permalink
Initial commits
Browse files Browse the repository at this point in the history
  • Loading branch information
jdevalk committed Feb 29, 2024
1 parent e43ad1e commit e103545
Show file tree
Hide file tree
Showing 16 changed files with 1,547 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .distignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/.wordpress-org
/.git
/.github
/tests
/coverage

.distignore
.gitignore
.gitattributes
.phpcs.xml.dist
phpunit.xml.dist
README.md
composer.json
composer.lock
33 changes: 33 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#
# Exclude these files from release archives.
# This will also make them unavailable when using Composer with `--prefer-dist`.
# If you develop for Clicky using Composer, use `--prefer-source`.
# https://www.reddit.com/r/PHP/comments/2jzp6k/i_dont_need_your_tests_in_my_production
# https://blog.madewithlove.be/post/gitattributes/
#
.distignore export-ignore
/.editorconfig export-ignore
/.eslintrc export-ignore
/.github export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.phpcs.xml export-ignore
/phpcs.xml.dist export-ignore
/.cache export-ignore
composer.json export-ignore
composer.lock export-ignore
phpunit.xml.dist export-ignore
/README.md export-ignore
/tests export-ignore

#
# Auto detect text files and perform LF normalization
# http://davidlaing.com/2012/09/19/customise-your-gitattributes-to-become-a-git-ninja/
#
* text=auto

#
# The above will handle all files NOT found below
#
*.md text
*.php text
68 changes: 68 additions & 0 deletions .github/workflows/cs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: CS

on:
# Run on all relevant pushes (except to main) and on all relevant pull requests.
push:
paths:
- '**.php'
- 'composer.json'
- 'composer.lock'
- '.phpcs.xml.dist'
- 'phpcs.xml.dist'
- '.github/workflows/cs.yml'
pull_request:
paths:
- '**.php'
- 'composer.json'
- 'composer.lock'
- '.phpcs.xml.dist'
- 'phpcs.xml.dist'
- '.github/workflows/cs.yml'
# Allow manually triggering the workflow.
workflow_dispatch:

# Cancels all previous workflow runs for the same branch that have not yet completed.
concurrency:
# The concurrency group contains the workflow name and the branch name.
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
checkcs:
name: 'Check code style'
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
coverage: none
tools: cs2pr

# Validate the composer.json file.
# @link https://getcomposer.org/doc/03-cli.md#validate
- name: Validate Composer installation
run: composer validate --no-check-all

# Install dependencies and handle caching in one go.
# @link https://github.com/marketplace/actions/install-composer-dependencies
- name: Install Composer dependencies
uses: ramsey/composer-install@v2
with:
# Bust the cache at least once a month - output format: YYYY-MM.
custom-cache-suffix: $(date -u "+%Y-%m")

# Check the codestyle of the files.
# The results of the CS check will be shown inline in the PR via the CS2PR tool.
# @link https://github.com/staabm/annotate-pull-request-from-checkstyle/
- name: Check PHP code style
id: phpcs
run: composer check-cs -- --no-cache --report-full --report-checkstyle=./phpcs-report.xml

- name: Show PHPCS results in PR
if: ${{ always() && steps.phpcs.outcome == 'failure' }}
run: cs2pr ./phpcs-report.xml
69 changes: 69 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
name: Lint

on:
# Run on pushes to select branches and on all pull requests.
push:
branches:
- main
- develop
- 'release/[0-9]+.[0-9]+*'
- 'hotfix/[0-9]+.[0-9]+*'
pull_request:
# Allow manually triggering the workflow.
workflow_dispatch:

# Cancels all previous workflow runs for the same branch that have not yet completed.
concurrency:
# The concurrency group contains the workflow name and the branch name.
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
lint:
runs-on: ubuntu-latest

strategy:
matrix:
# Lint against the highest/lowest supported versions of each PHP major.
# And also do a run against "nightly" (the current dev version of PHP).
php_version: ['7.4', '8.0', '8.1', '8.2']

name: "Lint: PHP ${{ matrix.php_version }}"

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Install PHP for the composer install
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
coverage: none

# The lint stage doesn't use code style, so no need for WPCS or phpcompatibility.
- name: 'Composer: adjust dependencies - remove PHPCompatibility'
run: composer remove --no-update --dev phpcompatibility/phpcompatibility-wp --no-scripts --no-interaction
- name: 'Composer: adjust dependencies - remove WPCS'
run: composer remove --no-update --dev wp-coding-standards/wpcs --no-scripts --no-interaction

# Install dependencies and handle caching in one go.
# @link https://github.com/marketplace/actions/install-composer-dependencies
- name: Install Composer dependencies
uses: ramsey/composer-install@v2
with:
# Bust the cache at least once a month - output format: YYYY-MM-DD.
custom-cache-suffix: $(date -u -d "-0 month -$(($(date +%d)-1)) days" "+%F")

- name: Install PHP for the actual test
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php_version }}
ini-values: zend.assertions=1, error_reporting=-1, display_errors=On
coverage: none
tools: cs2pr

- name: Lint against parse errors
run: composer lint -- --checkstyle | cs2pr

- name: Lint blueprint file
run: composer lint-blueprint
16 changes: 16 additions & 0 deletions .github/workflows/playground.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Playground Comment

on:
pull_request:

jobs:
test:
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- uses: mshick/add-pr-comment@v2
with:
message: |
**Test on Playground**
[Test this pull request on the Playground](https://playground.wordpress.net/#{"landingPage":"/wp-admin/tools.php?page=aaa-option-optimizer","features":{"networking":true},"steps":[{"step":"login","username":"admin","password":"password"},{"step":"installPlugin","pluginZipFile":{"resource":"url","url":"https://bypass-cors.altha.workers.dev/${{ github.server_url }}/${{ github.repository }}/archive/${{ github.sha }}.zip"},"options":{"activate":true}}]})
34 changes: 34 additions & 0 deletions .github/workflows/security.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Security

on:
# Run on all pushes and on all pull requests.
push:
pull_request:
# Also run this workflow every Monday at 6:00.
schedule:
- cron: '0 6 * * 1'
# Allow manually triggering the workflow.
workflow_dispatch:

# Cancels all previous workflow runs for the same branch that have not yet completed.
concurrency:
# The concurrency group contains the workflow name and the branch name.
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
security:
name: 'Security check'
runs-on: ubuntu-latest

# Don't run the cronjob in this workflow on forks.
if: github.event_name != 'schedule' || (github.event_name == 'schedule' && github.repository_owner == 'Emilia-Capital')

steps:
- name: Checkout code
uses: actions/checkout@v4

# This action checks the `composer.lock` file against known security vulnerabilities in the dependencies.
# https://github.com/marketplace/actions/the-php-security-checker
- name: Run Security Check
uses: symfonycorp/security-checker-action@v4
20 changes: 20 additions & 0 deletions .github/workflows/wp-version-checker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: "WordPress version checker"
on:
push:
branches:
- develop
- main
schedule:
- cron: '0 0 * * *'

permissions:
issues: write

jobs:
wordpress-version-checker:
runs-on: ubuntu-latest
steps:
- name: WordPress version checker
uses: skaut/[email protected]
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
55 changes: 55 additions & 0 deletions aaa-option-optimizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Plugin that tracks autoloaded options usage and allows the user to optimize them.
*
* @package Emilia\OptionOptimizer
*
* Plugin Name: AAA Option Optimizer
* Plugin URI: https://joost.blog/plugins/aaa-option-optimizer/
* Description: Tracks autoloaded options usage and allows the user to optimize them.
* Version: 1.0.0
* Author: Joost de Valk
* Author URI: https://joost.blog/
* Text Domain: aaa-option-optimizer
*/

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

define( 'AAA_OPTION_OPTIMIZER_FILE', __FILE__ );

register_activation_hook( __FILE__, 'aaa_option_optimizer_activation' );

/**
* Activation hooked function to store start stats.
*
* @return void
*/
function aaa_option_optimizer_activation() {
global $wpdb;
//phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- one time query, no caching needed.
$autoload_size = $wpdb->get_var( "SELECT SUM(LENGTH(option_value)) as autoload_size FROM {$wpdb->options} WHERE autoload='yes'" );
update_option(
'option_optimizer',
[
'starting_point_kb' => $autoload_size / 1024,
'starting_point_date' => current_time( 'mysql' ),
'used_options' => [],
],
true
);
}

/**
* Initializes the plugin.
*
* @return void
*/
function aaa_option_optimizer_init() {
require_once plugin_dir_path( __FILE__ ) . 'src/class-plugin.php';
$optimizer = new Emilia\OptionOptimizer\Plugin();
$optimizer->register_hooks();
}

aaa_option_optimizer_init();
33 changes: 33 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "emilia/aaa-option-optimizer",
"description": "Plugin that tracks autoloaded options usage and allows the user to optimize them.",
"type": "wordpress-plugin",
"license": "GPL-3.0-or-later",
"authors": [
{
"name": "Joost de Valk",
"email": "[email protected]"
}
],
"require-dev": {
"php-parallel-lint/php-parallel-lint": "^1.3",
"phpcompatibility/phpcompatibility-wp": "^2.1",
"wp-coding-standards/wpcs": "^3.0"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"scripts": {
"check-cs": [
"@php ./vendor/bin/phpcs"
],
"fix-cs": [
"@php ./vendor/bin/phpcbf"
],
"lint": [
"@php ./vendor/php-parallel-lint/php-parallel-lint/parallel-lint . -e php --show-deprecated --exclude vendor --exclude node_modules --exclude .git"
]
}
}
Loading

0 comments on commit e103545

Please sign in to comment.