Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mirekdlugosz committed Dec 11, 2022
0 parents commit 5f970e1
Show file tree
Hide file tree
Showing 12 changed files with 28,490 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
tab_width = 4
indent_style = tab
insert_final_newline = true
trim_trailing_whitespace = true

[*\.{json,xml,xml\.dist,eslintrc}]
indent_style = space
indent_size = 2

[*\.{yml,yaml}]
indent_style = space
indent_size = 4
55 changes: 55 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: CD

on:
push:
tags:
- 'v[0-9]+\.[0-9]+\.[0-9]+'

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
create-archive:
name: "Create archive with a plugin"

runs-on: ubuntu-latest

permissions:
contents: write

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

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
ini-values: "memory_limit=1G"
coverage: none

- name: Setup node.js
uses: actions/setup-node@v3
with:
node-version: '16'

- name: Install newest npm
run: npm i -g npm@latest

- name: Install Composer dependencies
uses: ramsey/composer-install@v2

- name: Install npm dependencies
run: npm ci

- name: Build frontend assets
run: npm run build

- name: Create WordPress plugin archive
run: composer archive --file="ast-template-view-${{ github.ref_name }}"

- name: Create GitHub release
uses: softprops/action-gh-release@v1
with:
files: ast-template-view*.zip
55 changes: 55 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: CI

on:
# Run on pushes to select branches and on all pull requests.
push:
branches:
- main
pull_request:
workflow_dispatch:

jobs:
lint:
runs-on: ubuntu-latest

strategy:
matrix:
php_version: ["8.0", "8.1", "8.2"]
node_version: ["16"]

name: "Lint: PHP ${{ matrix.php_version }} node.js ${{ matrix.node_version }}"

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

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php_version }}
ini-values: "memory_limit=1G"
coverage: none
tools: cs2pr

- name: Setup node.js
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

- name: Install newest npm
run: npm i -g npm@latest

- name: Install Composer dependencies
uses: ramsey/composer-install@v2

- name: Install npm dependencies
run: npm ci

- name: Actually lint PHP
run: npm run lint:php

- name: Include PHP lint results in PR
run: cs2pr ./phpcs-report.xml

- name: Actually lint JS
run: npm run lint:js
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
dist
vendor
57 changes: 57 additions & 0 deletions .phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0"?>
<ruleset name="WordPress Coding Standards">
<description>Apply WordPress Coding Standards</description>

<!-- Set the memory limit to 256M.
For most standard PHP configurations, this means the memory limit will temporarily be raised.
Ref: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#specifying-phpini-settings
-->
<ini name="memory_limit" value="256M"/>

<!-- Whenever possible, cache the scan results and re-use those for unchanged files on the next scan. -->
<arg name="cache"/>

<!-- Strip the filepaths down to the relevant bit. -->
<arg name="basepath" value="./"/>

<!-- Check up to 20 files simultaneously. -->
<arg name="parallel" value="20"/>

<!-- Show sniff codes in all reports. -->
<arg value="ps"/>

<!-- Use WordPress "Extra" Coding Standards. -->
<rule ref="WordPress-Extra">
<!-- Allow array short syntax. -->
<exclude name="Generic.Arrays.DisallowShortArraySyntax" />
<!-- Allow short prefixes. -->
<exclude name="WordPress.NamingConventions.PrefixAllGlobals.ShortPrefixPassed"/>
</rule>

<!-- Use WordPress "Docs" Coding Standards. -->
<rule ref="WordPress-Docs" />

<!-- The minimum supported WordPress version. This should match what's listed in style.css. -->
<rule ref="WordPress.WP.DeprecatedFunctions">
<properties>
<property name="minimum_supported_version" value="5.6"/>
</properties>
</rule>

<!-- Use WordPress PHP Compatibility. -->
<rule ref="PHPCompatibilityWP" />

<!-- WordPress Core currently supports PHP 5.6+. -->
<config name="testVersion" value="7.4-"/>

<!-- Only sniff PHP files. -->
<arg name="extensions" value="php"/>

<file>./</file>

<!-- Don't sniff the following directories or file types. -->
<exclude-pattern type="relative">^dist/*</exclude-pattern>
<exclude-pattern type="relative">^node_modules/*</exclude-pattern>
<exclude-pattern type="relative">^vendor/*</exclude-pattern>
<exclude-pattern>*/\.*</exclude-pattern>
</ruleset>
157 changes: 157 additions & 0 deletions ast-template-view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php
/**
* Plugin Name: AST Template View On Pages Table
* Plugin URI: https://github.com/mirekdlugosz/ast-template-view/
* Description: Allow page authors to mark specific pages as templates - used in internal process
* Version: 2022.12.11
* Author: Mirek Długosz
* Author URI: https://mirekdlugosz.com/
* Text Domain: associationforsoftwaretesting
* Update URI: false
*
* @package AssociationForSoftwareTesting\TemplateView
*/

// FIXME: Update URI.

const PAGE_TEMPLATE_META_KEY = 'ast_page_is_template';
const VIEWS_QUERY_VAR = 'ast_custom_filter';


/**
* Register custom post meta key.
*/
function ast_register_post_meta() {
register_post_meta(
'page',
PAGE_TEMPLATE_META_KEY,
array(
'show_in_rest' => true,
'single' => true,
'type' => 'boolean',
)
);
}

/**
* Register page edit sidebar component.
*/
function ast_register_block_type() {
register_block_type( __DIR__ . '/dist/mark-page-as-template/' );
}


/**
* Run plugin initialization - register all plugin components.
*/
function ast_template_view_plugin_init() {
ast_register_post_meta();
ast_register_block_type();
}

add_action( 'init', 'ast_template_view_plugin_init' );


/**
* Tell WordPress about URL query variable we can handle.
*
* @param array $query_vars See WordPress docs.
*/
function ast_query_vars( $query_vars ) {
$query_vars[] = VIEWS_QUERY_VAR;
return $query_vars;
}

add_filter( 'query_vars', 'ast_query_vars' );


/**
* Add link at the top of pages table.
* This is modeled after constructor in wp-admin/includes/class-wp-posts-list-table.php
*
* @param array $views See WordPress docs.
*/
function ast_views_edit_page( $views ) {
global $wpdb;
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$post_type = $_GET['post_type'];

$ast_view_args = array(
'post_type' => $post_type,
VIEWS_QUERY_VAR => PAGE_TEMPLATE_META_KEY,
);

$url = esc_url( add_query_arg( $ast_view_args, 'edit.php' ) );

$posts_count = (int) $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT( 1 )
FROM $wpdb->posts AS posts
JOIN $wpdb->postmeta AS pmeta
ON posts.ID = pmeta.post_id
WHERE post_status = 'draft'
AND pmeta.meta_key = %s
AND pmeta.meta_value = 1",
PAGE_TEMPLATE_META_KEY
)
);

$label = sprintf(
/* translators: %s: Number of posts. */
_nx(
'Templates <span class="count">(%s)</span>',
'Templates <span class="count">(%s)</span>',
$posts_count,
'associationforsoftwaretesting'
),
number_format_i18n( $posts_count )
);

$is_current = get_query_var( VIEWS_QUERY_VAR ) === PAGE_TEMPLATE_META_KEY;

$full_link = sprintf(
'<a href="%s"%s>%s</a>',
$url,
$is_current ? ' class="current" aria-current="page"' : '',
$label
);

$views[ PAGE_TEMPLATE_META_KEY ] = $full_link;
return $views;
}

add_filter( 'views_edit-page', 'ast_views_edit_page' );


/**
* Modify WPQuery params based on URL query values.
* WordPress calls this for *all* WPQuery calls,
* so we need to be careful to not impact the performance
* of entire website.
*
* @param array $query See WordPress docs.
*/
function ast_pre_get_posts( $query ) {
// viewers shouldn't pay price of the plugin.
if ( ! is_admin() ) {
return $query;
}

global $pagenow;

if ( 'edit.php' !== $pagenow ) {
return $query;
}

if ( get_query_var( VIEWS_QUERY_VAR ) !== PAGE_TEMPLATE_META_KEY ) {
return $query;
}

$query->query_vars['post_status'] = 'draft';
$query->query_vars['meta_key'] = PAGE_TEMPLATE_META_KEY;
$query->query_vars['meta_value'] = 1;

return $query;
}

add_filter( 'pre_get_posts', 'ast_pre_get_posts' );
15 changes: 15 additions & 0 deletions blocks/mark-page-as-template/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://json.schemastore.org/block.json",
"apiVersion": 2,
"name": "associationforsoftwaretesting/mark-page-as-template",
"version": "1.0.0",
"title": "meta-block",
"category": "embed",
"icon": "generic",
"description": "Allow page authors to mark specific pages as templates - used in internal process",
"supports": {
"html": false
},
"textdomain": "associationforsoftwaretesting",
"editorScript": "file:./index.js"
}
Loading

0 comments on commit 5f970e1

Please sign in to comment.