Skip to content

Commit

Permalink
#15 Move the logic of fetching meta-data to MetaFetcher PHP-class
Browse files Browse the repository at this point in the history
  • Loading branch information
versusbassz committed Aug 8, 2021
1 parent 42e7534 commit 58a3b3f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 38 deletions.
1 change: 1 addition & 0 deletions entity-viewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function vsm_display_admin_notice($message) {

function vsm_init_plugin() {
require_once __DIR__ . '/src/Fetcher/EntityFetcher.php';
require_once __DIR__ . '/src/Fetcher/MetaFetcher.php';
require_once __DIR__ . '/inc/logic.php';
require_once __DIR__ . '/inc/init.php';
}
40 changes: 4 additions & 36 deletions inc/logic.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use WP_Error;
use VsEntityViewer\Fetcher\EntityFetcher;
use VsEntityViewer\Fetcher\MetaFetcher;

defined('ABSPATH') || exit;

Expand Down Expand Up @@ -71,36 +72,19 @@ function get_metabox_payload(string $entity_name, int $item_id): array
{
// TODO smth with WP_Error ???
$entity_data = EntityFetcher::getData($entity_name, $item_id);
$fields_data = get_fields_data($entity_name, $item_id);
$meta_data = MetaFetcher::getData($entity_name, $item_id);

return [
'entity' => [
'fields' => $entity_data['fields'],
],
'meta' => [
'fields' => $fields_data['fields'],
'has_serialized_values' => $fields_data['has_serialized_values'], // TODO not used in JS for now
'fields' => $meta_data['fields'],
'has_serialized_values' => $meta_data['has_serialized_values'], // not used in JS for now
],
];
}

function get_fields_data(string $entity_name, int $item_id): array
{
$meta_id_key = get_meta_id_column_for_entity($entity_name);
$meta_raw = get_meta_from_db($entity_name, $item_id);
$has_serialized_values = false;

$fields = array_map(
construct_meta_data_mapper($meta_id_key, $has_serialized_values ),
$meta_raw
);

return [
'fields' => $fields,
'has_serialized_values' => $has_serialized_values,
];
}

/**
* Returns a name of "entity"-id column in main (not meta) table
* E.g., post -> post_id, user -> ID.
Expand Down Expand Up @@ -285,22 +269,6 @@ function construct_meta_data_mapper(string $meta_id_key, bool & $has_serialized_
};
}

function get_meta_from_db(string $entity_name, int $item_id) {
global $wpdb;

$table = $entity_name . 'meta';

return $wpdb->get_results(
$wpdb->prepare("
SELECT *
FROM {$wpdb->$table}
WHERE {$entity_name}_id = %d
ORDER BY meta_key ASC
", $item_id),
ARRAY_A
);
}

/**
* @deprecated maybe add styles for the metabox here?
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Fetcher/EntityFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public static function fetchDataFromDB(string $entity_name, int $item_id)
ARRAY_A
);

if (is_null($result)) {
return new WP_Error('empty_entity_db_response');
if (! is_array($result)) {
return new WP_Error('incorrect_entity_db_response');
}

return $result;
Expand Down
61 changes: 61 additions & 0 deletions src/Fetcher/MetaFetcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
declare(strict_types = 1);

namespace VsEntityViewer\Fetcher;

use WP_Error;
use function VsEntityViewer\construct_meta_data_mapper;
use function VsEntityViewer\get_meta_id_column_for_entity;

class MetaFetcher
{
/**
* @return array|WP_Error
*/
public static function getData(string $entity_name, int $item_id)
{
$has_serialized_values = false;

$meta_id_key = get_meta_id_column_for_entity($entity_name);
$meta_raw = self::fetchDataFromDB($entity_name, $item_id);

if (is_wp_error($meta_raw)) {
return $meta_raw;
}

$fields = array_map(
construct_meta_data_mapper($meta_id_key, $has_serialized_values),
$meta_raw
);

return [
'fields' => $fields,
'has_serialized_values' => $has_serialized_values,
];
}

/**
* @return array|WP_Error
*/
public static function fetchDataFromDB(string $entity_name, int $item_id) {
global $wpdb;

$table = $entity_name . 'meta';

$result = $wpdb->get_results(
$wpdb->prepare("
SELECT *
FROM {$wpdb->$table}
WHERE {$entity_name}_id = %d
ORDER BY meta_key ASC
", $item_id),
ARRAY_A
);

if (! is_array($result)) {
return new WP_Error('incorrect_meta_db_response');
}

return $result;
}
}

0 comments on commit 58a3b3f

Please sign in to comment.