| 1 |
<?php |
| 2 |
/** |
| 3 |
* UsersWP extensions screen related functions |
| 4 |
* |
| 5 |
* All UsersWP extensions screen related functions can be found here. |
| 6 |
* |
| 7 |
* @since 1.0.24 |
| 8 |
*/ |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* UsersWP_Admin_Addons Class. |
| 15 |
*/ |
| 16 |
class UsersWP_Admin_Addons extends Ayecode_Addons { |
| 17 |
|
| 18 |
|
| 19 |
/** |
| 20 |
* Get the extensions page tabs. |
| 21 |
* |
| 22 |
* @return array of tabs. |
| 23 |
*/ |
| 24 |
public function get_tabs(){ |
| 25 |
$tabs = array( |
| 26 |
'addons' => __("Addons", "userswp"), |
| 27 |
'recommended_plugins' => __("Recommended plugins", "userswp"), |
| 28 |
'membership' => __("Membership", "userswp"), |
| 29 |
); |
| 30 |
|
| 31 |
return $tabs; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Get section content for the addons screen. |
| 36 |
* |
| 37 |
* @param string $section_id |
| 38 |
* |
| 39 |
* @return array |
| 40 |
*/ |
| 41 |
public function get_section_data( $section_id ) { |
| 42 |
$section = $this->get_tab( $section_id ); |
| 43 |
$api_url = "https://userswp.io/edd-api/v2/products/"; |
| 44 |
$section_data = new stdClass(); |
| 45 |
|
| 46 |
//echo '###'.$section_id; |
| 47 |
|
| 48 |
if($section_id=='recommended_plugins'){ |
| 49 |
$section_data->products = $this->get_recommend_wp_plugins_edd_formatted(); |
| 50 |
} |
| 51 |
elseif ( ! empty( $section ) ) { |
| 52 |
if ( false === ( $section_data = get_transient( 'uwp_addons_section_' . $section_id ) ) ) { //@todo restore after testing |
| 53 |
//if ( 1==1) { |
| 54 |
|
| 55 |
$query_args = array( 'category' => $section_id, 'number' => 100); |
| 56 |
$query_args = apply_filters('wpeu_edd_api_query_args',$query_args,$api_url,$section_id); |
| 57 |
|
| 58 |
$raw_section = wp_safe_remote_get( esc_url_raw( add_query_arg($query_args ,$api_url) ), array( 'user-agent' => 'UsersWP Addons Page','timeout' => 15, ) ); |
| 59 |
|
| 60 |
if ( ! is_wp_error( $raw_section ) ) { |
| 61 |
$section_data = json_decode( wp_remote_retrieve_body( $raw_section ) ); |
| 62 |
|
| 63 |
if ( ! empty( $section_data->products ) ) { |
| 64 |
set_transient( 'uwp_addons_section_' . $section_id, $section_data, DAY_IN_SECONDS ); |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
$products = isset($section_data->products) ? $section_data->products : ''; |
| 71 |
|
| 72 |
return apply_filters( 'uwp_addons_section_data', $products, $section_id ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Outputs a button. |
| 77 |
* |
| 78 |
* @param string $url |
| 79 |
* @param string $text |
| 80 |
* @param string $theme |
| 81 |
* @param string $plugin |
| 82 |
*/ |
| 83 |
public function output_button( $addon ) { |
| 84 |
$current_tab = empty( $_GET['tab'] ) ? 'addons' : sanitize_title( $_GET['tab'] ); |
| 85 |
// $button_text = __('Free','userswp'); |
| 86 |
// $licensing = false; |
| 87 |
// $installed = false; |
| 88 |
// $price = ''; |
| 89 |
// $license = ''; |
| 90 |
// $slug = ''; |
| 91 |
// $url = isset($addon->info->link) ? $addon->info->link : ''; |
| 92 |
// $class = 'button-primary'; |
| 93 |
// $install_status = 'get'; |
| 94 |
// $onclick = ''; |
| 95 |
|
| 96 |
$wp_org_themes = array('supreme-directory','directory-starter'); |
| 97 |
|
| 98 |
$button_args = array( |
| 99 |
'type' => $current_tab, |
| 100 |
'id' => isset($addon->info->id) ? absint($addon->info->id) : '', |
| 101 |
'title' => isset($addon->info->title) ? $addon->info->title : '', |
| 102 |
'button_text' => __('Free','userswp'), |
| 103 |
'price_text' => __('Free','userswp'), |
| 104 |
'link' => isset($addon->info->link) ? $addon->info->link : '', // link to product |
| 105 |
'url' => isset($addon->info->link) ? $addon->info->link : '', // button url |
| 106 |
'class' => 'button-primary', |
| 107 |
'install_status' => 'get', |
| 108 |
'installed' => false, |
| 109 |
'price' => '', |
| 110 |
'licensing' => isset($addon->licensing->enabled) && $addon->licensing->enabled ? true : false, |
| 111 |
'license' => isset($addon->licensing->license) && $addon->licensing->license ? $addon->licensing->license : '', |
| 112 |
'onclick' => '', |
| 113 |
'slug' => isset($addon->info->slug) ? $addon->info->slug : '', |
| 114 |
'active' => false, |
| 115 |
'file' => '', |
| 116 |
'update_url' => '', |
| 117 |
); |
| 118 |
|
| 119 |
if($current_tab == 'addons' && isset($addon->info->id) && $addon->info->id){ |
| 120 |
include_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ); //for plugins_api.. |
| 121 |
if(!empty($addon->licensing->edd_slug)){$button_args['slug'] = $addon->licensing->edd_slug;} |
| 122 |
$status = $this->install_plugin_install_status($addon); |
| 123 |
$button_args['file'] = isset($status['file']) ? $status['file'] : ''; |
| 124 |
if(isset($status['status'])){$button_args['install_status'] = $status['status'];} |
| 125 |
$button_args['update_url'] = "https://userswp.io"; |
| 126 |
}elseif($current_tab == 'themes' && isset($addon->info->id) && $addon->info->id) { |
| 127 |
if(!empty($addon->licensing->edd_slug)){$button_args['slug'] = $addon->licensing->edd_slug;} |
| 128 |
$button_args['installed'] = $this->is_theme_installed($addon); |
| 129 |
if(!in_array($button_args['slug'],$wp_org_themes)){ |
| 130 |
$button_args['update_url'] = "https://userswp.io"; |
| 131 |
} |
| 132 |
}elseif($current_tab == 'recommended_plugins' && isset($addon->info->slug) && $addon->info->slug){ |
| 133 |
include_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ); //for plugins_api.. |
| 134 |
$status = install_plugin_install_status(array("slug"=>$button_args['slug'],"version"=>"")); |
| 135 |
$button_args['install_status'] = isset($status['status']) ? $status['status'] : 'install'; |
| 136 |
$button_args['file'] = isset($status['file']) ? $status['file'] : ''; |
| 137 |
} |
| 138 |
|
| 139 |
// set price |
| 140 |
if(isset($addon->pricing) && !empty($addon->pricing)){ |
| 141 |
if(is_object($addon->pricing)){ |
| 142 |
$prices = (Array)$addon->pricing; |
| 143 |
$button_args['price'] = reset($prices); |
| 144 |
}elseif(isset($addon->pricing)){ |
| 145 |
$button_args['price'] = $addon->pricing; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
// set price text |
| 150 |
if( $button_args['price'] && $button_args['price'] != '0.00' ){ |
| 151 |
$button_args['price_text'] = sprintf( __('From: $%d', 'userswp'), $button_args['price']); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
// set if installed |
| 156 |
if(in_array($button_args['install_status'], array('installed','latest_installed','update_available','newer_installed'))){ |
| 157 |
$button_args['installed'] = true; |
| 158 |
} |
| 159 |
|
| 160 |
// print_r($button_args); |
| 161 |
// set if active |
| 162 |
if($button_args['installed'] && ($button_args['file'] || $button_args['type'] == 'themes')){ |
| 163 |
if($button_args['type'] != 'themes'){ |
| 164 |
$button_args['active'] = is_plugin_active($button_args['file']); |
| 165 |
}else{ |
| 166 |
$button_args['active'] = $this->is_theme_active($addon); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
// set button text and class |
| 171 |
if($button_args['active']){ |
| 172 |
$button_args['button_text'] = __('Active','userswp'); |
| 173 |
$button_args['class'] = ' button-secondary disabled '; |
| 174 |
}elseif($button_args['installed']){ |
| 175 |
$button_args['button_text'] = __('Activate','userswp'); |
| 176 |
|
| 177 |
if($button_args['type'] != 'themes'){ |
| 178 |
if ( current_user_can( 'manage_options' ) ) { |
| 179 |
$button_args['url'] = wp_nonce_url(admin_url('plugins.php?action=activate&plugin='.$button_args['file']), 'activate-plugin_' . $button_args['file']); |
| 180 |
}else{ |
| 181 |
$button_args['url'] = '#'; |
| 182 |
} |
| 183 |
}else{ |
| 184 |
if ( current_user_can( 'switch_themes' ) ) { |
| 185 |
$button_args['url'] = $this->get_theme_activation_url($addon); |
| 186 |
}else{ |
| 187 |
$button_args['url'] = '#'; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
}else{ |
| 192 |
if($button_args['type'] == 'recommended_plugins'){ |
| 193 |
$button_args['button_text'] = __('Install','userswp'); |
| 194 |
}else{ |
| 195 |
$button_args['button_text'] = __('Get it','userswp'); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
// filter the button arguments |
| 200 |
$button_args = apply_filters('edd_api_button_args',$button_args); |
| 201 |
|
| 202 |
// set price text |
| 203 |
if(isset($button_args['price_text'])){ |
| 204 |
?> |
| 205 |
<a |
| 206 |
target="_blank" |
| 207 |
class="addons-price-text" |
| 208 |
href="<?php echo esc_url( $button_args['link'] ); ?>"> |
| 209 |
<?php echo esc_html( $button_args['price_text'] ); ?> |
| 210 |
</a> |
| 211 |
<?php |
| 212 |
} |
| 213 |
|
| 214 |
$target = ''; |
| 215 |
if ( ! empty( $button_args['url'] ) ) { |
| 216 |
$target = strpos($button_args['url'], get_site_url()) !== false ? '' : '_blank'; |
| 217 |
} |
| 218 |
|
| 219 |
?> |
| 220 |
<a |
| 221 |
data-licence="<?php echo esc_attr($button_args['license']);?>" |
| 222 |
data-licensing="<?php echo $button_args['licensing'] ? 1 : 0;?>" |
| 223 |
data-title="<?php echo esc_attr($button_args['title']);?>" |
| 224 |
data-type="<?php echo esc_attr($button_args['type']);?>" |
| 225 |
data-text-error-message="<?php esc_attr_e('Something went wrong!','userswp');?>" |
| 226 |
data-text-activate="<?php esc_attr_e('Activate','userswp');?>" |
| 227 |
data-text-activating="<?php esc_attr_e('Activating','userswp');?>" |
| 228 |
data-text-deactivate="<?php esc_attr_e('Deactivate','userswp');?>" |
| 229 |
data-text-installed="<?php esc_attr_e('Installed','userswp');?>" |
| 230 |
data-text-install="<?php esc_attr_e('Install','userswp');?>" |
| 231 |
data-text-installing="<?php esc_attr_e('Installing','userswp');?>" |
| 232 |
data-text-error="<?php esc_attr_e('Error','userswp');?>" |
| 233 |
<?php if(!empty($button_args['onclick'])){echo " onclick='".esc_html($button_args['onclick'] )."' ";}?> |
| 234 |
target="<?php echo esc_attr( $target );?>" |
| 235 |
class="addons-button <?php echo esc_attr( $button_args['class'] ); ?>" |
| 236 |
href="<?php echo esc_url( $button_args['url'] ); ?>"> |
| 237 |
<?php echo esc_html( $button_args['button_text'] ); ?> |
| 238 |
</a> |
| 239 |
<?php |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
/** |
| 244 |
* Handles output of the addons page in admin. |
| 245 |
*/ |
| 246 |
public function output() { |
| 247 |
$tabs = $this->get_tabs(); |
| 248 |
$sections = $this->get_sections(); |
| 249 |
$theme = wp_get_theme(); |
| 250 |
$section_keys = array_keys( $sections ); |
| 251 |
$current_section = isset( $_GET['section'] ) ? sanitize_text_field( $_GET['section'] ) : current( $section_keys ); |
| 252 |
$current_tab = empty( $_GET['tab'] ) ? 'addons' : sanitize_title( $_GET['tab'] ); |
| 253 |
include_once( USERSWP_PATH . '/admin/views/html-admin-page-addons.php' ); |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* A list of recommended wp.org plugins. |
| 258 |
* @return array |
| 259 |
*/ |
| 260 |
public function get_recommend_wp_plugins(){ |
| 261 |
$plugins = array( |
| 262 |
'geodirectory' => array( |
| 263 |
'url' => 'https://wordpress.org/plugins/geodirectory/', |
| 264 |
'slug' => 'geodirectory', |
| 265 |
'name' => 'GeoDirectory', |
| 266 |
'desc' => __('Turn any WordPress theme into a global business directory portal.','userswp'), |
| 267 |
), |
| 268 |
'invoicing' => array( |
| 269 |
'url' => 'https://wordpress.org/plugins/invoicing/', |
| 270 |
'slug' => 'invoicing', |
| 271 |
'name' => 'Invoicing', |
| 272 |
'desc' => __('Create & Send Invoices, Manage Taxes & VAT. Collect One Time & Recurring Payments.','userswp'), |
| 273 |
), |
| 274 |
); |
| 275 |
|
| 276 |
return $plugins; |
| 277 |
} |
| 278 |
} |
| 279 |
|