Plugin Directory

Changeset 2865958


Ignore:
Timestamp:
02/15/2023 07:49:09 PM (3 years ago)
Author:
andrija
Message:

v1.3.2

Location:
jane-menu/trunk/includes
Files:
1 added
8 edited
1 moved

Legend:

Unmodified
Added
Removed
  • jane-menu/trunk/includes/ajax.php

    r2854692 r2865958  
    1414 *
    1515 * @uses    IHeartJane\WebMenu\Helpers\get_page_relative_path() Helpers\get_page_relative_path()
     16 *
     17 * @since   1.3.0
    1618 *
    1719 * @return  void    Uses wp_send_json_success() to return a string or wp_send_json_error() to return an array
     
    4547 * Returns the full path for a specific Page ID
    4648 *
     49 * @since   1.3.0
     50 *
    4751 * @return  void    Uses wp_send_json_success() to return a string or wp_send_json_error() to return an array
    4852 */
  • jane-menu/trunk/includes/core.php

    r2854692 r2865958  
    1919 * @see      https://developer.wordpress.org/reference/functions/add_option/    Codex, function: add_option
    2020 *
     21 * @since    1.0.0
     22 * @since    1.3.0  Cleaned
     23 *
    2124 * @return   void
    2225 */
     
    3437 * @uses     IHeartJane\WebMenu\Constants\PLUGIN_ID                     Constants\PLUGIN_ID
    3538 *
     39 * @since    1.0.0
     40 * @since    1.3.0  Not actually used
     41 *
    3642 * @return   void
    3743 */
     
    4753 * @uses     IHeartJane\WebMenu\Constants\OPTION_SITEMAP_ENABLED_NAME   Constants\OPTION_SITEMAP_ENABLED_NAME
    4854 * @uses     IHeartJane\WebMenu\DB\drop_config_table()                  DB\drop_config_table()
     55 *
     56 * @since    1.3.0
    4957 *
    5058 * @return   void
     
    6775 *
    6876 * @see      https://developer.wordpress.org/reference/hooks/admin_menu/    Codex, action: admin_menu
     77 *
     78 * @since    1.0.0
    6979 *
    7080 * @return   void
     
    106116 * @uses     IHeartJane\WebMenu\Constants\TEMPLATES_DIR                 Constants\TEMPLATES_DIR
    107117 *
     118 * @since    1.0.0
     119 *
    108120 * @return   void
    109121 */
     
    128140 * @uses     IHeartJane\WebMenu\Helpers\enqueue_CSS() Helpers\enqueue_CSS()
    129141 * @uses     IHeartJane\WebMenu\Helpers\enqueue_JS()  Helpers\enqueue_JS()
     142 *
     143 * @since    1.3.0
    130144 *
    131145 * @return   void
  • jane-menu/trunk/includes/core__autoloader.php

    r2854692 r2865958  
    1414 * @param    string    $dir             Full path from where the namespaces will have their relative folders
    1515 * @param    array     $namespaces      Array of namespaces with FQCN base as the key and relative path as the value
     16 *
     17 * @since    1.3.0
    1618 *
    1719 * @return   void
  • jane-menu/trunk/includes/db.php

    r2854692 r2865958  
    1717 *
    1818 * @see      https://developer.wordpress.org/reference/hooks/admin_init/    Codex, action: admin_init
     19 *
     20 * @since    1.3.0
    1921 *
    2022 * @return   void
     
    4345 *
    4446 * @uses    IHeartJane\WebMenu\Constants\DB_TABLE_NAME Constants\DB_TABLE_NAME
     47 *
     48 * @since   1.0.0
     49 * @since   1.3.0   New structure
    4550 *
    4651 * @return  string[]  Array of strings containing the changes, format table_name.column_name => description of the performed action
     
    110115 * @uses    IHeartJane\WebMenu\Constants\DB_TABLE_NAME Constants\DB_TABLE_NAME
    111116 *
     117 * @since   1.1.0
     118 *
    112119 * @return  void
    113120 */
  • jane-menu/trunk/includes/helpers.php

    r2854692 r2865958  
    1212 *
    1313 * @param string Text that should be escaped
     14 *
     15 * @since 1.0.0
    1416 *
    1517 * @return string Escaped text
     
    2830 * @param    string   $replace    Replace string
    2931 * @param    string   $subject    Subject string, haystack
     32 *
     33 * @since    1.0.0
    3034 *
    3135 * @return   string   Altered string, in case the search string is not found - original string
     
    5155 *
    5256 * @param    int    $page_id    Page ID
     57 *
     58 * @since 1.3.0
    5359 *
    5460 * @return   string|false     Relative path or false if page does not exist
     
    7985 * @param    string   $text      Any descriptive text that helps you find your log
    8086 * @param    bool     $delete    Append or overwrite the file, default: false (don't delete)
     87 *
     88 * @since 1.3.0
    8189 *
    8290 * @return   void
     
    110118 * @param    bool      $is_footer     should the script get loaded in the footer, default: true
    111119 *
     120 * @since 1.3.0
     121 *
    112122 * @return   void
    113123 */
     
    132142 * @param    string[]  $dependency    list of dependency handles, default: empty array
    133143 *
     144 * @since 1.3.0
     145 *
    134146 * @return   void
    135147 */
  • jane-menu/trunk/includes/request.php

    r2854692 r2865958  
    99use IHeartJane\WebMenu\DB;
    1010use IHeartJane\WebMenu\StoreConfigs;
     11use IHeartJane\WebMenu\Helpers;
    1112
    1213/**
    13  * Creates the global variable with current Store Config ID
     14 * Creates the global variable with current Store Config data
    1415 *
    1516 * Allows us to perform the database check once and reuse the results in several places
    1617 *
    17  * @uses     IHeartJane\WebMenu\Constants\PLUGIN_ID                                 Constants\PLUGIN_ID
    1818 * @uses     \IHeartJane\WebMenu\StoreConfigs\Current_Store_Config::get_instance()  StoreConfigs\Current_Store_Config::get_instance()
    1919 *
    2020 * @see      https://developer.wordpress.org/reference/hooks/init/                  Codex, action: init
    2121 *
     22 * @since    1.3.0
     23 *
    2224 * @return   void
    2325 */
    2426function initial_check(){
    25 
     27   
    2628    global $jane__current_config;
    27 
     29   
    2830    $jane__current_config = StoreConfigs\Current_Store_Config::get_instance();
    29    
    3031}
    3132add_action( 'init', __NAMESPACE__ . '\\' . 'initial_check' );
    3233
    3334/**
    34  * Intercepts the paring of the request
     35 * Alter the WordPress request results
    3536 *
    36  * If the current request is for a current config, don't allow it to become 404
    37  *
    38  * @uses     \IHeartJane\WebMenu\StoreConfigs\Current_Store_Config::get_id()    StoreConfigs\Current_Store_Config::get_id()
    39  *
    40  * @see      https://developer.wordpress.org/reference/hooks/do_parse_request/  Codex, filter: do_parse_request
    41  *
    42  * @param    bool           $should_parse       Whether or not to parse the request. Default true
    43  * @param    \WP            $wp                 Current WordPress environment instance
    44  * @param    array|string   $extra_query_vars   Extra passed query variables
    45  *
    46  * @return   bool     If we're handling the Store Config request returns false so WordPress wouldn't handle it the usual way
    47  */
    48 function intercept_parse_request( $should_parse, $wp, $extra_query_vars ){
    49 
    50     global $wp_query, $jane__current_config;
    51 
    52     if( $jane__current_config->get_id() ){
    53        
    54         status_header( 200 );
    55         $wp_query->is_page      = true;
    56         $wp_query->is_singular  = true;
    57         $wp_query->is_404       = false;
    58        
    59         return false;
    60     }
    61    
    62     return $should_parse;
    63 }
    64 add_filter( 'do_parse_request', __NAMESPACE__ . '\\' . 'intercept_parse_request', 10, 3 );
    65 
    66 /**
    67  * Checks if the header request status code should get overriden
    68  *
    69  * This function is not required because of {@see \IHeartJane\WebMenu\Request\intercept_parse_request() Request\intercept_parse_request()}
    70  * but is left active intentionally in case the {@see \IHeartJane\WebMenu\Request\intercept_parse_request() Request\intercept_parse_request()} doesn't get triggered
    71  *
    72  * @uses     \IHeartJane\WebMenu\StoreConfigs\Current_Store_Config::get_id()    StoreConfigs\Current_Store_Config::get_id()
     37 * When a fictive subpage of a Jane Menu is requested, we have to simulate that the Jane Menu page was called.
    7338 *
    7439 * @see      https://developer.wordpress.org/reference/hooks/parse_request/     Codex, action: parse_request
    7540 *
    76  * @param    \WP            $wp                 Current WordPress environment instance
     41 * @param    \WP        $wp    Current WordPress environment instance (passed by reference)
    7742 *
    78  * @return   void
     43 * @since    1.3.0
     44 *
     45 * @return   void       The $wp is passed by reference
    7946 */
    80 function do_after_parse_request( $wp ){
    81 
    82     global $wp_query, $jane__current_config;
    83 
    84     if( $jane__current_config->get_id() ){
    85        
    86         status_header( 200 );
    87        
    88         $wp_query->is_page      = true;
    89         $wp_query->is_singular  = true;
    90         $wp_query->is_404       = false;
    91        
    92     }
    93 }
    94 add_action( 'parse_request', __NAMESPACE__ . '\\' . 'do_after_parse_request' );
    95 
    96 /**
    97  * Intercepts handling of 404 header status codes
    98  *
    99  * This function is not required because of {@see \IHeartJane\WebMenu\Request\intercept_parse_request() Request\intercept_parse_request()}
    100  * but is left active intentionally in case the {@see \IHeartJane\WebMenu\Request\intercept_parse_request() Request\intercept_parse_request()} doesn't get triggered
    101  *
    102  * @uses     \IHeartJane\WebMenu\StoreConfigs\Current_Store_Config::get_id()    StoreConfigs\Current_Store_Config::get_id()
    103  *
    104  * @see      https://developer.wordpress.org/reference/hooks/pre_handle_404/    Codex, filter: pre_handle_404
    105  *
    106  * @param    bool           $preempt        Whether to short-circuit default header status handling. Default false
    107  * @param    \WP_Query      $wp_query       WordPress Query object
    108  *
    109  * @return   bool
    110  */
    111 function intercept_404( $preempt, $wp_query ){
     47function after_parse_request( $wp ){
    11248   
    11349    global $jane__current_config;
    11450   
     51    // Helpers\debug_log( $wp, "Request-after_parse_request-wp" );
     52   
    11553    if( $jane__current_config->get_id() ){
    11654       
    117         $preempt = true;
     55        $query_vars = [
     56            "page" => null,
     57            "pagename" => $jane__current_config->get_config()->store_path,
     58        ];
     59       
     60        $wp->query_vars = $query_vars;
     61       
     62        $wp->matched_rule  = '(.?.+?)(?:/([0-9]+))?/?$';
     63        $wp->matched_query = urlencode( $jane__current_config->get_config()->store_path ) . '&page=';
     64       
    11865    }
    11966   
    120     return $preempt;
     67    // Helpers\debug_log( $wp, "Request-after_parse_request-wp" );
     68   
    12169}
    122 add_filter( 'pre_handle_404', __NAMESPACE__ . '\\' . 'intercept_404', 10, 2 );
    123 
    124 // if the config id exists, replace the template with our custom template
     70add_action( 'parse_request', __NAMESPACE__ . '\\' . 'after_parse_request', 10, 1 );
    12571
    12672/**
    127  * Replaces the default template with our custom template
     73 * Alter the page content
    12874 *
    129  * @uses     \IHeartJane\WebMenu\StoreConfigs\Current_Store_Config::get_template_path() StoreConfigs\Current_Store_Config::get_template_path()
     75 * It will replace the shortcode if it exists, otherwise it will append our code at the end of the content.
    13076 *
    131  * @see      https://developer.wordpress.org/reference/hooks/template_include/          Codex, filter: template_include
     77 * @see      https://developer.wordpress.org/reference/hooks/the_content/     Codex, filter: the_content
    13278 *
    133  * @param    string     $template   The path of the template to include
     79 * @param    string     $content   The original content of the page
    13480 *
    135  * @return   string
     81 * @since    1.3.0
     82 * @since    1.3.2      The hook priority altered to fire late (as late as possible)
     83 *
     84 * @return   string     Altered content
    13685 */
    137 function replace_template( $template ){
     86function inject_in_body( $content ) {
    13887   
    13988    global $jane__current_config;
    14089   
    141     if( $jane__current_config->get_template_path() ){
     90    if( is_singular() && in_the_loop() && is_main_query() && ! empty( $jane__current_config->get_template_html_body() ) ){
    14291       
    143         $template = $jane__current_config->get_template_path();
     92        if ( strpos( $content, '[jane_menu_shortcode]' ) !== false ){
     93           
     94            $content = str_replace( '[jane_menu_shortcode]', $jane__current_config->get_template_html_body(), $content );;
     95           
     96        } else {
     97           
     98            $content .= $jane__current_config->get_template_html_body();
     99        }
    144100    }
    145101   
    146     return $template;
     102    return $content;
    147103}
    148 add_filter( 'template_include', __NAMESPACE__ . '\\' . 'replace_template' );
     104add_filter( 'the_content', __NAMESPACE__ . '\\' . 'inject_in_body', PHP_INT_MAX );
     105
     106/**
     107 * Append our code in the <head> tag
     108 *
     109 * @see      https://developer.wordpress.org/reference/hooks/wp_head/     Codex, action: wp_head
     110 *
     111 * @since    1.3.0
     112 *
     113 * @return   void       The code is echoed
     114 */
     115function inject_in_head(){
     116   
     117    global $jane__current_config;
     118   
     119    if ( ! empty( $jane__current_config->get_template_html_head() ) ){
     120       
     121        echo $jane__current_config->get_template_html_head();
     122    }
     123}
     124add_action( 'wp_head', __NAMESPACE__ . '\\' . 'inject_in_head' );
     125
     126
     127
     128
     129
     130
  • jane-menu/trunk/includes/sitemap.php

    r2865957 r2865958  
    11<?php
    22/**
    3  * Robots.txt related functions
     3 * Robots and sitemaps related functions
    44 *
    55 */
    66
    7 namespace IHeartJane\WebMenu\Robots;
     7namespace IHeartJane\WebMenu\Sitemap;
    88
    99use IHeartJane\WebMenu\Constants;
    1010use IHeartJane\WebMenu\StoreConfigs;
     11use IHeartJane\WebMenu\Helpers;
    1112
    1213/**
    1314 * Adds all Store Configs sitemap URLs to robots.txt
    1415 *
    15  * @uses     IHeartJane\WebMenu\Constants\OPTION_SITEMAP_ENABLED_NAME   Constants\OPTION_SITEMAP_ENABLED_NAME
    16  * @uses     IHeartJane\WebMenu\StoreConfigs\get_all_configs()          StoreConfigs\get_all_configs()
     16 * @uses     IHeartJane\WebMenu\Constants\OPTION_SITEMAP_ENABLED_NAME       Constants\OPTION_SITEMAP_ENABLED_NAME
     17 * @uses     IHeartJane\WebMenu\Constants\SITEMAP_PATH                      Constants\SITEMAP_PATH
     18 * @uses     IHeartJane\WebMenu\Constants\SITEMAP_URL                       Constants\SITEMAP_URL
    1719 *
    18  * @see      https://developer.wordpress.org/reference/hooks/robots_txt/ Codex, filter: robots_txt
     20 * @see      https://developer.wordpress.org/reference/hooks/robots_txt/    Codex, filter: robots_txt
    1921 *
    2022 * @param    string    $output    The robots.txt output
    2123 * @param    bool      $public    Whether the site is considered "public"
     24 *
     25 * @since    1.0.0
     26 * @since    1.3.2     Only the local sitemap index is appended
    2227 *
    2328 * @return   string    Appended URLs at the end of the robots.txt file content
     
    2530function append_to_robots_txt( $output, $public ){
    2631   
    27     if( get_option( Constants\OPTION_SITEMAP_ENABLED_NAME ) ){
     32    if( get_option( Constants\OPTION_SITEMAP_ENABLED_NAME ) && file_exists( Constants\SITEMAP_PATH ) ){
    2833       
    29         $store_configs = StoreConfigs\get_all_configs();
    30 
    31         foreach( $store_configs as $store_config ){
    32            
    33             // $sitemap_url = site_url( "/" . $store_config->store_path . "/" . Constants\SITEMAP_FILE_NAME_FULL );
    34             $sitemap_url = $store_config->sitemap_url;
    35            
    36             $output .= sprintf( 'Sitemap: %s', $sitemap_url ) . PHP_EOL;
    37         }
     34        $output .= sprintf( 'Sitemap: %s', Constants\SITEMAP_URL ) . PHP_EOL;
    3835    }
    39 
     36   
    4037    return $output;
    4138}
    4239add_filter( 'robots_txt', __NAMESPACE__ . '\\' . 'append_to_robots_txt', 99, 2 );
     40
     41/**
     42 * Creates or updates the sitemap index file
     43 *
     44 * The sitemap index file lists all the remote sitemap URLs from existing Store Configs
     45 *
     46 * @todo    Needs to get triggered when a Store Config is deleted too
     47 *
     48 * @uses     IHeartJane\WebMenu\Constants\OPTION_SITEMAP_ENABLED_NAME   Constants\OPTION_SITEMAP_ENABLED_NAME
     49 * @uses     IHeartJane\WebMenu\Constants\SITEMAP_DIR                   Constants\SITEMAP_DIR
     50 * @uses     IHeartJane\WebMenu\Constants\SITEMAP_PATH                  Constants\SITEMAP_PATH
     51 * @uses     IHeartJane\WebMenu\StoreConfigs\get_all_configs()          StoreConfigs\get_all_configs()
     52 *
     53 * @see      https://developer.wordpress.org/reference/functions/wp_upload_dir/ Codex, function: wp_upload_dir
     54 * @see      https://developer.wordpress.org/reference/functions/wp_mkdir_p/    Codex, function: wp_mkdir_p
     55 *
     56 * @since    1.3.2
     57 *
     58 * @return   bool    True on success, otherwise false
     59 */
     60function update_remote_sitemap(){
     61   
     62   
     63    // empty and remove the existing folder
     64   
     65    if( is_dir( Constants\SITEMAP_DIR ) ){
     66       
     67        $files = array_diff( scandir( Constants\SITEMAP_DIR ), array( '.', '..' ) );
     68
     69        foreach( $files as $file ){
     70           
     71            if( ! is_dir( Constants\SITEMAP_DIR . $file ) ){
     72               
     73                unlink( Constants\SITEMAP_DIR . $file );
     74            }
     75           
     76        }
     77       
     78        rmdir( Constants\SITEMAP_DIR );
     79    }
     80   
     81   
     82    // if the option is turned on, proceed
     83   
     84    if( ! get_option( Constants\OPTION_SITEMAP_ENABLED_NAME ) ){
     85       
     86        return false;
     87    }
     88   
     89   
     90    // if we have anything to write, proceed
     91   
     92    $store_configs = StoreConfigs\get_all_configs();
     93   
     94    if( empty( $store_configs ) ){
     95        return false;
     96    }
     97   
     98   
     99    // attempt to create the folder
     100   
     101    $directory_exists = wp_mkdir_p( Constants\SITEMAP_DIR );
     102   
     103    if( ! $directory_exists ){
     104       
     105        return false;
     106    }
     107   
     108   
     109    // generate the xml content
     110   
     111    $output = [
     112        '<?xml version="1.0" encoding="UTF-8"?>',
     113        '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
     114    ];
     115   
     116    foreach( $store_configs as $store_config ){
     117       
     118        $sitemap_url = $store_config->sitemap_url;
     119       
     120        $output[] = '  <sitemap>';
     121        $output[] = '    <loc>' . $store_config->sitemap_url . '</loc>';
     122        $output[] = '  </sitemap>';
     123    }
     124   
     125    $output[] = '</sitemapindex>';
     126   
     127    $xml = implode( PHP_EOL, $output );
     128   
     129    // Helpers\debug_log( $xml, "sitemap-update_remote_sitemap-xml" );
     130   
     131   
     132    $success = file_put_contents( Constants\SITEMAP_PATH, $xml );
     133   
     134    return $success ? true : false;
     135}
  • jane-menu/trunk/includes/store-configs.php

    r2854692 r2865958  
    1616 *
    1717 * @param   array       $args     Array of Store Config properties, if ID is set, it will get updated, otherwise inserted
     18 *
     19 * @since   1.0.0
    1820 *
    1921 * @return  int|\WP_Error|false   The Store Config ID if it's inserted or updated, \WP_Error if initial validation fails, or false on error
     
    9395 * @param   int[]   $ids    Array of Store Config IDs
    9496 *
     97 * @since   1.0.0
     98 *
    9599 * @return  void    The query simply deletes the rows of found IDs, if any
    96100 */
     
    114118 * @param   int   $config_id     Store Config ID
    115119 *
     120 * @since   1.0.0
     121 *
    116122 * @return  object|null  Store Config database row as an object, null if not found
    117123 */
     
    136142 * @param   array   $args   Optional array of optional arguments
    137143 *
     144 * @since   1.0.0
     145 *
    138146 * @return  object[]|null   Array of Store Config database rows as objects, null if not found
    139147 */
     
    205213 * @uses    IHeartJane\WebMenu\Constants\DB_TABLE_NAME Constants\DB_TABLE_NAME
    206214 *
     215 * @since   1.0.0
     216 *
    207217 * @return  object[]|array   Array of Store Config database rows as objects, empty array if none found. Array keys are Store Config ID
    208218 */
     
    223233 * @uses     IHeartJane\WebMenu\Constants\DB_TABLE_NAME Constants\DB_TABLE_NAME
    224234 *
     235 * @since    1.0.0
     236 *
    225237 * @return   int     Returns the count of Store Config IDs
    226238 */
  • jane-menu/trunk/includes/store-configs__form.php

    r2854692 r2865958  
    88use IHeartJane\WebMenu\Constants;
    99use IHeartJane\WebMenu\StoreConfigs;
     10use IHeartJane\WebMenu\Sitemap;
    1011use IHeartJane\WebMenu\Helpers;
    1112
     
    1920 * @uses     IHeartJane\WebMenu\StoreConfigs\get_all_configs()          StoreConfigs\get_all_configs()
    2021 * @uses     IHeartJane\WebMenu\StoreConfigs\insert_config()            StoreConfigs\insert_config()
     22 * @uses     IHeartJane\WebMenu\Sitemap\update_remote_sitemap()         Sitemap\update_remote_sitemap()
    2123 *
    2224 * @see      https://developer.wordpress.org/reference/hooks/admin_init/ Codex, action: admin_init
     25 *
     26 * @since 1.0.0
    2327 *
    2428 * @return   void   The result is handled through the add_settings_error() or wp_redirect()
     
    140144    // redirect if success
    141145   
    142     if( ! empty( $success ) ){
     146    if( ! empty( $success ) && ! is_wp_error( $success ) ){
     147       
     148        Sitemap\update_remote_sitemap();
    143149       
    144150        $redirect_to = add_query_arg( array( 'message' => 'success' ), $page_url );
     
    148154    }
    149155   
     156    if( is_wp_error( $success ) ){
     157       
     158        add_settings_error( 'jane_store_config_error', 'jane_store_config_error', $success->get_error_message() );
     159       
     160    } else {
    150161   
    151     add_settings_error( 'jane_store_config_error', 'jane_store_config_error', "Error: Something went wrong" );
     162        add_settings_error( 'jane_store_config_error', 'jane_store_config_error', "Error: Something went wrong" );
     163    }
     164   
    152165   
    153166    return;
Note: See TracChangeset for help on using the changeset viewer.