Product templates validation
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
<?php
|
||||
/**
|
||||
* Plugin Name: Wiaas restful API
|
||||
* Plugin Name: Wiaas restful API
|
||||
* Plugin URI: http://www.saburly.com/
|
||||
* Description:A custom wordpress REST extension for wiass
|
||||
* Description:A custom wordpress REST extension for wiass
|
||||
* Version: 0.1
|
||||
* Author: Sabury
|
||||
* Author URI: http://www.saburly.com/
|
||||
@@ -11,25 +11,28 @@
|
||||
* @package Wiass
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
if (!defined('ABSPATH')) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
define('WP_DEBUG', true);
|
||||
|
||||
/**
|
||||
* Fetch user by id
|
||||
*
|
||||
* @param array $data Options for the function.
|
||||
* @return string|null User object from DB or null if user there is no uset with that id.
|
||||
*/
|
||||
function fetch_user_by_id( $data ) {
|
||||
$user = get_user_by( 'id', $data['id'] );
|
||||
|
||||
if ( empty( $user ) ) {
|
||||
function fetch_user_by_id($data)
|
||||
{
|
||||
$user = get_user_by('id', $data['id']);
|
||||
|
||||
$user = wp_get_current_user();
|
||||
}
|
||||
|
||||
return $user;
|
||||
if (empty($user)) {
|
||||
|
||||
return wp_get_current_user();
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -37,8 +40,9 @@ function fetch_user_by_id( $data ) {
|
||||
*
|
||||
* @return string|null User object from DB or null if user there is no uset with that id.
|
||||
*/
|
||||
function fetch_current_user( $data ) {
|
||||
if ( empty( wp_get_current_user() ) ) {
|
||||
function fetch_current_user($data)
|
||||
{
|
||||
if (empty(wp_get_current_user())) {
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -46,14 +50,16 @@ function fetch_current_user( $data ) {
|
||||
return wp_get_current_user();
|
||||
}
|
||||
|
||||
function check_permission($request) {
|
||||
return current_user_can( 'do_the_broking' );
|
||||
function check_permission($request)
|
||||
{
|
||||
return current_user_can('do_the_broking');
|
||||
}
|
||||
|
||||
|
||||
add_action('rest_api_init', function () {
|
||||
register_rest_route('wiaas/v1', '/user/(?P<id>\d+)', array(
|
||||
'methods' => 'GET',
|
||||
'permission_callback' => 'check_permission',
|
||||
'permission_callback' => 'check_permission',
|
||||
'callback' => 'fetch_user_by_id',
|
||||
));
|
||||
});
|
||||
@@ -61,7 +67,90 @@ add_action('rest_api_init', function () {
|
||||
add_action('rest_api_init', function () {
|
||||
register_rest_route('wiaas/v1', '/user', array(
|
||||
'methods' => 'GET',
|
||||
'permission_callback' => 'check_permission',
|
||||
'permission_callback' => 'check_permission',
|
||||
'callback' => 'fetch_user_by_id',
|
||||
));
|
||||
});
|
||||
|
||||
|
||||
add_action('added_post_meta', 'check_template_on_product_save', 1, 3);
|
||||
add_action('updated_post_meta', 'check_template_on_product_save', 1, 3);
|
||||
|
||||
function check_template_on_product_save($meta_id, $post_id, $meta_key)
|
||||
{
|
||||
|
||||
if ($meta_key == '_edit_lock' && get_post_type($post_id) == 'product') {
|
||||
|
||||
$product = wc_get_product($post_id);
|
||||
|
||||
if ($product->is_type('grouped')) {
|
||||
$nested_products = $product->get_children();
|
||||
$template_categories = [];
|
||||
$template_product_id = 0;
|
||||
|
||||
foreach ($nested_products as $nested_product_id) {
|
||||
|
||||
|
||||
$categories = wp_get_post_terms($nested_product_id, 'product_cat', array('fields' => 'names'));
|
||||
|
||||
if ((in_array("Templates", $categories))) {
|
||||
error_log("There are templates in array");
|
||||
$template_product_id = $nested_product_id;
|
||||
foreach ($categories as $category) {
|
||||
error_log($category);
|
||||
if ($category !== "Templates") {
|
||||
error_log($category);
|
||||
$template_categories[$category] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($template_categories)) {
|
||||
|
||||
error_log("Template product categories");
|
||||
|
||||
|
||||
foreach ($nested_products as $nested_product_id) {
|
||||
error_log("PRODUCT ID ==");
|
||||
error_log($nested_product_id);
|
||||
$categories_new = wp_get_post_terms($nested_product_id, 'product_cat', array('fields' => 'names'));
|
||||
|
||||
if ((in_array($categories_new[0], $template_categories))) {
|
||||
error_log("Found template category in package");
|
||||
error_log($categories_new[0]);
|
||||
if ($template_product_id !== $nested_product_id) {
|
||||
$template_categories[$categories_new[0]]++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
foreach ($template_categories as $index) {
|
||||
error_log("INDEX ==");
|
||||
error_log($index);
|
||||
if ($index === 0) {
|
||||
set_transient('sample_admin_notice__error', true, 5);
|
||||
add_action('admin_notices', 'sample_admin_notice__error');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
unset($template_categories);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function sample_admin_notice__error()
|
||||
{
|
||||
$class = 'notice notice-error';
|
||||
$message = __('This package is missing product categories that are required by template .', 'sample-text-domain');
|
||||
|
||||
printf('<div class="%1$s"><p>%2$s</p></div>', esc_attr($class), esc_html($message));
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user