| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions |
| 4 |
* |
| 5 |
* @package AutomatorWP\BuddyBoss\Functions |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
// Exit if accessed directly |
| 9 |
if( !defined( 'ABSPATH' ) ) exit; |
| 10 |
|
| 11 |
/** |
| 12 |
* Options callback for select2 fields assigned to groups |
| 13 |
* |
| 14 |
* @since 1.0.0 |
| 15 |
* |
| 16 |
* @param stdClass $field |
| 17 |
* |
| 18 |
* @return array |
| 19 |
*/ |
| 20 |
function automatorwp_buddyboss_options_cb_group( $field ) { |
| 21 |
|
| 22 |
// Setup vars |
| 23 |
$value = $field->escaped_value; |
| 24 |
$none_value = 'any'; |
| 25 |
$none_label = __( 'any group', 'automatorwp' ); |
| 26 |
$options = automatorwp_options_cb_none_option( $field, $none_value, $none_label ); |
| 27 |
|
| 28 |
if( ! empty( $value ) ) { |
| 29 |
if( ! is_array( $value ) ) { |
| 30 |
$value = array( $value ); |
| 31 |
} |
| 32 |
|
| 33 |
foreach( $value as $group_id ) { |
| 34 |
|
| 35 |
// Skip option none |
| 36 |
if( $group_id === $none_value ) { |
| 37 |
continue; |
| 38 |
} |
| 39 |
|
| 40 |
$options[$group_id] = automatorwp_buddyboss_get_group_title( $group_id ); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
return $options; |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Get the group title |
| 50 |
* |
| 51 |
* @since 1.0.0 |
| 52 |
* |
| 53 |
* @param int $group_id |
| 54 |
* |
| 55 |
* @return string|null |
| 56 |
*/ |
| 57 |
function automatorwp_buddyboss_get_group_title( $group_id ) { |
| 58 |
|
| 59 |
// Empty title if no ID provided |
| 60 |
if( absint( $group_id ) === 0 ) { |
| 61 |
return ''; |
| 62 |
} |
| 63 |
|
| 64 |
// Bail if BuddyBoss function does not exist |
| 65 |
if ( ! function_exists( 'groups_get_group' ) ) { |
| 66 |
return ''; |
| 67 |
} |
| 68 |
|
| 69 |
$group = groups_get_group( $group_id ); |
| 70 |
|
| 71 |
return $group->name; |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Options callback for select fields assigned to member types |
| 77 |
* |
| 78 |
* @since 1.0.0 |
| 79 |
* |
| 80 |
* @param stdClass $field |
| 81 |
* |
| 82 |
* @return array |
| 83 |
*/ |
| 84 |
function automatorwp_buddyboss_member_types_options_cb( $field ) { |
| 85 |
|
| 86 |
$none_value = 'any'; |
| 87 |
$none_label = __( 'any profile type', 'automatorwp' ); |
| 88 |
$options = automatorwp_options_cb_none_option( $field, $none_value, $none_label ); |
| 89 |
|
| 90 |
$member_types = bp_get_member_types( array(), 'objects' ); |
| 91 |
|
| 92 |
foreach( $member_types as $member_type => $member_type_obj ) { |
| 93 |
$options[$member_type] = $member_type_obj->labels['singular_name']; |
| 94 |
} |
| 95 |
|
| 96 |
return $options; |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Helper function to get the preview from a URL |
| 102 |
* |
| 103 |
* @since 1.5.0 |
| 104 |
* |
| 105 |
* @param string $link |
| 106 |
* |
| 107 |
* @return array|false |
| 108 |
*/ |
| 109 |
function automatorwp_buddyboss_get_link_preview( $link ) { |
| 110 |
|
| 111 |
// Bail if not is a valid URL |
| 112 |
if ( ! filter_var( $link, FILTER_VALIDATE_URL ) ) { |
| 113 |
return false; |
| 114 |
} |
| 115 |
|
| 116 |
$title = ''; |
| 117 |
$description = ''; |
| 118 |
$image_url = ''; |
| 119 |
|
| 120 |
// Check if BP parse URL can take the information |
| 121 |
if( function_exists( 'bp_core_parse_url' ) ) { |
| 122 |
|
| 123 |
$bp_data = bp_core_parse_url( $link ); |
| 124 |
|
| 125 |
$title = $bp_data['title']; |
| 126 |
$description = $bp_data['description']; |
| 127 |
$image_url = ( is_array( $bp_data['images'] ) && count( $bp_data['images'] ) ) ? $bp_data['images'][0] : ''; |
| 128 |
|
| 129 |
// All information done so return it here without continue |
| 130 |
if( ! empty( $title ) && ! empty( $description ) && ! empty( $image_url ) ) { |
| 131 |
return array( |
| 132 |
'url' => $link, |
| 133 |
'title' => $title, |
| 134 |
'description' => $description, |
| 135 |
'image_url' => $image_url |
| 136 |
); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
// Extract HTML using curl |
| 141 |
$ch = curl_init(); |
| 142 |
|
| 143 |
curl_setopt( $ch, CURLOPT_HEADER, 0 ); |
| 144 |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); |
| 145 |
curl_setopt( $ch, CURLOPT_URL, $link ); |
| 146 |
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 ); |
| 147 |
|
| 148 |
$data = curl_exec( $ch ); |
| 149 |
curl_close( $ch ); |
| 150 |
|
| 151 |
if( empty( $data ) ) { |
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
// Load HTML to DOM Object |
| 156 |
$dom = new DOMDocument(); |
| 157 |
@$dom->loadHTML( $data ); |
| 158 |
|
| 159 |
// Parse DOM to get Title |
| 160 |
if( empty( $title ) ) { |
| 161 |
$nodes = $dom->getElementsByTagName('title'); |
| 162 |
|
| 163 |
if( $nodes->length > 0 ) { |
| 164 |
$title = $nodes->item(0)->nodeValue; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
// Parse DOM to get Meta Description |
| 169 |
$metas = $dom->getElementsByTagName('meta'); |
| 170 |
|
| 171 |
for ($i = 0; $i < $metas->length; $i ++) { |
| 172 |
$meta = $metas->item( $i ); |
| 173 |
|
| 174 |
// Description meta |
| 175 |
if ( $meta->getAttribute('name') == 'description' && empty( $description ) ) { |
| 176 |
$description = $meta->getAttribute('content'); |
| 177 |
} |
| 178 |
|
| 179 |
// OG Metas (they have priority) |
| 180 |
if ( $meta->getAttribute('property') == 'og:title' ) { |
| 181 |
$title = $meta->getAttribute('content'); |
| 182 |
} |
| 183 |
|
| 184 |
if ( $meta->getAttribute('property') == 'og:description' ) { |
| 185 |
$description = $meta->getAttribute('content'); |
| 186 |
} |
| 187 |
|
| 188 |
if ( $meta->getAttribute('property') == 'og:image' ) { |
| 189 |
$image_url = $meta->getAttribute('content'); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
// Parse DOM to get Images |
| 194 |
if( empty( $image_url ) ) { |
| 195 |
$images = $dom->getElementsByTagName('img'); |
| 196 |
|
| 197 |
for ( $i = 0; $i < $images->length; $i ++) { |
| 198 |
$image = $images->item( $i ); |
| 199 |
$src = $image->getAttribute( 'src' ); |
| 200 |
|
| 201 |
if( filter_var( $src, FILTER_VALIDATE_URL ) ) { |
| 202 |
$image_url = $src; |
| 203 |
break; |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
return array( |
| 209 |
'url' => $link, |
| 210 |
'title' => $title, |
| 211 |
'description' => $description, |
| 212 |
'image_url' => $image_url |
| 213 |
); |
| 214 |
|
| 215 |
} |