WordPress plugin custom table for custom post

The idea is to create a plugin that will add a custom post but all the metadata of this custom post should be in a custom table. Invisible for the user and customizable. I’ve tried to follow this.

We create a new custom type

// Register Custom Post Type
function custom_darkink_film() {
 
 $labels = array(
 'name'                  => _x( 'Films', 'Post Type General Name', 'darkink_film_domain' ),
 'singular_name'         => _x( 'Film', 'Post Type Singular Name', 'darkink_film_domain' ),
 'menu_name'             => __( 'Films', 'darkink_film_domain' ),
 'name_admin_bar'        => __( 'Films', 'darkink_film_domain' ),
 'archives'              => __( 'Film Archives', 'darkink_film_domain' ),
 'attributes'            => __( 'Film Attributes', 'darkink_film_domain' ),
 'parent_item_colon'     => __( 'Parent film:', 'darkink_film_domain' ),
 'all_items'             => __( 'All films', 'darkink_film_domain' ),
 'add_new_item'          => __( 'Add New film', 'darkink_film_domain' ),
 'add_new'               => __( 'Add Film', 'darkink_film_domain' ),
 'new_item'              => __( 'New Film', 'darkink_film_domain' ),
 'edit_item'             => __( 'Edit Film', 'darkink_film_domain' ),
 'update_item'           => __( 'Update Film', 'darkink_film_domain' ),
 'view_item'             => __( 'View Film', 'darkink_film_domain' ),
 'view_items'            => __( 'View Films', 'darkink_film_domain' ),
 'search_items'          => __( 'Search Film', 'darkink_film_domain' ),
 'not_found'             => __( 'Film not found', 'darkink_film_domain' ),
 'not_found_in_trash'    => __( 'Film not found in Trash', 'darkink_film_domain' ),
 'featured_image'        => __( 'Featured Image', 'darkink_film_domain' ),
 'set_featured_image'    => __( 'Set featured image', 'darkink_film_domain' ),
 'remove_featured_image' => __( 'Remove featured image', 'darkink_film_domain' ),
 'use_featured_image'    => __( 'Use as featured image', 'darkink_film_domain' ),
 'insert_into_item'      => __( 'Insert into film', 'darkink_film_domain' ),
 'uploaded_to_this_item' => __( 'Uploaded to this film', 'darkink_film_domain' ),
 'items_list'            => __( 'Films list', 'darkink_film_domain' ),
 'items_list_navigation' => __( 'Films list navigation', 'darkink_film_domain' ),
 'filter_items_list'     => __( 'Filter films list', 'darkink_film_domain' ),
 );
 
 $args = array(
 'label'                 => __( 'Film', 'darkink_film_domain' ),
 'description'           => __( 'a shot film', 'darkink_film_domain' ),
 'labels'                => $labels,
 'supports'              => array( 'title' ),
 'taxonomies'            => array( 'category', 'post_tag' ),
 'hierarchical'          => false,
 'public'                => true,
 'show_ui'               => true,
 'show_in_menu'          => true,
 'menu_position'         => 5,
 'show_in_admin_bar'     => true,
 'show_in_nav_menus'     => true,
 'can_export'            => true,
 'has_archive'           => true,
 'exclude_from_search'   => false,
 'publicly_queryable'    => true,
 'capability_type'       => 'page',
 );

 register_post_type( 'post_darkink_film', $args );
}

add_action( 'init', 'custom_darkink_film', 0 );

We create the database

$darkink_test_db_version = '1.2';

// Create the Custom Table
function database_darkink_film(){
	global $wpdb;
	global $darkink_test_db_version;
	
	$charset_collate = $wpdb->get_charset_collate();
	$table_name = $wpdb->prefix . "darkink_film";

	$sql = "CREATE TABLE $table_name (
		post_id bigint(20) NOT NULL,
		director varchar(255) NOT NULL,
		writer varchar(255) NOT NULL,
		year year(4) NOT NULL,
		PRIMARY KEY  (post_id)
	  ) $charset_collate;";
	  
	require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
	dbDelta( $sql );

	add_option( 'darkink_test_db_version', $darkink_test_db_version );
}

register_activation_hook( __FILE__, 'database_darkink_film' );

Now we add all the handling for the custom table

function darkink_film_meta_insert_post( $post_id, $post, $udpate ) {
	global $wpdb;
	
	if ( get_post_type( $post ) == 'post_darkink_film' && 
		 $post->post_status != 'trash' && 
		 !$udpate &&
		 $wpdb->get_var( $wpdb->prepare("SELECT post_id FROM " . $wpdb->prefix . "darkink_film WHERE post_id = %d", $post_id )) == null
    ) {
        $wpdb->insert( $wpdb->prefix . "darkink_film", array( 'post_id' => $post_id ) );
    }
}

function darkink_film_meta_delete_post( $post_id ) {
	global $wpdb;

    if ( get_post_type( $post_id ) == 'post_darkink_film' ) {        
        $wpdb->query( $wpdb->prepare("DELETE FROM " . $wpdb->prefix . "darkink_film WHERE post_id = %d", $post_id ) );
    }
}

function darkink_film_meta_update( $check, $post_id, $meta_key, $meta_value ) {
	global $wpdb;

    if ( get_post_type( $post_id) == 'post_darkink_film' && in_array( $meta_key, array( 'director', 'writer', 'year' ) ) ) {   
        return $wpdb->update(
            $wpdb->prefix . "darkink_film",
            array( $meta_key => maybe_serialize( $meta_value ) ),
            array( 'post_id' => $post_id)
        );
    } else {
        return $check;
    }
}

function darkink_film_meta_delete( $check, $object_id, $meta_key ) {
	global $wpdb;

    if ( get_post_type( $post ) == 'post_darkink_film' && in_array( $meta_key, array( 'director', 'writer', 'year' ) ) ) {    
        return $wpdb->update(
            $wpdb->prefix . "darkink_film",
            array( $meta_key => null ),
            array( 'post_id' => $object_id )
        );
    } else {
        return $check;
    }
}

function darkink_film_meta_get( $check, $post_id, $meta_key ) {
    if ( get_post_type( $post_id) == 'post_darkink_film' && in_array( $meta_key, array( 'director', 'writer', 'year' ) ) ) {
        $result = $wpdb->get_var( $wpdb->prepare("SELECT $meta_key FROM " . $wpdb->prefix . "darkink_film WHERE post_id = %d", $post_id) );
        return maybe_unserialize( $result );
    } else {
        return $check;
    }
}

add_action( 'wp_insert_post', 'darkink_film_meta_insert_post', 10, 3 );
add_action( 'delete_post', 'darkink_film_meta_delete_post', 10 );
add_filter( 'add_post_metadata', 'darkink_film_meta_update', 0, 4 );
add_filter( 'update_post_metadata', 'darkink_film_meta_update', 0, 4 );
add_filter( 'delete_post_metadata', 'darkink_film_meta_delete', 0, 3 );
add_filter( 'get_post_metadata', 'darkink_film_meta_get', 0, 3 );

We need a lot of code to make it work….

Octopi behind proxy and credentials

I wanted to have an octopi behind a simple proxy.

So add the proxy to apache

<IfModule mod_proxy.c>
  ProxyRequests off
  ProxyVia On

  ProxyPass / http://octoprint.darkink.internal/
  ProxyPassReverse / http://octopi.darkink.internal/

  <Location />
    ProxyPassReverse /
  </Location> 
</IfModule>

then add some securtiy, creating a new CA following this and this.

openssl genrsa -des3 -out rootCA.key 2048
openssl req -x509 -new -nodes -key rootCA.key -sha256 -days 3650 -out rootCA.pem

openssl genrsa -out steven.key 2048
openssl req -new -key steven.key -out steven.csr
openssl x509 -req -in steven.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out steven.crt -days 365 -sha256

openssl pkcs12 -export -in server.crt -inkey server.key -out server.p12

adding to the proxy

SSLVerifyClient require
SSLVerifyDepth 10
SSLCACertificateFile /srv/www/clients/client1/web26/ssl/auth/rootCA.pem
SSLCACertificatePath  /srv/www/clients/client1/web26/ssl/auth

We then need to add revocation here and here:

openssl ca -config rootCA.conf -gencrl -out rootCA.crl

We add this to the proxy

SSLCARevocationFile /srv/www/clients/client1/web26/ssl/auth/rootCA.crl
SSLCARevocationCheck chain

then add the certificate to the browser and try it !

if we want to revoke it we must do

openssl ca -config rootCA.conf -revoke certs/test.crt
openssl ca -config rootCA.conf -gencrl -out rootCA.crl

openssl crl -in intermediate/crl/intermediate.crl.pem -noout -text

Install a custom web music player

This is a test blog

The choices

Sonaretzh

Installation

with docker

Test

some test

Another

with another thing

Last

More

Upgrade from Debian 8 to Debian 9

A little link to the Tuto : https://linuxconfig.org/how-to-upgrade-debian-8-jessie-to-debian-9-stretch

Darkink

Welcolm on the Darkink Site