render.php
95 lines
| 1 | <?php |
| 2 | defined('ABSPATH') || exit; |
| 3 | |
| 4 | class postaffiliatepro_Widget_TopAffiliates extends postaffiliatepro_Base { |
| 5 | private $title = ''; |
| 6 | private $refresh_minutes = 10; |
| 7 | private $order_by = ''; |
| 8 | private $order_asc = false; |
| 9 | private $limit = 5; |
| 10 | private $row_template = '{$firstname} {$lastname} ({$userid}): clicks: {$clicksRaw}; sales: {$salesCount}; commissions: {$commissions} [parent: {$parentuserid}]'; |
| 11 | |
| 12 | public function __construct($attributes) { |
| 13 | $this->title = isset($attributes['title']) ? sanitize_text_field($attributes['title']) : __('Top Affiliates', 'postaffiliatepro'); |
| 14 | $this->refresh_minutes = isset($attributes['refreshInterval']) ? absint($attributes['refreshInterval']) : $this->refresh_minutes; |
| 15 | $this->order_by = isset($attributes['orderBy']) ? sanitize_key($attributes['orderBy']) : postaffiliatepro_Util_TopAffiliatesHelper::COL_SALES_COUNT; |
| 16 | $this->order_asc = isset($attributes['orderAsc']) ? (bool) $attributes['orderAsc'] : $this->order_asc; |
| 17 | $this->limit = isset($attributes['limit']) ? absint($attributes['limit']) : $this->limit; |
| 18 | $this->row_template = isset($attributes['rowTemplate']) ? wp_kses_post($attributes['rowTemplate']): $this->row_template; |
| 19 | } |
| 20 | |
| 21 | private function getCacheKey() { |
| 22 | return 'pap_top_affiliates_' . md5($this->order_by . $this->order_asc . $this->limit); |
| 23 | } |
| 24 | |
| 25 | public function doRender() { |
| 26 | $cacheKey = $this->getCacheKey(); |
| 27 | $affiliates = get_transient($cacheKey); |
| 28 | |
| 29 | if ($affiliates === false) { |
| 30 | postaffiliatepro_Base::_log('No affiliates cached for widget, loading'); |
| 31 | if (!class_exists('postaffiliatepro_Util_TopAffiliatesHelper')) { |
| 32 | return; |
| 33 | } |
| 34 | $helper = new postaffiliatepro_Util_TopAffiliatesHelper(); |
| 35 | $affiliates = $helper->getTopAffiliatesList($this->order_by, $this->order_asc, $this->limit); |
| 36 | |
| 37 | if (empty($affiliates)) { |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | set_transient($cacheKey, $affiliates, $this->refresh_minutes * MINUTE_IN_SECONDS); |
| 42 | } |
| 43 | |
| 44 | $variables = array( |
| 45 | 'firstname', |
| 46 | 'lastname', |
| 47 | 'userid', |
| 48 | 'parentuserid', |
| 49 | postaffiliatepro_Util_TopAffiliatesHelper::COL_RAW_CLICKS, |
| 50 | postaffiliatepro_Util_TopAffiliatesHelper::COL_SALES_COUNT, |
| 51 | postaffiliatepro_Util_TopAffiliatesHelper::COL_COMMISSIONS |
| 52 | ); |
| 53 | for ($i = 1; $i <= 25; $i++) { |
| 54 | $variables[] = "data$i"; |
| 55 | } |
| 56 | |
| 57 | $wrapperAttributes = get_block_wrapper_attributes(array('class' => 'wp-block-postaffiliatepro-top-affiliates')); |
| 58 | $result = "<div $wrapperAttributes>\n"; |
| 59 | $result .= '<h2 class="wp-block-postaffiliatepro-top-affiliates__title">'.esc_html($this->title)."</h2>\n"; |
| 60 | $result .= '<ol class="wp-block-postaffiliatepro-top-affiliates__list">'; |
| 61 | foreach ($affiliates as $row) { |
| 62 | $result .= '<li class="wp-block-postaffiliatepro-top-affiliates__item">' |
| 63 | .$this->fillRowTemplate($row, $this->row_template, $variables)."</li>\n"; |
| 64 | } |
| 65 | $result .= '</ol></div>'; |
| 66 | echo $result; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Fill template variables for a single affiliate row. |
| 71 | * |
| 72 | * @param object $row Row object exposing a ->get( $key ) method. |
| 73 | */ |
| 74 | private function fillRowTemplate($row, string $template, array $variables): string { |
| 75 | foreach ($variables as $variable) { |
| 76 | $value = htmlspecialchars((string) $row->get($variable), ENT_QUOTES, 'UTF-8'); |
| 77 | if ($variable === postaffiliatepro_Util_TopAffiliatesHelper::COL_COMMISSIONS) { |
| 78 | $value = number_format((float) $value, 2); |
| 79 | } |
| 80 | $template = preg_replace('/\{\$' . preg_quote($variable, '/') . '\}/i', $value, $template); |
| 81 | } |
| 82 | // Backward-compatibility alias: {$clicksAll} → COL_RAW_CLICKS value. |
| 83 | $template = str_replace( |
| 84 | '{$clicksAll}', |
| 85 | htmlspecialchars((string) $row->get(postaffiliatepro_Util_TopAffiliatesHelper::COL_RAW_CLICKS), ENT_QUOTES, 'UTF-8'), |
| 86 | $template |
| 87 | ); |
| 88 | return $template; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | if (isset($attributes)) { |
| 93 | $papWidgetTopAffiliates = new postaffiliatepro_Widget_TopAffiliates($attributes); |
| 94 | $papWidgetTopAffiliates->doRender(); |
| 95 | } |