Product templates validation

This commit is contained in:
Nedim Uka
2018-07-17 12:24:06 +02:00
parent ae20e0eda3
commit 476badd59e
5808 changed files with 4085 additions and 1533451 deletions

View File

@@ -14,8 +14,8 @@
<file leaf-file-name="wiaas-extended-restful-api.php" pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/wiaas-extended-restful-api.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="765">
<caret line="61" column="42" selection-start-line="61" selection-start-column="42" selection-end-line="61" selection-end-column="42" />
<state relative-caret-position="997">
<caret line="90" lean-forward="true" selection-start-line="90" selection-end-line="90" />
</state>
</provider>
</entry>
@@ -49,6 +49,7 @@
<foldersAlwaysOnTop value="true" />
</navigator>
<panes>
<pane id="Scope" />
<pane id="ProjectPane">
<subPane>
<expand>
@@ -60,7 +61,6 @@
<select />
</subPane>
</pane>
<pane id="Scope" />
</panes>
</component>
<component name="PropertiesComponent">
@@ -92,12 +92,20 @@
<workItem from="1529404184526" duration="177000" />
<workItem from="1529411949167" duration="1604000" />
<workItem from="1529483201441" duration="25000" />
<workItem from="1529485101926" duration="3889000" />
<workItem from="1529485101926" duration="4489000" />
<workItem from="1530183029060" duration="82000" />
<workItem from="1530183735568" duration="37000" />
<workItem from="1530183784451" duration="10000" />
<workItem from="1530187625418" duration="2000" />
<workItem from="1530196590112" duration="3000" />
<workItem from="1530258026430" duration="373000" />
<workItem from="1531730113559" duration="22456000" />
<workItem from="1531815904664" duration="2871000" />
</task>
<servers />
</component>
<component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="5695000" />
<option name="totallyTimeSpent" value="32129000" />
</component>
<component name="ToolWindowManager">
<frame x="0" y="1048" width="1920" height="1082" extended-state="6" />
@@ -133,22 +141,8 @@
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/wiaas-extended-restful-api.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="435">
<caret line="39" column="3" selection-start-line="39" selection-start-column="3" selection-end-line="39" selection-end-column="3" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/wiaas-extended-restful-api.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="435">
<caret line="39" column="3" lean-forward="true" selection-start-line="39" selection-start-column="3" selection-end-line="39" selection-end-column="3" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/wiaas-extended-restful-api.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="360">
<caret line="24" column="28" lean-forward="true" selection-start-line="24" selection-start-column="28" selection-end-line="24" selection-end-column="28" />
<state relative-caret-position="1665">
<caret line="121" column="46" selection-start-line="121" selection-start-column="46" selection-end-line="121" selection-end-column="46" />
</state>
</provider>
</entry>
@@ -159,5 +153,12 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/wiaas-extended-restful-api.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="997">
<caret line="90" lean-forward="true" selection-start-line="90" selection-end-line="90" />
</state>
</provider>
</entry>
</component>
</project>

View File

@@ -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));
}