'SoundCloud Options',
'menu_title' => 'SoundCloud',
'menu_slug' => 'soundcloud-settings',
'capability' => $capability,
'parent_slug' => $parent_slug,
'position' => false,
'icon_url' => false,
);
return $pages;
}
// Get settings args
function get_acf_scp_page_args ($menu_slug) {
// not found page
if( empty($GLOBALS['extra_settings_pages'][ $menu_slug ]) ) {
return false;
}
// get vars
$page = $GLOBALS['extra_settings_pages'][ $menu_slug ];
// return
return $page;
}
// SoundCloud Client ID Input
function settings_field_acf_section_intro() {
echo '
' . __( sprintf('Configure your SoundCloud keys here for the %s to do its magic. If you have not registered an app in SoundCloud, please follow the guide here ','ACF SoundCloud Playlist ','http://soundcloud.com/you/apps/new'), 'acf/scp') . '
';
}
// Field: Status
function settings_field_acf_scp_status_html() {
$tokey = scp_get_app('tokey');
if ( empty($tokey) ) :
echo 'Disconnected ';
else :
/* disconnect URL */
$d_link = scp_settings_url( array(
'sc_disconnect' => 1,
'_wpnonce' => wp_create_nonce( 'acfscpapp-deauthorizeOK', __FILE__ ),
) );
$d_show = ( isset( $_REQUEST['show_disconnect'] ) && $_REQUEST['show_disconnect'] ) ? '': 'display:none';
?>
Connected
Disconnect SoundCloud?
registering an app','http://soundcloud.com/you/apps/new'), 'acf-scp'); ?>
'',
'secretkey' => '',
'tokey' => '',
);
$defaults = apply_filters( 'extra_default_theme_options', $defaults );
$options = wp_parse_args( $saved, $defaults );
$options = array_intersect_key( $options, $defaults );
return $options;
}
// Sanitize and validate updated theme options
function acf_scp_app_validate( $input ) {
$output = array();
if( isset( $input['clientkey'] ) && !empty( $input['clientkey'] ) )
$output['clientkey'] = sanitize_text_field( $input['clientkey'] );
if( isset( $input['secretkey'] ) && !empty( $input['secretkey'] ) )
$output['secretkey'] = sanitize_text_field( $input['secretkey'] );
if( isset( $input['tokey'] ) && !empty( $input['tokey'] ) )
$output['tokey'] = sanitize_text_field( $input['tokey'] );
$error = settings_required_scp_fields( $output, array( 'clientkey', 'secretkey' ) );
if( $error )
add_settings_error( "soundcloud-settings", "", "You miss to enter one or more keys", 'error' );
return apply_filters( 'acf_scp_app_validate', $output, $input );
}
// Render settings page
function acf_scp_render_page() {
// globals cc: acf/pro/admin/options.page
global $plugin_page;
$page = get_acf_scp_page_args($plugin_page);
?>
settings['version'] );
if( !empty($v) );
$v = $v[0];
}
if( $v == 4 ) {
// @version 4
$args_acf = array(
'post_type' => 'acf',
'post_status' => 'publish',
'posts_per_page' => -1,
);
$acf_groups = get_posts( $args_acf );
foreach( $acf_groups as $acf_group ) {
$meta = get_post_custom($acf_group->ID);
$meta_f = array_filter_use_key( $meta );
foreach ($meta_f as $field) {
$field = maybe_unserialize( $field[0] );
$key = $field['key'];
$name = $field['name'];
// filter fields and take out type scplaylist only.
maybe_scp_field_delete($field['type'], $key, $name);
}
}
} elseif( $v == 5 ) {
// @version 5
$args_acf = array(
'post_type' => 'acf-field',
'post_status' => 'publish',
'posts_per_page' => -1,
);
$acf_fields = get_posts( $args_acf );
foreach( $acf_fields as $acf_field ) {
$field = maybe_unserialize( $acf_field->post_content );
$key = $acf_field->post_name;
$name = $acf_field->post_excerpt;
maybe_scp_field_delete($field['type'], $key, $name);
}
}
} else {
/* error notice */
wp_redirect( scp_settings_url( array('message'=>'acf-soundcloud-disallowed') ) );
exit;
}
wp_redirect( scp_settings_url( array('message'=>'acf-soundcloud-disconnected') ) );
exit;
}
add_action( 'acf_scp_page_init', 'acf_scp_deauthorization_check');
/*
* scp_settings_sanitize_check
* Sanitize field and check if field has clean input.
*
* @return $error bool
*/
function settings_required_scp_fields( $keys, $reqs ){
if( empty( $reqs ) )
return false;
foreach( $reqs as $req ){
$err = ( !isset( $keys[$req] ) ) ? 1: 0;
if( $err )
return true;
}
return false;
}
/*
* maybe_scp_field_delete
* verify if field is scp and delete if passed.
*
* @return $return bool
*/
function maybe_scp_field_delete( $type, $key, $name ) {
global $wpdb;
// filter fields and take out type scplaylist only.
if( $type == 'scplaylist' ) :
// supportted location
$locations = array('post', 'widget', 'option');
// @todo Is it ideal to check its location and loop through rule match
// just to save going through the support location (post, widgets, options)?
foreach( $locations as $location) {
switch( $location ) {
case 'post':
### UNCOMMENT IF LIVE - Post type
$success = $wpdb->query(
$wpdb->prepare(
"
DELETE FROM $wpdb->postmeta
WHERE $wpdb->postmeta.meta_key = %s
OR $wpdb->postmeta.meta_value = %s
", $name, $key
)
);
break;
case 'widget':
case 'option':
### UNCOMMENT IF LIVE - Widgets
$option_name = $wpdb->get_results(
$wpdb->prepare(
"
SELECT $wpdb->options.option_name
FROM $wpdb->options WHERE $wpdb->options.option_value = %s
", $key
)
);
if( !empty($option_name) ) {
### @since 3.x - ACF has transient like field name in db
$option_name = ltrim($option_name[0]->option_name, '_');
### UNCOMMENT IF LIVE - Option
$success = $wpdb->query(
$wpdb->prepare(
"
DELETE FROM $wpdb->options
WHERE $wpdb->options.option_name = %s
OR $wpdb->options.option_value = %s
", $option_name, $key
)
);
}
break;
default:
$success = 0;
break;
}
}
endif;
return $success;
}
// WORK ONLY IN PHP 5.6.2
// function acf_scp_fields_only( $key, $val ) {
// return preg_match("/field_/", $key);
// }
/* scp_fields_only
* check if custom field is an acf instance.
*
* @return $return bool
*/
function array_filter_use_key( $meta ) {
foreach( array_keys($meta) as $key ) {
if ( !preg_match("/^field_\d+/", $key) ) {
unset($meta[$key]);
}
}
return $meta;
}