class-give-donor-wall.php
236 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Give Donor Wall Block Class |
| 4 | * |
| 5 | * @package Give |
| 6 | * @subpackage Classes/Blocks |
| 7 | * @copyright Copyright (c) 2016, GiveWP |
| 8 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
| 9 | * @since 2.3.0 |
| 10 | */ |
| 11 | |
| 12 | // Exit if accessed directly. |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Give_Donor_Wall_Block Class. |
| 19 | * |
| 20 | * This class handles donation forms block. |
| 21 | * |
| 22 | * @since 2.3.0 |
| 23 | */ |
| 24 | class Give_Donor_Wall_Block { |
| 25 | /** |
| 26 | * Instance. |
| 27 | * |
| 28 | * @since |
| 29 | * @access private |
| 30 | * @var Give_Donor_Wall_Block |
| 31 | */ |
| 32 | private static $instance; |
| 33 | |
| 34 | /** |
| 35 | * Singleton pattern. |
| 36 | * |
| 37 | * @since |
| 38 | * @access private |
| 39 | */ |
| 40 | private function __construct() { |
| 41 | } |
| 42 | |
| 43 | |
| 44 | /** |
| 45 | * Get instance. |
| 46 | * |
| 47 | * @since |
| 48 | * @access public |
| 49 | * @return Give_Donor_Wall_Block |
| 50 | */ |
| 51 | public static function get_instance() { |
| 52 | if ( null === static::$instance ) { |
| 53 | self::$instance = new static(); |
| 54 | |
| 55 | self::$instance->init(); |
| 56 | } |
| 57 | |
| 58 | return self::$instance; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Class Constructor |
| 63 | * |
| 64 | * Set up the Give Donation Grid Block class. |
| 65 | * |
| 66 | * @since 2.3.0 |
| 67 | * @access private |
| 68 | */ |
| 69 | private function init() { |
| 70 | add_action( 'init', [ $this, 'register_block' ], 999 ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Register block |
| 75 | * |
| 76 | * @access public |
| 77 | */ |
| 78 | public function register_block() { |
| 79 | // Bailout. |
| 80 | if ( ! function_exists( 'register_block_type' ) ) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | // Register block. |
| 85 | register_block_type( |
| 86 | 'give/donor-wall', |
| 87 | [ |
| 88 | 'render_callback' => [ $this, 'render_block' ], |
| 89 | 'attributes' => [ |
| 90 | 'donorsPerPage' => [ |
| 91 | 'type' => 'string', |
| 92 | 'default' => '12', |
| 93 | ], |
| 94 | 'formID' => [ |
| 95 | 'type' => 'string', |
| 96 | 'default' => '0', |
| 97 | ], |
| 98 | 'ids' => [ |
| 99 | 'type' => 'string', |
| 100 | 'default' => '', |
| 101 | ], |
| 102 | 'orderBy' => [ |
| 103 | 'type' => 'string', |
| 104 | 'default' => 'post_date', |
| 105 | ], |
| 106 | 'order' => [ |
| 107 | 'type' => 'string', |
| 108 | 'default' => 'DESC', |
| 109 | ], |
| 110 | 'paged' => [ |
| 111 | 'type' => 'string', |
| 112 | 'default' => '1', |
| 113 | ], |
| 114 | 'columns' => [ |
| 115 | 'type' => 'string', |
| 116 | 'default' => 'best-fit', |
| 117 | ], |
| 118 | 'showAvatar' => [ |
| 119 | 'type' => 'boolean', |
| 120 | 'default' => true, |
| 121 | ], |
| 122 | 'showName' => [ |
| 123 | 'type' => 'boolean', |
| 124 | 'default' => true, |
| 125 | ], |
| 126 | 'showTotal' => [ |
| 127 | 'type' => 'boolean', |
| 128 | 'default' => true, |
| 129 | ], |
| 130 | 'showDate' => [ |
| 131 | 'type' => 'boolean', |
| 132 | 'default' => true, |
| 133 | ], |
| 134 | 'showComments' => [ |
| 135 | 'type' => 'boolean', |
| 136 | 'default' => true, |
| 137 | ], |
| 138 | 'showAnonymous' => [ |
| 139 | 'type' => 'boolean', |
| 140 | 'default' => true, |
| 141 | ], |
| 142 | 'onlyComments' => [ |
| 143 | 'type' => 'boolean', |
| 144 | 'default' => false, |
| 145 | ], |
| 146 | 'commentLength' => [ |
| 147 | 'type' => 'string', |
| 148 | 'default' => '140', |
| 149 | ], |
| 150 | 'readMoreText' => [ |
| 151 | 'type' => 'string', |
| 152 | 'default' => __( 'Read more', 'give' ), |
| 153 | ], |
| 154 | 'loadMoreText' => [ |
| 155 | 'type' => 'string', |
| 156 | 'default' => __( 'Load more', 'give' ), |
| 157 | ], |
| 158 | 'avatarSize' => [ |
| 159 | 'type' => 'string', |
| 160 | 'default' => '60', |
| 161 | ], |
| 162 | ], |
| 163 | ] |
| 164 | ); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Block render callback |
| 169 | * |
| 170 | * @param array $attributes Block parameters. |
| 171 | * |
| 172 | * @access public |
| 173 | * @return string; |
| 174 | */ |
| 175 | public function render_block( $attributes ) { |
| 176 | $parameters = [ |
| 177 | 'donors_per_page' => absint( $attributes['donorsPerPage'] ), |
| 178 | 'form_id' => absint( $attributes['formID'] ), |
| 179 | 'ids' => $attributes['ids'], |
| 180 | 'orderby' => $attributes['orderBy'], |
| 181 | 'order' => $attributes['order'], |
| 182 | 'pages' => absint( $attributes['paged'] ), |
| 183 | 'columns' => $attributes['columns'], |
| 184 | 'show_avatar' => $attributes['showAvatar'], |
| 185 | 'show_name' => $attributes['showName'], |
| 186 | 'show_total' => $attributes['showTotal'], |
| 187 | 'show_time' => $attributes['showDate'], |
| 188 | 'show_comments' => $attributes['showComments'], |
| 189 | 'anonymous' => $attributes['showAnonymous'], |
| 190 | 'comment_length' => absint( $attributes['commentLength'] ), |
| 191 | 'only_comments' => $attributes['onlyComments'], |
| 192 | 'readmore_text' => $attributes['readMoreText'], |
| 193 | 'loadmore_text' => $attributes['loadMoreText'], |
| 194 | 'avatar_size' => absint( $attributes['avatarSize'] ), |
| 195 | ]; |
| 196 | |
| 197 | $html = Give_Donor_Wall::get_instance()->render_shortcode( $parameters ); |
| 198 | $html = ! empty( $html ) ? $html : $this->blank_slate(); |
| 199 | |
| 200 | return $html; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Return formatted notice when shortcode returns an empty string |
| 205 | * |
| 206 | * @since 2.4.0 |
| 207 | * |
| 208 | * @return string |
| 209 | */ |
| 210 | private function blank_slate() { |
| 211 | if ( ! defined( 'REST_REQUEST' ) ) { |
| 212 | return ''; |
| 213 | } |
| 214 | |
| 215 | ob_start(); |
| 216 | |
| 217 | $content = [ |
| 218 | 'image_url' => GIVE_PLUGIN_URL . 'assets/dist/images/give-icon-full-circle.svg', |
| 219 | 'image_alt' => __( 'GiveWP Icon', 'give' ), |
| 220 | 'heading' => __( 'No donors found.', 'give' ), |
| 221 | 'help' => sprintf( |
| 222 | /* translators: 1: Opening anchor tag. 2: Closing anchor tag. */ |
| 223 | __( 'Need help? Learn more about %1$sDonors%2$s.', 'give' ), |
| 224 | '<a href="http://docs.givewp.com/core-donors/">', |
| 225 | '</a>' |
| 226 | ), |
| 227 | ]; |
| 228 | |
| 229 | include_once GIVE_PLUGIN_DIR . 'includes/admin/views/blank-slate.php'; |
| 230 | |
| 231 | return ob_get_clean(); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | Give_Donor_Wall_Block::get_instance(); |
| 236 |