Advertisement
bedas

Untitled

Mar 29th, 2021
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.56 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * The admin-specific functionality of the plugin.
  5.  *
  6.  * @link       https://www.tukutoi.com/
  7.  * @since      1.0.0
  8.  *
  9.  * @package    Tw_Seo
  10.  * @subpackage Tw_Seo/admin
  11.  */
  12.  
  13. /**
  14.  * The admin-specific functionality of the plugin.
  15.  *
  16.  * Defines the plugin name, version, and two examples hooks for how to
  17.  * enqueue the admin-specific stylesheet and JavaScript.
  18.  *
  19.  * @package    Tw_Seo
  20.  * @subpackage Tw_Seo/admin
  21.  * @author     TukuToi <[email protected]>
  22.  */
  23. class Tw_Seo_Admin {
  24.  
  25.     /**
  26.      * The ID of this plugin.
  27.      *
  28.      * @since    1.0.0
  29.      * @access   private
  30.      * @var      string    $plugin_name    The ID of this plugin.
  31.      */
  32.     private $plugin_name;
  33.  
  34.     /**
  35.      * The Human Name of the plugin
  36.      *
  37.      * @since    1.0.0
  38.      * @access   private
  39.      * @var      string    $human_plugin_name    The humanly readable plugin name
  40.      */
  41.     private $human_plugin_name;
  42.  
  43.     /**
  44.      * The version of this plugin.
  45.      *
  46.      * @since    1.0.0
  47.      * @access   private
  48.      * @var      string    $version    The current version of this plugin.
  49.      */
  50.     private $version;
  51.  
  52.  
  53.     /**
  54.      * The SEO Field Slugs
  55.      *
  56.      * @since    1.0.0
  57.      * @access   private
  58.      * @var      array    $field_slugs    Slugs of the Fields to create/use.
  59.      */
  60.     private $field_slugs;
  61.  
  62.     /**
  63.      * Initialize the class and set its properties.
  64.      *
  65.      * @since    1.0.0
  66.      * @param      string    $plugin_name       The name of this plugin.
  67.      * @param      string    $version    The version of this plugin.
  68.      */
  69.     public function __construct( $plugin_name, $human_plugin_name, $version ) {
  70.  
  71.         $this->plugin_name       = $plugin_name;
  72.         $this->version           = $version;
  73.         $this->human_plugin_name = $human_plugin_name;
  74.         $this->fields            = $this->instantiate_fields();
  75.  
  76.         $this->load_dependencies();
  77.  
  78.     }
  79.  
  80.     private function load_dependencies() {
  81.  
  82.         /**
  83.          * The class responsible for orchestrating the actions and filters of the
  84.          * core plugin.
  85.          */
  86.         require_once plugin_dir_path( dirname( __FILE__ ) ) .  'admin/class-tw-seo-settings.php';
  87.  
  88.     }
  89.  
  90.     private function instantiate_fields(){
  91.  
  92.         $frequency_options = array(
  93.             'wpcf-fields-select-option-frequency-always'    => [ 'title' => 'Always', 'value' => 'always'],
  94.             'wpcf-fields-select-option-frequency-hourly'    => [ 'title' => 'Hourly', 'value' => 'hourly'],
  95.             'wpcf-fields-select-option-frequency-daily'     => [ 'title' => 'Daily', 'value' => 'daily'],
  96.             'wpcf-fields-select-option-frequency-weekly'    => [ 'title' => 'Weekly', 'value' => 'weekly'],
  97.             'wpcf-fields-select-option-frequency-monthly'   => [ 'title' => 'Monthly', 'value' => 'monthly'],
  98.             'wpcf-fields-select-option-frequency-yearly'    => [ 'title' => 'Yearly', 'value' => 'yearly'],
  99.             'wpcf-fields-select-option-frequency-never'     => [ 'title' => 'Never', 'value' => 'never'],
  100.             'default'                                       => 'wpcf-fields-select-option-frequency-weekly',
  101.         );
  102.  
  103.         $conditional_display = array(
  104.             'relation'      => 'AND',
  105.             'conditions'    => array(
  106.                 uniqid('condition_')    => array(
  107.                     'field'     => 'Exclude from SiteMap',
  108.                     'operation' => '<>',
  109.                     'value'     => '1',
  110.                     'month' => '03',
  111.                     'date' => '29',
  112.                     'year' => '2021',
  113.                 ),
  114.             ),
  115.             'custom'        => null,
  116.             'custom_use'    => 0,
  117.         );
  118.  
  119.  
  120.         $fields = array(
  121.             $this->plugin_name .'-title'                    => ['type' => 'textfield', 'name' => 'Custom SEO Title', 'description' => 'Optional dedicated SEO Title (for SEO "title" meta tag)'],
  122.             $this->plugin_name .'-keywords'                 => ['type' => 'textfield', 'name' => 'SEO Keywords', 'description' => 'Keywords for "keywords" meta tag '],
  123.             $this->plugin_name .'-description'              => ['type' => 'textarea', 'name' => 'SEO Description', 'description' => 'Short Description for "description" meta tag'],
  124.             $this->plugin_name .'-exclude-from-sitemap'     => ['type' => 'checkbox', 'name' => 'Exclude from SiteMap', 'description' => 'Wether to include or exclude this Post from the Sitemap XML'],
  125.             $this->plugin_name .'-sitemap-priority'         => ['type' => 'numeric', 'name' => 'SiteMap Priority', 'data' => ['user_default_value' => 0.5, 'conditional_display' => $conditional_display], 'description' => 'The Priority of this Post in the Sitemap XML (0.0 to 1.0)'],
  126.             $this->plugin_name .'-frequency'                => ['type' => 'select', 'name' => 'Update Frequency', 'data' => ['options' => $frequency_options, 'conditional_display' => $conditional_display, 'submit-key' => 'select-frequency-always', 'disabled_by_type' => 0], 'description' => 'Sitemap XML Frequency this Post will be updated with (how often do you plan to update this post)'],
  127.         );
  128.  
  129.         return $fields;
  130.  
  131.     }
  132.    
  133.     /**
  134.      * Check if Toolset Types Custom Field Group exist already
  135.      * @since 1.0.0
  136.      * @access private
  137.      */
  138.     private function check_if_types_group_exist( $title ) {
  139.  
  140.         global $wpdb;
  141.  
  142.         $db_query = $wpdb->get_row($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title=%s && post_status = 'publish' && post_type = 'wp-types-group' ", $title),'ARRAY_N');
  143.  
  144.         if( empty( $db_query ) )
  145.             return false;
  146.        
  147.         return true;
  148.  
  149.     }
  150.  
  151.     /**
  152.      * Register the stylesheets for the admin area.
  153.      *
  154.      * @since    1.0.0
  155.      */
  156.     public function register_styles() {
  157.  
  158.         /**
  159.          * This function is provided for demonstration purposes only.
  160.          *
  161.          * An instance of this class should be passed to the run() function
  162.          * defined in Tw_Seo_Loader as all of the hooks are defined
  163.          * in that particular class.
  164.          *
  165.          * The Tw_Seo_Loader will then create the relationship
  166.          * between the defined hooks and the functions defined in this
  167.          * class.
  168.          */
  169.  
  170.         wp_register_style( $this->plugin_name . '-styles', plugin_dir_url( __FILE__ ) . 'css/tw-seo-admin.css', array(), $this->version, 'all' );
  171.  
  172.     }
  173.  
  174.     /**
  175.      * Register the JavaScript for the admin area.
  176.      *
  177.      * @since    1.0.0
  178.      */
  179.     public function enqueue_scripts() {
  180.  
  181.         /**
  182.          * This function is provided for demonstration purposes only.
  183.          *
  184.          * An instance of this class should be passed to the run() function
  185.          * defined in Tw_Seo_Loader as all of the hooks are defined
  186.          * in that particular class.
  187.          *
  188.          * The Tw_Seo_Loader will then create the relationship
  189.          * between the defined hooks and the functions defined in this
  190.          * class.
  191.          */
  192.  
  193.         wp_register_script( $this->plugin_name . '-scripts', plugin_dir_url( __FILE__ ) . 'js/tw-seo-admin.js', array( 'jquery' ), $this->version, false );
  194.  
  195.     }
  196.  
  197.     /**
  198.      * Create Toolset Types Custom Fields if not existing
  199.      * @since 1.0.0
  200.      * @access private
  201.      */
  202.     public function register_fields() {
  203.  
  204.         //Preparation to Types control
  205.         $tw_seo_fields_array        = array();
  206.         $string_wpcf_not_controlled = md5( 'wpcf_not_controlled');
  207.  
  208.         foreach ( $this->fields  as $key => $value ) {
  209.             $tw_seo_fields_array[] = $key .'_'. $string_wpcf_not_controlled;
  210.         }
  211.  
  212.         if ( defined('WPCF_INC_ABSPATH') ) {
  213.  
  214.             //First, check if SEO Meta Tag Fields Group field does not exist
  215.             if ( ! $this->check_if_types_group_exist( 'SEO Fields' ) ) {
  216.                
  217.                 require_once WPCF_INC_ABSPATH . '/fields.php';
  218.                
  219.                 //Part 1: Assign to Types Control
  220.                 //Get Fields
  221.                 $fields      = wpcf_admin_fields_get_fields(false, true);
  222.                 $fields_bulk = wpcf_types_cf_under_control( 'add', array( 'fields' => $tw_seo_fields_array ) );
  223.  
  224.                 foreach ($fields_bulk as $field_id) {
  225.  
  226.                     if ( isset( $fields[$field_id] ) ) {
  227.                         $fields[$field_id]['data']['disabled'] = 0;
  228.                     }
  229.  
  230.                 }
  231.  
  232.                 //Save fields
  233.                 wpcf_admin_fields_save_fields( $fields );
  234.  
  235.                 //Retrieve updated fields
  236.                 $fields = wpcf_admin_fields_get_fields( false, false );
  237.  
  238.                 //Add Readable name to each field
  239.                 foreach ( $fields as $key => $value ) {
  240.                     if( array_key_exists( $key, $this->fields ) ){
  241.                         $fields[$key]['name'] = $this->fields[$key]['name'];
  242.                         $fields[$key]['type'] = $this->fields[$key]['type'];
  243.                         if( isset( $this->fields[$key]['data']['user_default_value'] ) )
  244.                             $fields[$key]['data']['user_default_value'] = $this->fields[$key]['data']['user_default_value'];
  245.                         if( isset( $this->fields[$key]['data']['options'] ) )
  246.                             $fields[$key]['data']['options'] = $this->fields[$key]['data']['options'];
  247.                         if( isset( $this->fields[$key]['data']['conditional_display'] ) )
  248.                             $fields[$key]['data']['conditional_display'] = $this->fields[$key]['data']['conditional_display'];
  249.                     }
  250.                 }
  251.  
  252.                 //Save fields
  253.                 wpcf_admin_fields_save_fields( $fields );
  254.                 error_log( print_r( $fields , true) );
  255.                 //Define group
  256.                 $group = array(
  257.                     'name'                  => 'SEO Fields',
  258.                     'description'           => 'Custom Fields to specifiy SEO Meta Tag contents and settings.',
  259.                     'filters_association'   => 'any',
  260.                     'conditional_display'   => array( 'relation' => 'AND', 'custom' => '' ),
  261.                     'preview'               =>  'edit_mode',
  262.                     'admin_html_preview'    => '',
  263.                     'admin_styles'          => '',
  264.                     'slug'                  => $this->plugin_name .'-meta-tag-fields'
  265.                 );
  266.  
  267.                 //Save group
  268.                 $group_id = wpcf_admin_fields_save_group( $group );
  269.  
  270.                 //Save group fields
  271.                 wpcf_admin_fields_save_group_fields( $group_id, $fields_bulk );
  272.  
  273.             }
  274.         }
  275.     }
  276.    
  277.     public function tkt_seo_create_sitemap() {
  278.         $options = get_option( $this->plugin_name );
  279.  
  280.         $posts_for_sitemap = get_posts( array(
  281.             'numberposts' => -1,
  282.             'post_type'   => array_keys( $options['tw_seo_sitemap'] ),
  283.             'meta_key'    => $this->plugin_name .'-sitemap-priority',
  284.             'meta_type'      => 'NUMERIC',
  285.             'orderby' => array(
  286.                 'meta_value_num' => 'ASC',
  287.                 'modified'      => 'DESC',
  288.             ),
  289.             'meta_query' => array(
  290.                 array(
  291.                     'key' => 'wpcf-tkt-exclude-from-sitemap',
  292.                     'compare' => 'NOT EXISTS'
  293.                 )
  294.             )
  295.         ));
  296.  
  297.         //https://www.sitemaps.org/protocol.html
  298.         $sitemap  = '<?xml version="1.0" encoding="UTF-8"?>';
  299.         $sitemap .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
  300.  
  301.         foreach( $posts_for_sitemap as $post ) {
  302.            
  303.             setup_postdata( $post );
  304.  
  305.             $postdate = explode( " ", $post->post_modified );
  306.  
  307.             $sitemap .= '<url>';
  308.             $sitemap .= '<loc>' . get_permalink( $post->ID ) . '</loc>';
  309.             $sitemap .= '<lastmod>' . $postdate[0] . '</lastmod>';//should be YYYY-MM-DD or YYYY-MM-DDThh:mmTZD
  310.             $sitemap .= '<changefreq>monthly</changefreq>';//always hourly daily weekly monthly yearly never
  311.             $sitemap .= '<priority>0.8</priority>';//0.0 to 1.0
  312.             $sitemap .= '</url>';
  313.  
  314.         }
  315.  
  316.         $sitemap .= '</urlset>';
  317.  
  318.         $fopen = fopen( ABSPATH . 'sitemap.xml', 'w' );
  319.  
  320.         fwrite( $fopen, $sitemap );
  321.         fclose( $fopen );
  322.  
  323.     }
  324.  
  325. }
  326.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement