@@ -27,45 +27,51 @@ public function add_options_page()
27
27
);
28
28
}
29
29
30
+
30
31
public function update_idm_callback ()
31
32
{
32
33
// Check if AJAX request to update allow_uri or allow_get
33
34
check_ajax_referer ('update_shorturl_idm_nonce ' , '_ajax_nonce ' );
34
35
35
- global $ wpdb ;
36
-
37
36
// Validate and sanitize input
38
37
if (!isset ($ _POST ['id ' ]) || !isset ($ _POST ['field ' ])) {
39
38
wp_send_json_error ('Invalid request ' );
40
39
return ;
41
40
}
42
41
43
42
$ id = intval ($ _POST ['id ' ]);
44
- $ allowed_fields = ['allow_uri ' , 'allow_get ' ]; // List of valid fields
43
+ $ allowed_fields = ['allow_uri ' , 'allow_get ' , ' allow_utm ' ]; // List of valid fields
45
44
$ field = sanitize_text_field (wp_unslash ($ _POST ['field ' ]));
46
45
47
46
if (!in_array ($ field , $ allowed_fields )) {
48
47
wp_send_json_error ('Invalid field ' );
49
48
return ;
50
49
}
51
50
51
+ // Convert the value to an integer 1 or 0 based on the input
52
52
$ value = isset ($ _POST ['value ' ]) && $ _POST ['value ' ] === 'true ' ? 1 : 0 ;
53
53
54
- // Update the allow_uri or allow_get field
55
- $ wpdb ->update (
56
- $ wpdb ->prefix . 'shorturl_idms ' ,
57
- array ($ field => $ value ),
58
- array ('id ' => $ id ),
59
- array ('%d ' ),
60
- array ('%d ' )
61
- );
54
+ // Check if the post with the given ID exists and is of the correct type
55
+ if (get_post_type ($ id ) !== 'shorturl_idm ' ) {
56
+ wp_send_json_error ('Invalid post ID or post type ' );
57
+ return ;
58
+ }
62
59
63
- // Return success response
64
- wp_send_json_success ();
60
+ // Update the corresponding meta field for the Custom Post Type
61
+ $ result = update_post_meta ($ id , $ field , $ value );
62
+
63
+ // Check if the update was successful and return the appropriate response
64
+ if (false === $ result ) {
65
+ wp_send_json_error ('Meta update failed ' );
66
+ } else {
67
+ wp_send_json_success (array (
68
+ 'message ' => 'Field updated successfully ' ,
69
+ 'updated_field ' => $ field ,
70
+ 'new_value ' => $ value ,
71
+ ));
72
+ }
65
73
}
66
74
67
-
68
-
69
75
// Register settings sections and fields
70
76
public function register_settings ()
71
77
{
@@ -841,28 +847,29 @@ class="button button-primary"><?php echo esc_html__('Save Changes', 'rrze-shortu
841
847
}
842
848
}
843
849
844
- public function render_statistic_section () {
850
+ public function render_statistic_section ()
851
+ {
845
852
// Determine the current sorting order and column
846
853
$ orderby = isset ($ _GET ['orderby ' ]) ? sanitize_text_field (wp_unslash ($ _GET ['orderby ' ])) : 'hostname ' ;
847
854
$ order = isset ($ _GET ['order ' ]) && in_array (sanitize_text_field (wp_unslash ($ _GET ['order ' ])), ['asc ' , 'desc ' ]) ? sanitize_text_field (wp_unslash ($ _GET ['order ' ])) : 'asc ' ;
848
-
855
+
849
856
// Get all domains to count associated links
850
857
$ args = [
851
858
'post_type ' => 'shorturl_domain ' ,
852
859
'posts_per_page ' => -1 , // Fetch all domains
853
860
'orderby ' => 'title ' , // Sort by hostname (title)
854
861
'order ' => $ order
855
862
];
856
-
863
+
857
864
$ domain_query = new \WP_Query ($ args );
858
865
$ link_counts = [];
859
-
866
+
860
867
if ($ domain_query ->have_posts ()) {
861
868
while ($ domain_query ->have_posts ()) {
862
869
$ domain_query ->the_post ();
863
870
$ domain_id = get_the_ID ();
864
871
$ hostname = get_the_title ();
865
-
872
+
866
873
// Query to count links associated with the current domain
867
874
$ link_args = [
868
875
'post_type ' => 'shorturl_link ' ,
@@ -876,19 +883,19 @@ public function render_statistic_section() {
876
883
'posts_per_page ' => -1 , // Count all links
877
884
'fields ' => 'ids ' // Only retrieve post IDs for counting
878
885
];
879
-
886
+
880
887
$ link_query = new \WP_Query ($ link_args );
881
888
$ link_count = $ link_query ->post_count ; // Get the count of associated links
882
-
889
+
883
890
$ link_counts [] = (object ) [
884
891
'hostname ' => $ hostname ,
885
892
'link_count ' => $ link_count
886
893
];
887
894
}
888
895
}
889
-
896
+
890
897
wp_reset_postdata (); // Reset post data after query
891
-
898
+
892
899
// Sort the results by link count if necessary
893
900
if ($ orderby === 'link_count ' ) {
894
901
usort ($ link_counts , function ($ a , $ b ) use ($ order ) {
@@ -899,7 +906,7 @@ public function render_statistic_section() {
899
906
}
900
907
});
901
908
}
902
-
909
+
903
910
// Output the statistics table
904
911
?>
905
912
<div class="wrap">
0 commit comments