WordPress.org

Plugin Directory

Changeset 1523456


Ignore:
Timestamp:
10/27/16 23:35:19 (10 months ago)
Author:
iambriansreed
Message:

Added the Local JSON feature which saves post type settings to files within your theme. The idea is similar to caching and both dramatically speeds up ACPT and allows for version control over your post type settings. Removed unused filters and added the save_json filter to determine where post type setting files are saved.

Location:
advanced-custom-post-types/trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • advanced-custom-post-types/trunk/acpt.php

    r1506712 r1523456  
    33Plugin Name: Advanced Custom Post Types 
    44Description: Customise WordPress with custom post types 
    5 Version: 0.2.0 
     5Version: 0.3.0 
    66Author: iambriansreed 
    77Author URI: http://iambrian.com/ 
  • advanced-custom-post-types/trunk/admin/class-post-type.php

    r1506712 r1523456  
    22 
    33namespace Advanced_Custom_Post_Types\Admin; 
     4 
     5use Advanced_Custom_Post_Types\Settings; 
    46 
    57class Post_Type { 
     
    79    private $fields; 
    810    private $dashicons; 
    9  
    10     function __construct( Fields $fields, Dashicons $dashicons ) { 
     11    private $settings; 
     12 
     13    function __construct( Settings $settings, Fields $fields, Dashicons $dashicons ) { 
    1114        $this->fields    = $fields; 
    1215        $this->dashicons = $dashicons; 
     16        $this->settings  = $settings; 
    1317    } 
    1418 
    1519    public function save( $post ) { 
    1620 
     21        if ( ! is_object( $post ) ) { 
     22            return; 
     23        } 
     24 
    1725        $post_data = $this->get_post_data( $post->ID ); 
    1826 
    19         if ( $post_data['error'] ) { 
    20  
    21             Notices::add( $post_data['error'], 'error', false ); 
    22             unset( $post_data['error'] ); 
    23         } 
    24  
    25         // $this->test( $post_data ); 
    26  
    27         wp_update_post( $post_data ); 
     27        if ( $post_data->errors ) { 
     28            Notices::add( $post_data->errors, 'error', false ); 
     29        } 
     30 
     31        wp_update_post( $post_data->post ); 
     32 
     33        $save_json = $this->save_json( $post_data ); 
     34 
     35        if ( $save_json->errors ) { 
     36 
     37            Notices::add( $save_json->errors, 'error', false ); 
     38        } 
     39    } 
     40 
     41    public function save_json( $post_data ) { 
     42 
     43        // vars 
     44        $path      = $this->settings->get( 'save_json' ); 
     45        $file_name = $post_data->post_type . '.json'; 
     46        $output    = (object) array( 'errors' => false ); 
     47 
     48        // remove trailing slash 
     49        $path = untrailingslashit( $path ); 
     50 
     51        // bail early if dir does not exist 
     52        if ( ! is_writable( $path ) ) { 
     53            $output->errors = "The ACPT JSON save path '$path' is not writable."; 
     54 
     55            return $output; 
     56        } 
     57 
     58        // write file 
     59        $f = fopen( "{$path}/{$file_name}", 'w' ); 
     60        fwrite( $f, $this->json_encode( $post_data->data ) ); 
     61        fclose( $f ); 
     62 
     63        // return 
     64        return $output; 
     65 
    2866    } 
    2967 
     
    3169     * @param $post_id 
    3270     * 
    33      * @return array 
     71     * @return object 
    3472     * @throws \Exception 
    3573     */ 
     
    198236        $content->saved = time(); 
    199237 
    200         return array( 
    201             'ID'                => $post_id, 
    202             'post_title'        => $args['plural_name'], 
    203             'post_name'         => 'acpt_post_type_' . $post_type, 
    204             'post_status'       => $content->error ? 'draft' : 'publish', 
    205             'post_content'      => json_encode( $content ), 
    206             'post_content_data' => $content, 
    207             'error'             => $content->error 
    208         ); 
     238        return (object) array( 
     239            'errors'    => $content->error, 
     240            'post_type' => $post_type, 
     241            'data'      => $content, 
     242            'post'      => array( 
     243                'ID'                => $post_id, 
     244                'post_type'         => $post_type, 
     245                'post_title'        => $args['plural_name'], 
     246                'post_type'         => ACPT_POST_TYPE, 
     247                'post_name'         => 'acpt_post_type_' . $post_type, 
     248                'post_status'       => $content->error ? 'draft' : 'publish', 
     249                'post_content'      => $this->json_encode( $content ), 
     250                'post_content_data' => $content 
     251            ) 
     252        ); 
     253    } 
     254 
     255    function json_encode( $data ) { 
     256 
     257        // create json string 
     258        if ( version_compare( PHP_VERSION, '5.4.0', '>=' ) ) { 
     259            // PHP at least 5.4 
     260            return json_encode( $data, JSON_PRETTY_PRINT ); 
     261 
     262        } else { 
     263            // PHP less than 5.4 
     264            return json_encode( $data ); 
     265        } 
    209266    } 
    210267 
  • advanced-custom-post-types/trunk/class-load.php

    r1506712 r1523456  
    1616 
    1717    function init() { 
     18         
     19        $settings = new Settings(); 
    1820 
    19         $post_types = new Post_Types(); 
     21        $post_types = new Post_Types( $settings ); 
    2022 
    2123        $post_types->register(); 
     
    2325        if ( is_admin() ) { 
    2426 
    25             $settings     = new Settings(); 
    26             $dashicons    = new Dashicons(); 
    27             $fields       = new Fields( $settings, $dashicons ); 
    28             $post_type    = new Post_Type( $fields, $dashicons ); 
     27            $dashicons = new Dashicons(); 
     28            $fields    = new Fields( $settings, $dashicons ); 
     29            $post_type = new Post_Type( $settings, $fields, $dashicons ); 
    2930 
    3031            new Admin\Load( $settings, $post_types, $fields, $post_type ); 
  • advanced-custom-post-types/trunk/class-post-types.php

    r1506712 r1523456  
    44 
    55class Post_Types { 
     6 
     7    private $json_path = ''; 
     8 
     9    function __construct( Settings $settings ) { 
     10 
     11        $this->json_path = $settings->get( 'save_json' ); 
     12    } 
    613 
    714    private function get_saved_data() { 
     
    2532 
    2633        foreach ( $posts as $post ) { 
    27             $post_types[] = json_decode( $post->post_content, true ); 
     34 
     35            $post_data = json_decode( $post->post_content, true ); 
     36 
     37            $post_types[ $post_data['post_type'] ] = $post_data; 
    2838        } 
    2939 
    30         $post_types = apply_filters( 'acpt/post_types', $post_types ); 
     40        if ( $this->json_path ) { 
     41 
     42            $files = scandir( $this->json_path ); 
     43 
     44            foreach ( $files as $file ) { 
     45 
     46                if ( substr( $file, - 5 ) === '.json' ) { 
     47 
     48                    $post_type_json = file_get_contents( "$this->json_path/$file" ); 
     49 
     50                    $post_data = json_decode( $post_type_json, true ); 
     51 
     52                    $post_types[ $post_data['post_type'] ] = $post_data; 
     53                } 
     54            } 
     55        } 
    3156 
    3257        return $post_types; 
     58 
     59    } 
     60 
     61    public function valid_post_type( $post_type ) { 
     62        return is_string( $post_type ) && strlen( $post_type ) > 0 && strlen( $post_type ) < 21; 
    3363    } 
    3464 
     
    4373        foreach ( $post_types as $post_type ) { 
    4474 
    45             if ( ! isset( $post_type['post_type'] ) || ! isset( $post_type['args'] ) ) { 
     75            if ( ! isset( $post_type['post_type'] ) || ! $this->valid_post_type( $post_type['post_type'] ) || ! isset( $post_type['args'] ) ) { 
    4676                continue; 
    4777            } 
  • advanced-custom-post-types/trunk/class-settings.php

    r1506712 r1523456  
    1010 
    1111        $this->defaults = array( 
    12             'show_admin'   => true, 
    13             'capability'   => 'manage_options' 
     12            'show_admin' => true, 
     13            'capability' => 'manage_options', 
     14            'save_json'  => null 
    1415        ); 
    1516    } 
  • advanced-custom-post-types/trunk/readme.txt

    r1506712 r1523456  
    1717**acpt/settings/show_admin** - Determines whether or not the admin menu is shown. 
    1818 
    19 **acpt/settings/admin_fields** - Determines which field groups may appear in the post type editor. 
    20  
    2119**acpt/settings/capability** - Determines capability is needed to manage custom post types. 
    2220 
    23 **acpt/post_types** - Alter the post types created 
     21**acpt/settings/save_json** - The path to save post types locally.  
    2422 
    2523== Actions == 
     
    6058= 0.2.0 = 
    6159* Massive refactoring and cleanup, added export 
     60 
     61= 0.3.0 = 
     62* Added the Local JSON feature which saves post type settings to files within your theme. The idea is similar to caching and both dramatically speeds up ACPT and allows for version control over your post type settings. Removed unused filters and added the save_json filter to determine where post type setting files are saved. 
     63 
     64  
Note: See TracChangeset for help on using the changeset viewer.