<?php
/*
Theme Designed By: S O R O U S H M A R K E T
Email: info@soroushmarket.ir
Demo Website: soroushmarket.ir
Author Website: soroushmarket.ir
*/
   require_once get_template_directory() . '/admin/index.php';
   require_once get_template_directory() . '/includes/edd-recent-downloads.php';
   require_once get_template_directory() . '/includes/edd-random-downloads.php';
   require_once get_template_directory() . '/includes/random-post.php';
   require_once get_template_directory() . '/includes/recent-post.php';
   require_once get_template_directory() . '/includes/epay.php';
   require_once get_template_directory() . '/includes/fnews.php';
   require_once get_template_directory() . '/includes/d-sidebar.php';
   require_once get_template_directory() . '/includes/views.php';
   require_once get_template_directory() . '/includes/dynamic_style.php';
   require_once get_template_directory() . '/includes/breadcrumbs.php';
   require_once get_template_directory() . '/includes/image-banner.php';
   require_once get_template_directory() . '/includes/menu-icon.php';
   require_once get_template_directory() . '/includes/widget-icon/widget-icon.php';
   require_once get_template_directory() . '/includes/wp_bootstrap_navwalker.php';
   require_once get_template_directory() . '/includes/edd-download-images.php';
   require_once get_template_directory() . '/plugins/plugin.php';
   require_once get_template_directory() . '/includes/simple-link-list-widget/simple-link-list-widget.php';
   require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
   
   function my_func_docs(){
    global $data;
   $color = basename($data['custom_color']);
   $css_color = str_replace("png","css",$color);
   $theme_color = str_replace(".png","",$color); 
    wp_register_style('my-boot',get_template_directory_uri() . '/css/bootstrap.min.css');
    wp_enqueue_style('my-boot');
    wp_register_style('my-boot-rtl',get_template_directory_uri() . '/css/bootstrap-rtl.min.css');
    wp_enqueue_style('my-boot-rtl');
    wp_register_style('my-fontawe',get_template_directory_uri() . '/css/font-awesome.min.css');
    wp_enqueue_style('my-fontawe');
    wp_register_style('my-npro',get_template_directory_uri() . '/css/nprogress.css');
    wp_enqueue_style('my-npro');
    wp_register_style('my-style',get_template_directory_uri() . '/style.css');
    wp_enqueue_style('my-style');
    wp_register_style('my-color',get_template_directory_uri() . '/css/'.$css_color);
    wp_enqueue_style('my-color');
    }
    add_action('wp_enqueue_scripts','my_func_docs');


function my_init() {
  if (!is_admin()) {
    wp_deregister_script('jquery');
    wp_register_script('jquery', get_template_directory_uri() . '/js/jquery.min.js', false, '2.2.4');
    wp_enqueue_script('jquery');
    wp_enqueue_script('my_script', get_template_directory_uri() . '/js/bootstrap.min.js', array('jquery'), '1.0');
    wp_enqueue_script('my_np', get_template_directory_uri() . '/js/nprogress.js', array('jquery'), '1.0');
    wp_enqueue_script('my_rs', get_template_directory_uri() . '/js/responsiveslides.min.js', array('jquery'), '1.0');
    wp_enqueue_script('my_sp', get_template_directory_uri() . '/js/smoothproducts.min.js', array('jquery'), '1.0');
    wp_enqueue_script('my_ps', get_template_directory_uri() . '/js/persianumber.min.js', array('jquery'), '1.0');
  }
}
add_action('init', 'my_init');

   //Rregister navigation menu
   register_nav_menus( array(
           'primary' => __( 'Primary Menu', 'THEMENAME' ),
   ));
   
   //Wordpress page navigation
   function wp_pagination() {
   global $wp_query;
   $big = 999999999;
   $page_format = paginate_links( array(
       'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
       'format' => '?paged=%#%',
       'current' => max( 1, get_query_var('paged') ),
       'total' => $wp_query->max_num_pages,
    'prev_text'    => __('« صفحه قبل'),
       'next_text'    => __('صفحه بعد »'),
       'type'  => 'array'
   ));
   if( is_array($page_format) ) {
               $paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');
               echo '<div><ul>';
               echo '<li><span>'. $paged . ' از ' . $wp_query->max_num_pages .'</span></li>';
               foreach ( $page_format as $page ) {
               echo "<li>$page</li>";
               }
              echo '</ul></div>';
   }
   }
   function wp_product_pagination() {
   global $products;
   $big = 999999999;
   $page_format = paginate_links( array(
   'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
   'format' => '?paged=%#%',
   'current' => max( 1, get_query_var('paged') ),
   'total' => $products->max_num_pages,
   'prev_text'    => __('« صفحه قبل'),
   'next_text'    => __('صفحه بعد »'),
   'type'  => 'array'
   ) );
   if( is_array($page_format) ) {
               $paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');
               echo '<div><ul>';
               echo '<li><span>'. $paged . ' از ' . $products->max_num_pages .'</span></li>';
               foreach ( $page_format as $page ) {
                       echo "<li>$page</li>";
               }
              echo '</ul></div>';
   }
   }
   
   // remove the standard button that shows after the download's content
   remove_action( 'edd_after_download_content', 'edd_append_purchase_link' );
   /**
    * Easy Digital Downloads
    * Change the button text
  */
   function edd_text_args( $args ) {
            $args['text'] = 'خرید';
            $args['class'] = 'col-lg-11 col-md-11 col-sm-11 col-xs-12 btn btn-primary btn-sm';
    return $args;
   }
   add_filter( 'edd_purchase_link_args', 'edd_text_args' );
  
   // Add Thumbnail Theme Support
   add_theme_support('title-tag');
   add_theme_support('html5');
   add_theme_support('post-thumbnails');
   add_image_size( 'single-download', 300, 300, true );
   add_image_size( 'widget-img', 64, 64, true );
   add_theme_support( 'automatic-feed-links' );
   function custom_edd_add_custom_field_support( $supports ) {
    $new = array( 'custom-fields' );
    return array_merge( $new, $supports );
   }
   add_action('init', 'my_custom_init');
   function my_custom_init() {
       add_post_type_support( 'download', 'publicize' );
   }
   //Theme Designed By: S O R O U S H M A R K E T
   //Add meta-Box for download details
   add_action( 'add_meta_boxes', 'cd_meta_box_add' );
   function cd_meta_box_add()
   {
       add_meta_box( 'download-meta-box-id', 'مشخصات فایل', 'download_meta_box_cb', 'download', 'normal', 'high' );
   }
   function download_meta_box_cb()
   {
       // $post is already set, and contains an object: the WordPress post
       global $post;
    $file_type = get_post_meta($post->ID, 'download_file_type', true);
    $file_size = get_post_meta($post->ID, 'download_file_size', true);
    $page_count = get_post_meta($post->ID, 'download_page_count', true);
       $preview_link = get_post_meta($post->ID, 'download_preview_link', true);
       // We'll use this nonce field later on when saving.
       wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
       ?>
<p>
   <label for="download_file_type">نوع فایل</label>
   <input type="text" name="download_file_type" id="download_file_type" value="<?php echo $file_type; ?>" />
</p>
<p>
   <label for="download_file_size">حجم فایل</label>
   <input type="text" name="download_file_size" id="download_file_size" value="<?php echo $file_size; ?>" />
</p>
<p>
   <label for="download_page_count">تعداد صفحات</label>
   <input type="text" name="download_page_count" id="download_page_count" value="<?php echo $page_count; ?>" />
</p>
<p>
   <label for="download_preview_link">لینک فایل پیش نمایش</label>
   <input type="text" class="custom_media_url" name="download_preview_link"  id="download_preview_link"  value="<?php echo $preview_link; ?>">
   <input type="button" class="button button-primary custom_media_button" id="custom_media_button" value="بارگذاری فایل" style="margin-top:5px;" />
</p>
<?php    
   }
   add_action( 'save_post', 'cd_meta_box_save' );
   function cd_meta_box_save( $post_id )
   {
       // Bail if we're doing an auto save
       if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
       // if our nonce isn't there, or we can't verify it, bail
       if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
       // if our current user can't edit this post, bail
       if( !current_user_can( 'edit_post' ) ) return;
       // now we can actually save the data
       $allowed = array( 
           'a' => array( // on allow a tags
           'href' => array() // and those anchors can only have href attribute
           )
       );
       // Make sure your data is set before trying to save it
       if( isset( $_POST['download_file_type'] ) )
           update_post_meta( $post_id, 'download_file_type', wp_kses( $_POST['download_file_type'], $allowed ) );
    if( isset( $_POST['download_file_size'] ) )
           update_post_meta( $post_id, 'download_file_size', wp_kses( $_POST['download_file_size'], $allowed ) );
    if( isset( $_POST['download_page_count'] ) )
           update_post_meta( $post_id, 'download_page_count', wp_kses( $_POST['download_page_count'], $allowed ) );
       if( isset( $_POST['download_preview_link'] ) )
           update_post_meta( $post_id, 'download_preview_link', wp_kses( $_POST['download_preview_link'], $allowed ) );
   }
   
   //Enable comments on downloads
   function custom_enable_comments( $supports ) {
    return array_merge( $supports, array( 'comments' ) );
   }
   add_filter( 'edd_download_supports', 'custom_enable_comments' );
   
   /**
    * Filter the except length to 20 characters.
    *
    * @param int $length Excerpt length.
    * @return int (Maybe) modified excerpt length.
    */
   function excerpt_length( $length ) {
       return 10;
   }
   add_filter( 'excerpt_length', 'excerpt_length', 999 );
   function fontawesome_dashboard() {
      wp_enqueue_style('fontawesome', get_template_directory_uri() .'/css/font-awesome.min.css');
   }
   add_action('admin_init', 'fontawesome_dashboard');
   /**
   * Loads admin-side menu-icon scripts and styles
   */
    function admin_menu_icon_styles($page)
    {
        if($page !== 'nav-menus.php')
            return;
        wp_enqueue_style('menu-icon-admin-style', get_template_directory_uri() .'/includes/menu-icon-admin.css');
    }
    add_action( 'admin_enqueue_scripts', 'admin_menu_icon_styles' );

   //Modify Rss Feed to add download post_type
   function myfeed_request($qv) {
    if (isset($qv['feed']))
        $qv['post_type'] = array('post', 'download');
    return $qv;
   }
   add_filter('request', 'myfeed_request');

   /*** Remove Query String from Static Resources ***/
   function remove_cssjs_ver( $src ) {
   if( strpos( $src, '?ver=' ) )
   $src = remove_query_arg( 'ver', $src );
   return $src;
   }
   add_filter( 'style_loader_src', 'remove_cssjs_ver', 10, 2 );
   add_filter( 'script_loader_src', 'remove_cssjs_ver', 10, 2 );

   //Toman EDD Currency
   function edd_rial_currency( $formatted, $currency, $price ) {
   if ( ! is_admin() ) {
   $price = @str_replace( edd_get_option( 'thousands_separator', ',' ) , '', $price );
   $price = @$price / 10;
   $price = per_number($price);
   return $price . ' تومان';
   }
   else
   return $price . ' ریال';
   }
  if ($data['toman_currency'] == 1) {   
   add_filter( 'edd_rial_currency_filter_after', 'edd_rial_currency', 10, 3 );
   add_filter( 'edd_rial_currency_filter_before', 'edd_rial_currency', 10, 3 );
  }

   add_filter( 'pre_get_posts', 'my_get_posts' );
   function my_get_posts( $query ) {
   
    if ( is_home() && $query->is_main_query() )
        $query->set( 'post_type', 'download' );
   
    return $query;
   }
   function get_attachment_id_from_src ($image_src) {
   
                                               global $wpdb;
                                               $query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$image_src'";
                                               $id = $wpdb->get_var($query);
                                               return $id;
   
                                           }
   add_filter( 'widget_tag_cloud_args', 'tj_tag_cloud_limit' );
   function tj_tag_cloud_limit($args){ 
    global $data;
    // Check if taxonomy option of the widget is set to tags
    if ( isset($args['taxonomy']) && $args['taxonomy'] == 'download_tag' ){
        $args['number'] = $data['tag_number']; // Number of tags to show
        }
    return $args;
   }
   function short_title($after = '', $length) {
      $mytitle = explode(' ', get_the_title(), $length);
      if (count($mytitle)>=$length) {
          array_pop($mytitle);
          $mytitle = implode(" ",$mytitle). $after;
      } else {
          $mytitle = implode(" ",$mytitle);
      }
          return $mytitle;
   }
   
   //Create edit-profile page
   if (isset($_GET['activated']) && is_admin()){
           $new_page_title = 'حساب کاربری';
           $new_page_content = '[edd_profile_editor]';
           $new_page_addr = 'edit-profile';
           $new_page_template = ''; //ex. template-custom.php. Leave blank if you don't want a custom page template.
           //don't change the code bellow, unless you know what you're doing
           $page_check = get_page_by_title($new_page_title);
           $new_page = array(
                   'post_type' => 'page',
                   'post_title' => $new_page_title,
                   'post_name' => $new_page_addr,
                   'post_content' => $new_page_content,
                   'post_status' => 'publish',
                   'post_author' => 1,
           );
           if(!isset($page_check->ID)){
                   $new_page_id = wp_insert_post($new_page);
                   if(!empty($new_page_template)){
                           update_post_meta($new_page_id, '_wp_page_template', $new_page_template);
                   }
           }
   }
   //Create register page
   if (isset($_GET['activated']) && is_admin()){
           $new_page_title = 'ثبت نام';
           $new_page_content = '[edd_register]';
           $new_page_addr = 'register';
           $new_page_template = ''; //ex. template-custom.php. Leave blank if you don't want a custom page template.
           //don't change the code bellow, unless you know what you're doing
           $page_check = get_page_by_title($new_page_title);
           $new_page = array(
                   'post_type' => 'page',
                   'post_title' => $new_page_title,
                   'post_name' => $new_page_addr,
                   'post_content' => $new_page_content,
                   'post_status' => 'publish',
                   'post_author' => 1,
           );
           if(!isset($page_check->ID)){
                   $new_page_id = wp_insert_post($new_page);
                   if(!empty($new_page_template)){
                           update_post_meta($new_page_id, '_wp_page_template', $new_page_template);
                   }
           }
   }
   //Create login page
   if (isset($_GET['activated']) && is_admin()){
           $new_page_title = 'ورود';
           $new_page_content = '[edd_login]';
           $new_page_addr = 'login';
           $new_page_template = ''; //ex. template-custom.php. Leave blank if you don't want a custom page template.
           //don't change the code bellow, unless you know what you're doing
           $page_check = get_page_by_title($new_page_title);
           $new_page = array(
                   'post_type' => 'page',
                   'post_title' => $new_page_title,
                   'post_name' => $new_page_addr,
                   'post_content' => $new_page_content,
                   'post_status' => 'publish',
                   'post_author' => 1,
           );
           if(!isset($page_check->ID)){
                   $new_page_id = wp_insert_post($new_page);
                   if(!empty($new_page_template)){
                           update_post_meta($new_page_id, '_wp_page_template', $new_page_template);
                   }
           }
   }
// unregister all widgets
 function unregister_default_widgets() {
     unregister_widget('WP_Widget_Links');
     unregister_widget('WP_Widget_Recent_Posts');
     unregister_widget('Twenty_Eleven_Ephemera_Widget');
 }
  add_action('widgets_init', 'unregister_default_widgets', 11);
   // Allow .ico extention uploads.
  function my_custom_upload_mimes($mimes = array()) {
  $mimes['ico'] = "application/ico";
  return $mimes;
  }
  add_action('upload_mimes', 'my_custom_upload_mimes');
?>