<?php

namespace RemoveUnusedMedia;

use RemoveUnusedMedia\Util as Util;
/*
* Plugin Name:       Remove Unused Media
* Plugin URI:        https://remove-unused-media.com/
* Description:       Detect and delete unused images
* Version:           1.3.10
* Requires at least: 4.0
* Requires PHP:      7.0
* Author:            Technikelfe
* Author URI:        https://technikelfe.com
* Text Domain:       remove-unused-media
*
*/
if ( !function_exists( 'RemoveUnusedMedia\\rum_fs' ) ) {
    function rum_fs() {
        global $rum_fs;
        if ( !isset( $rum_fs ) ) {
            require_once dirname( __FILE__ ) . '/freemius/start.php';
            $rum_fs = fs_dynamic_init( [
                'id'               => '8338',
                'slug'             => 'remove-unused-media-free',
                'premium_slug'     => 'remove-unused-media',
                'type'             => 'plugin',
                'public_key'       => 'pk_8a40a8386381530ed16feb04ef940',
                'premium_suffix'   => 'Remove Unused Media',
                'has_addons'       => false,
                'has_paid_plans'   => true,
                'is_org_compliant' => true,
                'has_affiliation'  => 'selected',
                'menu'             => [
                    'slug'        => 'rum',
                    'first-path'  => 'upload.php?page=rum',
                    'contact'     => false,
                    'support'     => false,
                    'affiliation' => false,
                    'parent'      => [
                        'slug' => 'upload.php',
                    ],
                ],
                'is_live'          => true,
                'is_premium'       => false,
            ] );
        }
        return $rum_fs;
    }

    rum_fs();
    do_action( 'rum_fs_loaded' );
}
if ( !function_exists( 'RemoveUnusedMedia\\init' ) ) {
    require_once 'rum_api.php';
    require_once 'rum_html.php';
    require_once 'rum_util.php';
    require_once 'rum_db.php';
    function init() {
        add_action( 'admin_menu', 'RemoveUnusedMedia\\options_page' );
        add_action( 'admin_enqueue_scripts', 'RemoveUnusedMedia\\enqueue_css_js' );
        add_action( 'remove_unused_media_ajax', 'RemoveUnusedMedia\\API\\ajax_script' );
        add_action( 'wp_ajax_remove_unused_media', 'RemoveUnusedMedia\\API\\rum_ajax' );
        $file = plugin_basename( __FILE__ );
        add_filter( 'plugin_action_links_' . $file, 'RemoveUnusedMedia\\dash_link' );
        register_deactivation_hook( __FILE__, 'RemoveUnusedMedia\\deactivate' );
        if ( rum_fs()->is_free_plan() || rum_fs()->can_use_premium_code() ) {
            add_action( 'rum_analyze', 'RemoveUnusedMedia\\API\\analyze' );
        }
        add_filter(
            'set-screen-option',
            'RemoveUnusedMedia\\set_screen_option',
            10,
            3
        );
        add_action( 'activated_plugin', 'RemoveUnusedMedia\\activation_redirect' );
        add_action( 'admin_notices', 'RemoveUnusedMedia\\HTML\\admin_notice' );
        rum_fs()->add_action( 'after_uninstall', 'RemoveUnusedMedia\\uninstall' );
        add_action( 'delete_attachment', 'RemoveUnusedMedia\\attachment_deleted' );
        add_action( 'wp_trash_post', 'RemoveUnusedMedia\\attachment_trashed' );
        add_action( 'plugins_loaded', 'RemoveUnusedMedia\\load_plugin_textdomain' );
        if ( rum_fs()->is_free_plan() ) {
            add_action( 'admin_notices', 'RemoveUnusedMedia\\show_free_message' );
        }
    }

    function show_free_message() {
        $screen = get_current_screen();
        if ( $screen->id !== 'media_page_rum' ) {
            return;
        }
        $pro_link = admin_url( 'upload.php?page=rum-pricing' );
        $logo_url = plugins_url( 'assets/RUM-Logo.png', __FILE__ );
        $total_post_count = Util\num_posts( ['post', 'page'] );
        ?>
        <div class="notice" style="background: rgb(6,24,57);">
            <a href="<?php 
        echo esc_url( $pro_link );
        ?>" style="display: flex; padding: 1em; text-decoration: none; color: #fff;gap: 3em;align-items: center;">
                <img style="max-width: 25%;" width="150px" src="<?php 
        echo $logo_url;
        ?>" />
                <div>
                    <p style="font-size: 1.5em; font-weight: 600; margin-top: 0;">
                    <?php 
        _e( 'Want to look for more than 15 images or do you also want to search in custom post types and plugin content?', 'remove-unused-media' );
        ?>
                        <br>
                    <?php 
        _e( 'Upgrade to the Pro version of Remove Unused Media now.', 'remove-unused-media' );
        ?>
                    </p>
                    <?php 
        if ( $total_post_count > 15 ) {
            echo "<p style=\"font-size: 1.25em;\">";
            printf( __( 'You currently have %d posts, pages!', 'remove-unused-media' ), $total_post_count );
            echo "</p>";
        }
        ?>
                    <div class="button" style="margin-top: 1em; font-weight: bold;">
                        <?php 
        _e( 'Get Remove Unused Media Pro now!', 'remove-unused-media' );
        ?>
                    </div>

                </div>
            </a>
        </div>
        <?php 
        if ( $found_items = Util\check_rum_pro_active_items() ) {
            ?>
            <div class="notice notice-warning">
                <h2><?php 
            _e( 'Warning!', 'remove-unused-media' );
            ?></h2>
                <p><strong>
                    <?php 
            _e( 'You are using a plugin or theme that will only be searched if you upgrade to the Pro version of Remove Unused Media. Please upgrade to Pro!', 'remove-unused-media' );
            ?>
                    </strong>
                </p>
                <ul>
                    <?php 
            foreach ( $found_items as $item ) {
                echo "<li>" . $item . "</li>";
            }
            ?>
                </ul>
            </div>
        <?php 
        }
    }

    function load_plugin_textdomain() {
        \load_plugin_textdomain( 'remove-unused-media', false, basename( __DIR__ ) . '/languages/' );
    }

    function attachment_deleted(  $post_id  ) {
        $file = get_attached_file( $post_id );
        $filesize = filesize( $file );
        $data = get_option( 'rum_deleted', [] );
        $data[] = $filesize;
        update_option( 'rum_deleted', $data, false );
    }

    function attachment_trashed(  $post_id  ) {
        if ( 'attachment' == get_post_type( $post_id ) ) {
            $file = get_attached_file( $post_id );
            $filesize = filesize( $file );
            $data = get_option( 'rum_trashed', [] );
            $data[] = $filesize;
            update_option( 'rum_trashed', $data, false );
        }
    }

    function activation_redirect(  $plugin  ) {
        if ( $plugin == plugin_basename( __FILE__ ) ) {
            exit( wp_redirect( menu_page_url( 'rum', false ) ) );
        }
    }

    function set_screen_option(  $status, $option, $value  ) {
        return ( 'rum_per_page' == $option ? $value : $status );
    }

    function options_page() {
        $hook = add_media_page(
            'Remove Unused Media',
            'Remove Unused Media',
            'activate_plugins',
            'rum',
            '\\RemoveUnusedMedia\\HTML\\page_html',
            20
        );
        add_action( 'load-' . $hook, 'RemoveUnusedMedia\\API\\rum_action' );
    }

    function dash_link(  $links  ) : array {
        $url = menu_page_url( 'rum', false );
        $link_text = _x( 'Dashboard', 'Link in plugin list', 'remove-unused-media' );
        $link = "<a href='{$url}'>{$link_text}</a>";
        return array_merge( [$link], $links );
    }

    function enqueue_css_js(  $hook  ) {
        if ( 'media_page_rum' === $hook ) {
            wp_enqueue_style( 'rum.css', plugin_dir_url( __FILE__ ) . 'rum.css' );
        }
    }

    function deactivate() {
        Util\reset();
    }

    function uninstall() {
        Util\reset( true );
    }

    init();
}