neuf-associations: format PHP code as per PSR-2

This commit is contained in:
ponas committed 2019-05-04 19:09:33 +02:00
1 parent 0ca92815f3
commit f3110839c2
3 files changed
+144 -133

No files matched your search

@@ -1,84 +1,90 @@
<?php
/* Add custom columns. */
function neuf_associations_change_columns( $cols ) {
$custom_cols = array(
'cb' => '<input type="checkbox" />',
'title' => __( 'Association', 'neuf_assoc' ),
'image' => __( 'Image', 'neuf_assoc' ),
'homepage' => __( 'Website', 'neuf_assoc' ),
'type' => __( 'Type', 'neuf_assoc' ),
);
return $custom_cols;
function neuf_associations_change_columns($cols)
{
$custom_cols = array(
'cb' => '<input type="checkbox" />',
'title' => __('Association', 'neuf_assoc'),
'image' => __('Image', 'neuf_assoc'),
'homepage' => __('Website', 'neuf_assoc'),
'type' => __('Type', 'neuf_assoc'),
);
return $custom_cols;
}
add_filter( "manage_association_posts_columns", "neuf_associations_change_columns" );
add_filter("manage_association_posts_columns", "neuf_associations_change_columns");
// Add values to the custom columns
function neuf_associations_custom_columns( $column, $post_id ) {
switch ( $column ) {
case "homepage":
$homepage = get_post_meta( $post_id, '_neuf_associations_homepage', true );
// default to permalink if not set
$homepage = $homepage ? $homepage : get_permalink( $post_id );
echo '<a href="'.$homepage.'" alt="'.$homepage.'" target="_blank">'.$homepage.'</a>';
break;
case "image":
$thumb = get_the_post_thumbnail( $post_id, array(100, 50) );
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), 'large' );
echo '<a href="'.$large_image_url[0].'" alt="'.$large_image_url[0].'">'.$thumb.'</a>';
break;
case "type":
echo get_post_meta( $post_id, '_neuf_associations_type', true );
break;
}
function neuf_associations_custom_columns($column, $post_id)
{
switch ($column) {
case "homepage":
$homepage = get_post_meta($post_id, '_neuf_associations_homepage', true);
// default to permalink if not set
$homepage = $homepage ? $homepage : get_permalink($post_id);
echo '<a href="' . $homepage . '" alt="' . $homepage . '" target="_blank">' . $homepage . '</a>';
break;
case "image":
$thumb = get_the_post_thumbnail($post_id, array(100, 50));
$large_image_url = wp_get_attachment_image_src(get_post_thumbnail_id($post_id), 'large');
echo '<a href="' . $large_image_url[0] . '" alt="' . $large_image_url[0] . '">' . $thumb . '</a>';
break;
case "type":
echo get_post_meta($post_id, '_neuf_associations_type', true);
break;
}
}
add_action( "manage_posts_custom_column" , "neuf_associations_custom_columns", 10, 2 );
add_action("manage_posts_custom_column", "neuf_associations_custom_columns", 10, 2);
// Make these columns sortable
function neuf_associations_sortable_columns( $cols ) {
$custom_cols = array(
'type' => 'type',
);
return array_merge($custom_cols, $cols);
function neuf_associations_sortable_columns($cols)
{
$custom_cols = array(
'type' => 'type',
);
return array_merge($custom_cols, $cols);
}
add_filter( "manage_edit-association_sortable_columns", "neuf_associations_sortable_columns" );
add_filter("manage_edit-association_sortable_columns", "neuf_associations_sortable_columns");
/* Add metaboxes (with styles) */
function add_associations_metaboxes() {
add_meta_box(
'neuf_associations_div',
__('Association details', 'neuf_assoc'),
'neuf_associations_div',
'association',
'side'
);
function add_associations_metaboxes()
{
add_meta_box(
'neuf_associations_div',
__('Association details', 'neuf_assoc'),
'neuf_associations_div',
'association',
'side'
);
}
/* Metabox with additional info. */
function neuf_associations_div(){
global $post;
function neuf_associations_div()
{
global $post;
/* Static types */
$association_types = array('Forening', 'Utvalg');
/* Static types */
$association_types = array('Forening', 'Utvalg');
$association_type = get_post_meta( $post->ID, '_neuf_associations_type', true );
$association_homepage = get_post_meta( $post->ID, '_neuf_associations_homepage', true );
?>
$association_type = get_post_meta($post->ID, '_neuf_associations_type', true);
$association_homepage = get_post_meta($post->ID, '_neuf_associations_homepage', true);
?>
<div class="misc-pub-section misc-pub-section-last">
<label for="_neuf_associations_homepage"><?php _e( 'Website', 'neuf_assoc' ); ?></label>
<label for="_neuf_associations_homepage"><?php _e('Website', 'neuf_assoc');?></label>
<input type="text" name="_neuf_associations_homepage" value="<?php echo $association_homepage ? $association_homepage : ""; ?>" />
<label for="_neuf_associations_type"><?php _e( 'Type', 'neuf_assoc' ); ?>:</label>
<label for="_neuf_associations_type"><?php _e('Type', 'neuf_assoc');?>:</label>
<select name="_neuf_associations_type">
<?php foreach ($association_types as $type) {
echo '<option value="'.$type.'"';
if($type== $association_type) {
echo ' selected="selected"';
}
echo '>'.$type.'</option>';
}
?>
<?php
foreach ($association_types as $type) {
echo '<option value="' . $type . '"';
if ($type == $association_type) {
echo ' selected="selected"';
}
echo '>' . $type . '</option>';
}
?>
</select><br />
<?php wp_nonce_field( 'neuf_associations_nonce', 'neuf_associations_nonce' ); ?>
<?php wp_nonce_field('neuf_associations_nonce', 'neuf_associations_nonce');?>
</div>
<?php
}
@@ -1,84 +1,89 @@
<?php
/* Setup the custom post type */
function neuf_associations_post_type() {
$labels = array(
'name' => __( 'Associations', 'neuf_assoc'),
'singular_name' => __( 'Association', 'neuf_assoc'),
'add_new' => __( 'Add New', 'neuf_assoc'),
'add_new_item' => __( 'Add New', 'neuf_assoc'),
'edit_item' => __( 'Edit Association', 'neuf_assoc'),
'new_item' => __( 'Add New Association', 'neuf_assoc'),
'view_item' => __( 'View Association', 'neuf_assoc'),
'search_items' => __( 'Search Associations', 'neuf_assoc'),
'not_found' => __( 'No associations found', 'neuf_assoc'),
'not_found_in_trash' => __( 'No associations found in trash', 'neuf_assoc')
);
register_post_type(
'association',
array(
'labels' => $labels,
'menu_position' => 5,
'public' => true,
'publicly_queryable' => true,
'query_var' => 'association',
'show_ui' => true,
'show_in_rest' => true,
'rest_base' => 'associations',
'capability_type' => 'post',
'supports' => array(
'title',
'editor',
'author',
'thumbnail',
'excerpt',
'comments',
'revisions',
'administrator',
'custom-fields'
),
'register_meta_box_cb' => 'add_associations_metaboxes',
)
);
function neuf_associations_post_type()
{
$labels = array(
'name' => __('Associations', 'neuf_assoc'),
'singular_name' => __('Association', 'neuf_assoc'),
'add_new' => __('Add New', 'neuf_assoc'),
'add_new_item' => __('Add New', 'neuf_assoc'),
'edit_item' => __('Edit Association', 'neuf_assoc'),
'new_item' => __('Add New Association', 'neuf_assoc'),
'view_item' => __('View Association', 'neuf_assoc'),
'search_items' => __('Search Associations', 'neuf_assoc'),
'not_found' => __('No associations found', 'neuf_assoc'),
'not_found_in_trash' => __('No associations found in trash', 'neuf_assoc'),
);
register_post_type(
'association',
array(
'labels' => $labels,
'menu_position' => 5,
'public' => true,
'publicly_queryable' => true,
'query_var' => 'association',
'show_ui' => true,
'show_in_rest' => true,
'rest_base' => 'associations',
'capability_type' => 'post',
'supports' => array(
'title',
'editor',
'author',
'thumbnail',
'excerpt',
'comments',
'revisions',
'administrator',
'custom-fields',
),
'register_meta_box_cb' => 'add_associations_metaboxes',
)
);
}
/* When the post is saved, saves our custom data */
function neuf_associations_save_postdata( $post_id ) {
// verify if this is an auto save routine.
function neuf_associations_save_postdata($post_id)
{
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !array_key_exists('neuf_associations_nonce', $_POST) || !wp_verify_nonce( $_POST['neuf_associations_nonce'], 'neuf_associations_nonce' ) )
return;
// Check permissions
if ( 'page' == $_POST['post_type'] )
{
if ( !current_user_can( 'edit_page', $post_id ) )
return;
if (!array_key_exists('neuf_associations_nonce', $_POST) || !wp_verify_nonce($_POST['neuf_associations_nonce'], 'neuf_associations_nonce')) {
return;
}
else
{
if ( !current_user_can( 'edit_post', $post_id ) )
// Check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id)) {
return;
}
} else {
if (!current_user_can('edit_post', $post_id)) {
return;
}
}
// OK, we're authenticated.
// Note esc_url prefixes url with protocol if not present
$meta_data['_neuf_associations_homepage'] = esc_url( $_POST['_neuf_associations_homepage'] );
$meta_data['_neuf_associations_homepage'] = esc_url($_POST['_neuf_associations_homepage']);
$meta_data['_neuf_associations_type'] = $_POST['_neuf_associations_type'];
// Update database with meta data
foreach($meta_data as $key => $value) {
foreach ($meta_data as $key => $value) {
update_post_meta($post_id, $key, $value);
}
}
/** API: Add our custom fields to the events endpoint */
add_filter('rest_prepare_association', 'neuf_associations_rest_custom_fields', 10, 3);
function neuf_associations_rest_custom_fields($response, $post, $request) {
function neuf_associations_rest_custom_fields($response, $post, $request)
{
$custom_field_data = get_post_custom($post->ID);
$field_map = array(
'_neuf_associations_type' => 'association_type',
+16 -16
View File
@@ -1,29 +1,29 @@
<?php
/*
Plugin Name: neuf-associations
Plugin URI: http://www.studentersamfundet.no
Description: Association custom post type
Version: 0.2
Author: EDB-web
Author URI: http://www.studentersamfundet.no
License: GPL v2 or later
Text Domain: neuf_assoc
Plugin Name: neuf-associations
Plugin URI: http://www.studentersamfundet.no
Description: Association custom post type
Version: 0.2
Author: EDB-web
Author URI: http://www.studentersamfundet.no
License: GPL v2 or later
Text Domain: neuf_assoc
*/
/* TODO (nikolark):
* - Pickup stuff from: http://codex.wordpress.org/Post_Types
*/
require_once( 'neuf-associations-post-type.php' );
require_once( 'neuf-associations-admin.php' );
require_once 'neuf-associations-post-type.php';
require_once 'neuf-associations-admin.php';
/* Register the translations */
add_action( 'init' , 'neuf_associations_i18n' , 0 );
add_action('init', 'neuf_associations_i18n', 0);
/* Register the post type */
add_action( 'init' , 'neuf_associations_post_type' , 0 );
add_action( 'save_post' , 'neuf_associations_save_postdata');
add_action('init', 'neuf_associations_post_type', 0);
add_action('save_post', 'neuf_associations_save_postdata');
//add_action( 'publish_post' , 'neuf_associations_save_postdata' );
function neuf_associations_i18n() {
load_plugin_textdomain( 'neuf_assoc', false, dirname( plugin_basename( __FILE__ ) ) .'/languages' );
function neuf_associations_i18n()
{
load_plugin_textdomain('neuf_assoc', false, dirname(plugin_basename(__FILE__)) . '/languages');
}
?>