PluginProbe
Lasso Lite – Affiliate Link Manager & Product Displays / 109
Lasso Lite – Affiliate Link Manager & Product Displays v109
158 157 155 156 154 153 152 151 150 149 148 trunk 0.9.9 104 105 106 107 108 109 110 111 112 113 114 115 All 57 releases
simple-urls / pages / class-ajax.php

class-ajax.php in Lasso Lite – Affiliate Link Manager & Product Displays 109, at pages/class-ajax.php

190 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Lasso Lite General - Ajax.
4 *
5 * @package Pages
6 */
7
8 namespace LassoLite\Pages;
9
10 use LassoLite\Classes\Affiliate_Link;
11 use LassoLite\Classes\Meta_Enum;
12 use LassoLite\Classes\Helper;
13 use LassoLite\Classes\Setting;
14 use LassoLite\Classes\SURL;
15
16 /**
17 * Lasso General - Ajax.
18 */
19 class Ajax {
20 /**
21 * Declare "Lasso Lite ajax requests" to WordPress.
22 */
23 public function register_hooks() {
24 add_action( 'wp_ajax_lasso_lite_add_a_new_link', array( $this, 'lasso_lite_add_a_new_link' ) );
25 add_action( 'wp_ajax_lasso_lite_get_single', array( $this, 'lasso_lite_get_single' ) );
26 add_action( 'wp_ajax_lasso_lite_get_shortcode_content', array( $this, 'lasso_lite_get_shortcode_content' ) );
27 add_action( 'wp_ajax_lasso_lite_get_display_html', array( $this, 'lasso_lite_get_display_html' ) );
28 add_action( 'wp_ajax_lasso_lite_get_link_quick_detail', array( $this, 'lasso_lite_get_link_quick_detail' ) );
29 add_action( 'wp_ajax_lasso_lite_save_link_quick_detail', array( $this, 'lasso_lite_save_link_quick_detail' ) );
30 add_action( 'wp_ajax_lasso_lite_get_setup_progress', array( $this, 'lasso_lite_get_setup_progress' ) );
31 add_action( 'wp_ajax_lasso_lite_save_support', array( $this, 'lasso_lite_save_support' ) );
32 }
33
34 /**
35 * Add a new Lasso link
36 */
37 public function lasso_lite_add_a_new_link() {
38 $lasso_lite_affiliate_link = new Affiliate_Link();
39 return $lasso_lite_affiliate_link->add_a_new_link();
40 }
41
42 /**
43 * Get display html
44 */
45 public function lasso_lite_get_single() {
46 // phpcs:ignore
47 $post = Helper::POST();
48 $page = intval( $post['page'] ) ?? 1;
49 $limit = intval( $post['limit'] ) ?? 5;
50 $keyword = $post['keyword'] ?? '';
51 $list = SURL::get_list( $keyword, $page, $limit );
52 $output = array();
53
54 foreach ( $list as $surl ) {
55 $lasso_url = Affiliate_Link::get_lasso_url( $surl->get_id() );
56
57 $output[] = array(
58 'post_id' => $lasso_url->id,
59 'title' => $lasso_url->name,
60 'permalink' => $lasso_url->permalink,
61 'link_detail' => $lasso_url->edit_link,
62 'img_src' => $lasso_url->image_src,
63 'redirect' => $lasso_url->target_url,
64 'slug' => $lasso_url->slug,
65 );
66 }
67
68 $data['output'] = $output;
69 $data['total'] = SURL::total( $keyword );
70 $data['limit_on_page'] = $limit;
71 $data['page'] = $page;
72
73 wp_send_json_success( $data );
74 }
75
76 /**
77 * Get display html
78 */
79 public function lasso_lite_get_shortcode_content() {
80 $shortcode = stripslashes( $_GET['shortcode'] ?? '' ); // phpcs:ignore
81 $html = '';
82
83 if ( '' !== $shortcode ) {
84 $html = do_shortcode( $shortcode );
85 }
86
87 wp_send_json_success(
88 array(
89 'shortcode' => $shortcode,
90 'html' => $html,
91 )
92 );
93 }
94
95 /**
96 * Get display html
97 */
98 public function lasso_lite_get_display_html() {
99 $html = Helper::get_display_modal_html();
100
101 wp_send_json_success(
102 array(
103 'html' => $html,
104 )
105 );
106 }
107
108 /**
109 * Get a new Lasso quick link
110 */
111 public function lasso_lite_get_link_quick_detail() {
112 $lasso_id = $_POST['post_id'] ?? null; // phpcs:ignore
113
114 if ( ! empty( $lasso_id ) ) {
115 $lasso_url = Affiliate_Link::get_lasso_url( $lasso_id, true );
116
117 wp_send_json_success(
118 array(
119 'success' => true,
120 'lasso_url' => $lasso_url,
121 )
122 );
123 } else {
124 wp_send_json_error( 'No affiliate link to get.' );
125 }
126 }
127
128 /**
129 * Save a lasso link with basic data
130 */
131 public function lasso_lite_save_link_quick_detail() {
132 $data = Helper::POST(); // phpcs:ignore
133 $lasso_id = $data['lasso_id'] ?? null; // phpcs:ignore
134 $lasso_post = get_post( $lasso_id );
135 $thumbnail_id = intval( $data['thumbnail_id'] ?? 0 );
136
137 if ( $lasso_post ) {
138 $lasso_lite_post = array(
139 'ID' => $lasso_post->ID,
140 'post_title' => trim( $data['affiliate_name'] ),
141 'meta_input' => array(
142 Meta_Enum::LASSO_LITE_CUSTOM_THUMBNAIL => trim( $data['thumbnail_image_url'] ?? '' ),
143 Meta_Enum::BUY_BTN_TEXT => trim( $data['buy_btn_text'] ?? '' ),
144 Meta_Enum::DESCRIPTION => trim( $data['description'] ?? '' ),
145 Meta_Enum::BADGE_TEXT => trim( $data['badge_text'] ?? '' ),
146 ),
147 );
148
149 wp_update_post( $lasso_lite_post );
150
151 // ? update thumbnail
152 if ( $thumbnail_id > 0 ) {
153 set_post_thumbnail( $lasso_id, $thumbnail_id );
154 $image_url = wp_get_attachment_url( $thumbnail_id );
155 update_post_meta( $lasso_id, Meta_Enum::LASSO_LITE_CUSTOM_THUMBNAIL, $image_url );
156 } else {
157 delete_post_thumbnail( $lasso_id );
158 }
159
160 clean_post_cache( $lasso_id ); // ? clean post cache
161 wp_send_json_success(
162 array(
163 'success' => true,
164 )
165 );
166 } else {
167 wp_send_json_success(
168 array(
169 'success' => false,
170 'msg' => 'No affiliate link existed.',
171 )
172 );
173 }
174 }
175
176 /**
177 * Get setup progress information
178 */
179 public function lasso_lite_get_setup_progress() {
180 wp_send_json_success( Helper::get_setup_progress_information() );
181 }
182
183 /**
184 * Do save support to open intercom chat
185 */
186 public function lasso_lite_save_support() {
187 Setting::save_support();
188 }
189 }
190