Skip to content
This repository has been archived by the owner on Jul 26, 2024. It is now read-only.

Commit

Permalink
Merge branch 'enhancement/add-awards-block' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
admturner committed Aug 31, 2022
2 parents b32fa2e + 4dff00a commit 1641b53
Show file tree
Hide file tree
Showing 20 changed files with 682 additions and 48 deletions.
32 changes: 32 additions & 0 deletions hrswp-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,35 @@ function uninstall(): void {
delete_option( 'hrswp_plugins_protected_ids' );
}
register_uninstall_hook( __FILE__, __NAMESPACE__ . '\uninstall' );

/**
* Verifies plugin dependencies.
*
* @since 3.2.0
*
* @return bool True if dependencies are met, false if not.
*/
function verify_plugin_dependencies(): bool {
return in_array(
'hrswp-plugin-sqlsrv-db/hrswp-sqlsrv-db.php',
apply_filters( 'active_plugins', get_option( 'active_plugins' ) ),
true
);
}

add_action(
'plugins_loaded',
function(): void {
if ( ! verify_plugin_dependencies() ) {
add_action(
'admin_notices',
function(): void {
printf(
'<div class="error"><p>%s</p></div>',
esc_html__( 'The HRSWP Blocks plugin requires the HRSWP Sqlsrv DB plugin to function properly. Please install before continuing.', 'hrswp-blocks' )
);
}
);
}
}
);
29 changes: 27 additions & 2 deletions inc/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function(): void {
)
);

// Register a public route to get job classification data.
// Register a public route to get job classification data.
register_rest_route(
$namespace,
'/jobclassification/table/(?P<table>[a-z0-9_\-]+)',
Expand All @@ -60,7 +60,7 @@ function(): void {
)
);

// Register a public route to get job classification data.
// Register a public route to get job classification data.
register_rest_route(
$namespace,
'/salarydata/table/(?P<table>[a-z0-9_\-]+)',
Expand All @@ -79,5 +79,30 @@ function(): void {
},
)
);

// Register a route to import awards data as attachement posts.
register_rest_route(
$namespace,
'/awardsdata/table/(?P<table>[a-z0-9_\-]+)/post/(?P<post>[a-z0-9_\-]+)',
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => 'HRSWP\Blocks\Query\import_awards_data',
'args' => array(
'table' => array(
'sanitize_callback' => function( string $param ): string {
return sanitize_key( $param );
},
),
'post' => array(
'sanitize_callback' => function( string $param ): string {
return absint( sanitize_key( $param ) );
},
),
),
'permission_callback' => function (): bool {
return current_user_can( 'edit_posts' );
},
)
);
}
);
26 changes: 26 additions & 0 deletions inc/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

// Include files required for dynamic block registration.
require plugin_dir_path( dirname( __FILE__ ) ) . 'build/blocks/posts-list/index.php';
require plugin_dir_path( dirname( __FILE__ ) ) . 'build/blocks/list-awards/index.php';

/**
* Registers HRSWP blocks.
Expand Down Expand Up @@ -44,3 +45,28 @@ function(): void {
}
}
);

/**
* Adds a custom block category for the plugin blocks.
*
* Callback function for the `block_categories` WP filter hook.
*
* @since 3.2.0
*
* @param array $default_categories Array of default block categories.
* @return array Array of block categories.
*/
add_filter(
'block_categories_all',
function( array $default_categories ): array {
$plugin_categories = array(
array(
'slug' => 'hrswp-blocks-external',
'title' => __( 'HRS External Content', 'hrswp-blocks' ),
),
);
return wp_parse_args( $plugin_categories, $default_categories );
},
10,
1
);
41 changes: 8 additions & 33 deletions inc/classes/class-sideload-image.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,17 @@ public function __construct( $props ) {
$args = wp_parse_args( $props, $defaults );

if ( $args['skip_if_exists'] ) {
$existing_attachment = $this->get_attachment_by_title( $args['title'] );
$existing_attachment = get_page_by_title( $args['title'], OBJECT, 'attachment' );

// If the attachment already exists, populate the class property and exit early.
if ( false !== $existing_attachment ) {
if ( $existing_attachment ) {
$this->attachment = array(
'id' => $existing_attachment->ID,
'title' => $existing_attachment->post_title,
'parent_id' => $existing_attachment->post_parent,
);

return;
return $this->attachment;
}
}

Expand All @@ -122,7 +122,9 @@ public function __construct( $props ) {
// Sideload the file and create the attachment, then populate the default alt text and award group meta.
$this->sideload_file( $args['image_contents'], $args['page_id'] );
update_post_meta( $this->attachment['id'], '_wp_attachment_image_alt', $this->fileinfo['description'] );
update_post_meta( $this->attachment['id'], '_' . Setup\Setup::$slug . '_award_group', $this->fileinfo['group_year'] );
update_post_meta( $this->attachment['id'], '_hrswp_sqlsrv_db_award_group', $this->fileinfo['group_year'] );

return $this->attachment;
}

/**
Expand All @@ -134,10 +136,10 @@ public function __construct( $props ) {
*/
private function includes() {
// The WordPress Filesystem API: Top-level functionality.
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once ABSPATH . 'wp-admin/includes/file.php';

// All of the WordPress administration image manipulation functions.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once ABSPATH . 'wp-admin/includes/image.php';
}

/**
Expand Down Expand Up @@ -225,31 +227,4 @@ private function sideload_file( $image_contents, $page_id = 0 ) {

return $attachment_id;
}

/**
* Retrieves an attachment by its post title value.
*
* If multiple posts match the query only the first is returned.
*
* @since 0.4.0
*
* @param string $title The attachment post title.
* @return WP_Post|false The attachment post object if it exists, false if not.
*/
private function get_attachment_by_title( $title ) {
// Set up the query args.
$args = array(
'name' => sanitize_title( $title ),
'post_type' => 'attachment',
'post_status' => 'inherit',
);

$attachment = new \WP_Query( $args );

if ( ! $attachment->have_posts() ) {
return false;
}

return $attachment->posts[0];
}
}
112 changes: 99 additions & 13 deletions inc/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

namespace HRSWP\Blocks\Query;

use HRSWP\SQLSRV\Sqlsrv_Query;
use HRSWP\Blocks;
use HRSWP\Blocks\Sideload_Image;
use HRSWP\SQLSRV\Sqlsrv_DB;
use HRSWP\SQLSRV\Sqlsrv_Query;

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
Expand All @@ -25,16 +27,12 @@
*
* @since 3.2.0
*
* @return array|null JSON feed of returned objects, null if no tables are found.
* @return \WP_REST_Response||\WP_Error||null JSON feed of returned objects, null if no tables are found.
*/
function get_tables_list(): ?array {
function get_tables_list(): ?object {
// Check to make sure required plugin is active.
if ( ! in_array(
'hrswp-plugin-sqlsrv-db/hrswp-sqlsrv-db.php',
apply_filters( 'active_plugins', get_option( 'active_plugins' ) ),
true
) ) {
return new \WP_Error( 'missing-plugin', __( 'The HRSWP Sqlsrv DB must be activated.', 'hrswp-blocks' ) );
if ( ! Blocks\verify_plugin_dependencies() ) {
return new \WP_Error( 'missing-plugin', __( 'The HRSWP Sqlsrv DB plugin must be activated.', 'hrswp-blocks' ) );
}

// Initialize the HRSWP Sqlsrv DB connector.
Expand Down Expand Up @@ -69,9 +67,14 @@ function get_tables_list(): ?array {
* @since 3.2.0
*
* @param \WP_REST_Request $request Data from the request passed to the callback.
* @return array|null JSON feed of returned data, null if no data is found.
* @return \WP_REST_Response||\WP_Error JSON feed of returned data, WP Error if missing table.
*/
function get_job_classification_data( \WP_REST_Request $request ): ?array {
function get_job_classification_data( \WP_REST_Request $request ): object {
// Check to make sure required plugin is active.
if ( ! Blocks\verify_plugin_dependencies() ) {
return new \WP_Error( 'missing-plugin', __( 'The HRSWP Sqlsrv DB plugin must be activated.', 'hrswp-blocks' ) );
}

if ( ! $request || '' === $request['table'] ) {
return new \WP_Error( 'missing-table', __( 'No table specified for query.', 'hrswp-blocks' ) );
}
Expand Down Expand Up @@ -111,9 +114,14 @@ function get_job_classification_data( \WP_REST_Request $request ): ?array {
* @since 3.2.0
*
* @param \WP_REST_Request $request Data from the request passed to the callback.
* @return array|null JSON feed of returned data, null if no data is found.
* @return \WP_REST_Response||\WP_Error JSON feed of returned data, WP Error if missing table.
*/
function get_salary_data( \WP_REST_Request $request ): ?array {
function get_salary_data( \WP_REST_Request $request ): object {
// Check to make sure required plugin is active.
if ( ! Blocks\verify_plugin_dependencies() ) {
return new \WP_Error( 'missing-plugin', __( 'The HRSWP Sqlsrv DB plugin must be activated.', 'hrswp-blocks' ) );
}

if ( ! $request || '' === $request['table'] ) {
return new \WP_Error( 'missing-table', __( 'No table specified for query.', 'hrswp-blocks' ) );
}
Expand All @@ -137,3 +145,81 @@ function get_salary_data( \WP_REST_Request $request ): ?array {

return new \WP_REST_Response( array( 'request' => $request['table'] ), 304 );
}

/**
* Imports data from a given Awards data table.
*
* @since 3.2.0
*
* @param \WP_REST_Request $request Data from the request passed to the callback.
* @return \WP_REST_Response||\WP_Error JSON feed of returned data, WP Error if missing table.
*/
function import_awards_data( \WP_REST_Request $request ): object {
// Check to make sure required plugin is active.
if ( ! Blocks\verify_plugin_dependencies() ) {
return new \WP_Error( 'missing-plugin', __( 'The HRSWP Sqlsrv DB plugin must be activated.', 'hrswp-blocks' ) );
}

if ( ! $request || '' === $request['table'] ) {
return new \WP_Error( 'missing-table', __( 'No table specified for query.', 'hrswp-blocks' ) );
}

// Get the awards records from the external database.
if ( 'undefined' !== $request['table'] ) {
$args = array(
'dataset' => array(
array(
'table' => 'awards',
'fields' => array(
'BinaryFile',
'GroupDescription',
'GroupName',
'GroupYear',
),
),
),
'orderby' => 'GroupYear',
);

$result = new Sqlsrv_Query\Sqlsrv_Query( $args );
$awards = $result->records ?? null;
}

if ( ! $awards ) {
return new \WP_REST_Response( array( 'request' => $request['table'] ), 304 );
}

// Import the retrieved awards into the media library.
foreach ( $awards as $award ) {
$image_props = array(
// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
'title' => $award->GroupName,
'image_contents' => $award->BinaryFile,
'group_year' => $award->GroupYear,
'page_id' => (int) $request['post'],
'description' => $award->GroupDescription,
'skip_if_exists' => true,
// phpcs:enable
);

$image = new Sideload_Image\Sideload_Image( $image_props );

if ( is_wp_error( $image ) ) {
return new \WP_REST_Response( array( 'request' => $request['table'] ), 304 );
}
}

// Get the imported and existing awards.
$attachments = get_children(
array(
'post_parent' => (int) $request['post'],
'post_type' => 'attachment',
'posts_per_page' => -1,
'orderby' => 'meta_value_num',
'meta_key' => '_hrswp_sqlsrv_db_award_group',
'order' => 'ASC',
)
);

return new \WP_REST_Response( $attachments, 200 );
}
1 change: 1 addition & 0 deletions src/blocks/editor.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
@import "./buttons/editor.css";
@import "./callout/editor.css";
@import "./filter/editor.css";
@import "./list-awards/editor.css";
@import "./notification/editor.css";
@import "./posts-list/editor.css";
@import "./sidebar/editor.css";
2 changes: 2 additions & 0 deletions src/blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import * as filterSection from './filter-section';
import * as callout from './callout';
import * as notification from './notification';
import * as sidebar from './sidebar';
import * as listAwards from './list-awards';

const hrsBlocks = [
accordionHeading,
Expand All @@ -31,6 +32,7 @@ const hrsBlocks = [
callout,
notification,
sidebar,
listAwards,
];

const hiddenBlocks = [
Expand Down
Loading

0 comments on commit 1641b53

Please sign in to comment.