search_theme_files()

Search theme files or plugins for a keyphrase.

PHP January 30, 2017

Usage

This function runs automatically, so it is not called manually. Is this incorrect?

Additional Notes

This function is used by the Developer Information dashboard metabox and otherwise shouldn’t need to be called manually.

Source File

Located in /functions/nebula_admin.php on line 1463.

PHP
add_action('wp_ajax_search_theme_files', 'search_theme_files');
add_action('wp_ajax_nopriv_search_theme_files', 'search_theme_files');
function search_theme_files(){
    if ( !wp_verify_nonce($_POST['nonce'], 'nebula_ajax_nonce') ){ die('Permission Denied.'); }

    ini_set('max_execution_time', 120);
    ini_set('memory_limit', '512M');
    $searchTerm = htmlentities(stripslashes($_POST['data'][0]['searchData']));

    if ( strlen($searchTerm) < 3 ){
        echo '<p><strong>Error:</strong> Minimum 3 characters needed to search!</p>';
        die();
    }

    if ( $_POST['data'][0]['directory'] === 'theme' ){
        $dirpath = get_template_directory();
    } elseif ( $_POST['data'][0]['directory'] === 'parent' ){
        $dirpath = get_template_directory();
    } elseif ( $_POST['data'][0]['directory'] === 'child' ){
        $dirpath = get_stylesheet_directory();
    } elseif ( $_POST['data'][0]['directory'] === 'wireframe' ){
        $dirpath = get_theme_root() . '/' . nebula_option('wireframe_theme');
    } elseif ( $_POST['data'][0]['directory'] === 'staging' ){
        $dirpath = get_theme_root() . '/' . nebula_option('staging_theme');
    } elseif ( $_POST['data'][0]['directory'] === 'production' ){
        if ( nebula_option('production_theme') ){
            $dirpath = get_theme_root() . '/' . nebula_option('production_theme');
        } else {
            $dirpath = get_stylesheet_directory();
        }
    } elseif ( $_POST['data'][0]['directory'] === 'plugins' ){
        $dirpath = WP_PLUGIN_DIR;
    } elseif ( $_POST['data'][0]['directory'] === 'uploads' ){
        $uploadDirectory = wp_upload_dir();
        $dirpath = $uploadDirectory['basedir'];
    } else {
        echo '<p><strong>Error:</strong> Please specify a directory to search!</p>';
        die();
    }

    echo '<p class="resulttext">Search results for <strong>"' . $searchTerm . '"</strong> in the <strong>' . $_POST['data'][0]['directory'] . '</strong> directory:</p><br />';

    $file_counter = 0;
    $instance_counter = 0;
    foreach ( glob_r($dirpath . '/*') as $file ){
        $counted = 0;
        if ( is_file($file) ){
            if ( strpos(basename($file), $searchTerm) !== false ){
                echo '<p class="resulttext">' . str_replace($dirpath, '', dirname($file)) . '/<strong>' . basename($file) . '</strong></p>';
                $file_counter++;
                $counted = 1;
            }

            $skipFilenames = array('error_log');
            if ( !contains(basename($file), skip_extensions()) && !contains(basename($file), $skipFilenames) ){
                foreach ( file($file) as $lineNumber => $line ){
                    if ( stripos(stripslashes($line), $searchTerm) !== false ){
                        $actualLineNumber = $lineNumber+1;
                        echo '<div class="linewrap">
                                <p class="resulttext">' . str_replace($dirpath, '', dirname($file)) . '/<strong>' . basename($file) . '</strong> on <a class="linenumber" href="#">line ' . $actualLineNumber . '</a>.</p>
                                <div class="precon"><pre class="actualline">' . trim(htmlentities($line)) . '</pre></div>
                            </div>';
                        $instance_counter++;
                        if ( $counted === 0 ){
                            $file_counter++;
                            $counted = 1;
                        }
                    }
                }
            }
        }
    }
    echo '<br /><p class="resulttext">Found ';
    if ( $instance_counter ){
        echo '<strong>' . $instance_counter . '</strong> instances in ';
    }
    echo '<strong>' . $file_counter . '</strong> file';
    echo ( $file_counter == 1 )? '.</p>': 's.</p>';
    wp_die();
}

Override

To override this PHP function, call the override filter (found in the first line of the function if it exists) in a hook.

PHP
add_filter('pre_nebula_whatever', 'my_custom_function_name');
function my_custom_function_name($parameter){
    //Write your own code here
}

You can completely disable this PHP function with a single line filter:

PHP
 add_filter('pre_nebula_whatever', '__return_false');