name = 'sites';
$this->label = __( 'Sites', 'acf-sites' );
$this->category = __( 'Relation', 'acf-sites' );
$this->defaults = array(
'field_type' => 'checkbox',
'status' => 'all',
'hide_main' => 1,
'allow_null' => 0,
'return_value' => 'id',
'site__not_in' => '',
);
// do not delete!
parent::__construct();
// settings
$this->settings = $settings;
}
/*
* create_options()
*
* Create extra options for your field. This is rendered when editing a field.
* The value of $field['name'] can be used (like below) to save extra data to the $field
*
* @type action
* @since 3.6
* @date 23/01/13
*
* @param $field - an array holding all the field's data
*/
function create_options( $field ) {
// defaults?
$field = array_merge( $this->defaults, $field );
// key is needed in the field names to correctly save the data
$key = $field['name'];
?>
'select',
'name' => 'fields[' . $key . '][field_type]',
'value' => $field['field_type'],
'layout' => 'horizontal',
'optgroup' => true,
'choices' => array(
__( 'Multiple Values','acf-sites' ) => array(
'checkbox' => __( 'Checkbox', 'acf-sites' ),
'multi_select' => __( 'Multi Select', 'acf-sites' ),
),
__( 'Single Value', 'acf-sites' ) => array(
'radio' => __( 'Radio Buttons', 'acf-sites' ),
'select' => _x( 'Select', 'noun', 'acf-sites' ),
),
),
));
?>
'radio',
'name' => 'fields[' . $key . '][allow_null]',
'value' => $field['allow_null'],
'layout' => 'horizontal',
'choices' => array(
1 => __( 'Yes' ),
0 => __( 'No' ),
),
));
?>
'radio',
'name' => 'fields[' . $key . '][status]',
'value' => $field['status'],
'layout' => 'horizontal',
'choices' => array(
'all' => __( 'All', 'acf-sites' ),
'public' => __( 'Public', 'acf-sites' ),
'archived' => __( 'Archived', 'acf-sites' ),
'mature' => __( 'Mature', 'acf-sites' ),
'spam' => __( 'Spam', 'acf-sites' ),
'deleted' => __( 'Deleted', 'acf-sites' ),
),
));
?>
'radio',
'name' => 'fields[' . $key . '][hide_main]',
'value' => $field['hide_main'],
'layout' => 'horizontal',
'choices' => array(
1 => __( 'Yes' ),
0 => __( 'No' ),
),
));
?>
'text',
'name' => 'fields[' . $key . '][site__not_in]',
'value' => $field['site__not_in'],
));
?>
'radio',
'name' => 'fields[' . $key . '][return_value]',
'value' => $field['return_value'],
'layout' => 'horizontal',
'choices' => array(
'id' => __( 'Site ID', 'acf-sites' ),
'name' => __( 'Site name', 'acf-sites' ),
'url' => __( 'Site URL', 'acf-sites' ),
'all' => __( 'All (Array)', 'acf-sites' ),
),
));
?>
' . __( 'You need to have set up a multisite network for this field to work', 'acf-sites' ) . '';
return;
}
/**
* Setup query.
*/
global $wpdb;
$args = array();
if ( $field['hide_main'] ) {
$args['offset'] = 1;
}
if ( 'all' != $field['status'] ) {
$args[ $field['status'] ] = '1';
}
if ( '' != $field['site__not_in'] ) {
$excluded_sites = explode( ',', $field['site__not_in'] );
$excluded_sites_2 = explode( ', ', $field['site__not_in'] );
$excluded_merged = array_merge( $excluded_sites, $excluded_sites_2 );
$args['site__not_in'] = $excluded_merged;
}
$sites = get_sites( $args );
if ( empty( $sites ) ) {
echo '
' . __( 'No sites found with the current field settings.', 'acf-sites' ) . '
';
}
if ( 'select' == $field['field_type'] ) {
$field['multiple'] = 0;
$this->render_field_select( $field, $sites );
} elseif ( 'multi_select' == $field['field_type'] ) {
$field['multiple'] = 1;
$this->render_field_select( $field, $sites );
} elseif ( 'radio' == $field['field_type'] ) {
$this->render_field_checkbox( $field, $sites );
} elseif ( 'checkbox' == $field['field_type'] ) {
$this->render_field_checkbox( $field, $sites );
}
echo '
';
}
/*
* render_field_select()
*
* Create the HTML interface for your field
*
* @type action
* @since 3.6
* @date 23/01/13
*
* @param $field - an array holding all the field's data
*/
function render_field_select( $field, $sites ) {
if ( $field['multiple'] ) {
$multiple = 'multiple="multiple" size="5"';
$field['name'] .= '[]';
}
?>
>
blog_id, $field['value'] ) ? 'selected' : '' ); ?>
>blogname; ?>
blogname;
} elseif ( 'url' == $field['return_value'] ) {
$formatted_values[] = $site_details->siteurl;
} elseif ( 'all' == $field['return_value'] ) {
$formatted_values[] = array(
'id' => $site_details->blog_id,
'url' => $site_details->siteurl,
'name' => $site_details->blogname,
);
}
}
return $formatted_values;
}
}
// initialize
new Acf_Field_Sites( $this->settings );
endif; // class_exists check