forked from codepress/admin-columns-hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
acp-editing-saved.php
106 lines (85 loc) · 3.15 KB
/
acp-editing-saved.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* The acp/editing/saved action fires after the value of a column is stored when using Inline Edit.
*/
/**
* Fires after a inline-edit saved a value
*
* @param AC\Column $column
* @param int $id
* @param string $value
*/
function acp_editing_saved_usage( AC\Column $column, $id, $value ) {
// Place your code here
}
add_action( 'acp/editing/saved', 'acp_editing_saved_usage', 10, 3 );
/**
* In this example we will save the submitted value to second custom field.
*
* @param AC\Column $column
* @param int $id
* @param string $value
*/
function acp_editing_save_value_to_another_custom_field( AC\Column $column, $id, $value ) {
if ( $column instanceof AC\Column\CustomField && 'my_custom_field' === $column->get_meta_key() && 'post' === $column->get_meta_type() ) {
// Modify the value if necessairy
// $value = $value * 2;
update_post_meta( $id, 'my_second_custom_field', $value );
}
}
add_action( 'acp/editing/saved', 'acp_editing_save_value_to_another_custom_field', 10, 3 );
/**
* By default, the post modified date is not always updated when using inline or bulk edit with Admin Column Pro.
* For example, when updating meta data the `wp_update_post` is not called, which is the call that set then post's modified date.
* In this example we will trigger this call manually.
*
* @param AC\Column $column
* @param int $id
*/
function acp_editing_update_post_modified_date( AC\Column $column, $id ) {
$meta_type = $column->get_list_screen()->get_meta_type();
// Update the `modified_date` after making any changes using inline or bulk editing
if ( AC\MetaType::POST === $meta_type ) {
wp_update_post( [ 'ID' => $id ] );
}
// Update the `modified_date` after making changes to a specific custom field
if ( AC\MetaType::POST === $meta_type && $column instanceof AC\Column\Meta && 'my_custom_field' === $column->get_meta_key() ) {
wp_update_post( [ 'ID' => $id ] );
}
}
add_action( 'acp/editing/saved', 'acp_editing_update_post_modified_date', 10, 2 );
/**
* Update a WooCommerce product price based on a custom field
*
* @param AC\Column $column
* @param int $id
* @param string $value
*/
function acp_editing_saved_update_wc_price_on_base_price( AC\Column $column, $id, $value ) {
if ( $column instanceof AC\Column\CustomField && 'product' === $column->get_post_type() && 'base_price' === $column->get_meta_key() ) {
$price = (float) $value;
// Modify price if neccessairy
$price = $price * 1.2;
$product = wc_get_product( $id );
$product->set_regular_price( $price );
$product->save();
}
}
add_action( 'acp/editing/saved', 'acp_editing_saved_update_wc_price_on_base_price', 10, 3 );
/**
* Update a taxonomy term after using inline or bulk editing
*
* @param AC\Column $column
* @param int $id
* @param string $value
*/
function acp_editing_saved_update_taxonomy_term( AC\Column $column, $id, $value ) {
if ( 'term' === $column->get_meta_type() ) {
// Modify term arguments
$args = [
'description' => 'My new value: ' . $value,
];
wp_update_term( $id, $column->get_taxonomy(), $args );
}
}
add_action( 'acp/editing/saved', 'acp_editing_saved_update_taxonomy_term', 10, 3 );