Categories
Uncategorized

Snippet: How to Clean up Shortcodes (vs)

I stumbled upon a nice little way to clean up short codes in WordPress. I did not create the gist nor do I take credit for it.

MAKE A BACKUP

I cannot tell you how many times I’ve done something small directly to the database only to find that I’ve fucked EVERYTHING up.

Do not be that guy. Do not lose all your work. It is WAY easier to setup automatic backups or run manual back ups. Protect yourself, wear a condom and back up your database.

If you WP-CLI, SSH into your server and run wp db backup.

Remove Visual Composer Shortcodes (WPBakery)

Run the following SQL query in Cpanel.

UPDATE wp_posts SET post_content = REGEXP_REPLACE(post_content, "\\[\/?vc(.*?)\]", "");

Replace “vs” with whatever short code you wish to remove. This will completely remove all

I needed this snippet because I wanted to transition away from Visual Composer (formerly WPBakery).

Remove Any WordPress Short code

I use the Woodmart theme and noticed alot of left over woodmart* shortcodes. Like I mentioned above, use the sql statement and replace ‘vc’ with whatever shortcode you wish to REMOVE.

In my case, to remove all [woodmart*] shortcodes. I ran the follow sql query.

UPDATE wp_posts SET post_content = REGEXP_REPLACE(post_content, "\\[\/?woodmart(.*?)\]", "");

Cleanup WordPress Shortcodes w/ WP-CLI

Remove Visual Composer shortcodes with WP-CLI db search-replace.

This method is easier if you have WP-CLI setup on your server AND feel comfortable executing commands. I really do not recommend you do this on a live website.

I would suggest you use VVV to setup a proper development environment.

TDLR;

wp search-replace --regex --verbose "\\[\/?vc(.*?)\]" "" wp_posts

Okay, check your content. If you dont see broken shortcodes anywhere you are good. Otherwise, look at remove any shortcode with WordPress .

Categories
Uncategorized

Snippet: How to setup git+ssh on Unix

Git is wonderful, we love it; but if you like me are sick and tired of being prompted for your username and password every push/pull then you haven’t discovered how to utilize SSH until now.

Sources:

Check for existing SSH keys

First, check to see if you have any SSH keys.

ls -al ~/.ssh

If you have any of the following, skip to Adding SSH key to Github.

  • id_dsa.pub
  • id_ecdsa.pub
  • id_ed25519.pub
  • id_rsa.pub

Generate an SSH key

If you do not have a key, you must generate a new key. Enter the follow command. Replace ‘your_email@example.com’ with the email on your GitHub account.

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

You will be prompted for a password. I never use one, but feel free to.

Add SSH key to ssh-agent

Start the ssh-agent in the background

eval "$(ssh-agent -s)"

Add your private key to the SSH-agent. If your key has a different name, replace ‘id_rsa‘ with the name of your key.

ssh-add ~/.ssh/id_rsa

Add SSH key to GitHub

Copy your SSH key to your clipboard

First, download xclip.

sudo apt-get install xclip

After xclip is finished downloading, copy your ssh key to your clipboard using the following command. If your SSH key have a different name than the example, replace ‘id_rsa‘ with the name of your key.

xclip -sel clip < ~/.ssh/id_rsa.pub

Next, go to visit Github, click your account in the top-right corner and click settings.

Click SSH and GPG keys

Authentication keys

Click New SSH key or Add SSH key.

SSH Key button

In the title field, add a descriptive label like ‘work computer’.

Paste your key into the “Key” field.

The key field

Click add SSH key, and enter password if prompted.

Sudo mode dialog

Categories
Uncategorized

How to find how many open ports you currently have via linux command line

In short: netstat -l | wc -l

Categories
Uncategorized

Ch 1: Security Introduction

The follow are questions from TestOut Security Pro. This page is merely for personal educational purposes.

1.1 Security Overview

What challenges does a security professional face?

A never ending arms race. Every day cyber criminals are finding new ways to exploit systems and use security professionals can barely keep up. You must EXPECT a breach, and know what do to when one occurs and how to minimize damage before hand.

What is the difference between integrity and non-repudiation?

Integrity is protection against alteration. Non-repudiation is proof a sender sent a message.

What are the three main goals of the CIA of Security?

  • Confidentiality – Ensure access to data is authorized.
  • Integrity – Ensures data is not modified.
  • Availability – Ensures 99.9% up time.

What are the key components of risk management?

The main goal of risk management is to reduce risk to an acceptable level. Components of risk management include:

  • Asset – Something of value to an entity
  • Threat – Potential for the loss of asset
  • Threat Agent – An attacker
  • Vulnerability – A weakness that allows a threat to be carried out
  • Exploit – A way of taking advantage of a vulnerability

What are three types of threat agents?

  • Employee
  • Spy
  • Hacker

Lab 1.2.2 & 1.2.3 Screenshots

What I learned…

Learned alot about information security. One, it is a constant game of cat & mouse similar to the relationship of a detective and serial criminal. I didn’t realize that if you connect to the internet, you can NEVER be completely safe against threats. The job of a security professional is to minimize vulnerabilities and potential for damage by breach.

Categories
Uncategorized

Ruby on Rails Model Associations – belongs_to, has_one & has_many

Source: https://guides.rubyonrails.org/association_basics.html

Setup

Hello, lets learn about how to link Model’s together in Ruby on Rails 5. First lets create our application and create a few models. This assumes you are properly setup to run rails new.

Open your terminal CTRL+ALT+T. Create or move to your projects folder, create the project and creates models.

~$ mkdir projects
~$ cd projects
~/projects$ rails new associations
~/projects$ cd associations
~/projects/associations$ rails g model author name website email
~/projects/associations$ rails g model post author:references
~/projects/associations$ rails db:migrate

After running rails db:migrate your schema should look like this:

db/schema.rb

ActiveRecord::Schema.define(version: 2019_08_23_165356) do

  create_table "authors", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "posts", force: :cascade do |t|
    t.integer "author_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["author_id"], name: "index_posts_on_author_id"
  end

end

belongs_to

By declaring that one model belongs_to another, you are telling Rails to maintain Primary Key – Foreign Key information between those two models. This sets up a one-to-one relationship, requiring that every Post can only have 1 author.

class Post < ApplicationRecord
  belongs_to :author
end

This code gives us gives our model a number of useful methods like getting the author of the post.

$ Post.first.author.name
=> "greg"

belongs_to associations must use the singular term. This is because Rails automatically infers the class name from the association name. If the association name is wrongly pluralized, then the inferred class will be wrongly pluralized too.

has_one

A has_one association is similar to belongs_to, but with one particular consequence. has_one assumes there is something on the other end of the association and will raise an error if nil.

has_one Association Diagram

Has_many

Indicates a one-to-many connection with another model. Has many is often found on the “opposite end” of a belongs_to connection. has_many means this model can have zero, one or more instances of another model.

class Author < ApplicationRecord
  has_many :books
end
has_many Association Diagram

has_many :through

Used to setup a many-to-many relationship with another model. This association declares there can be zero, 1 or more instanaces of another model through a third model.

class Physician < ApplicationRecord
  has_many :appointments
  has_many :patients, through: :appointments
end
 
class Appointment < ApplicationRecord
  belongs_to :physician
  belongs_to :patient
end
 
class Patient < ApplicationRecord
  has_many :appointments
  has_many :physicians, through: :appointments
end
has_many :through Association Diagram
Categories
Uncategorized

Planning the Adinabook Rails 5 Application

This is a toy application project from The Odin Project. My goal with this project is to really focus on replicating Facebook functionality. This goal will hopefully result in me learning more about Rails than I know about my backside.

Users

  • Name
  • Email
  • Encrypted_password
  • Birthday – Date
  • Avatar
class User < ApplicationRecord
  has_many :friendships
  has_many :friends, :through => :friendships,
                       :source => :friend

  has_many :posts
end

Friendship

  • user_id – Integer
  • friend_id – Integer
class Friendship < ApplicationRecord
  belongs_to :user
  belongs_to :friend, class_name: 'User'
end

Post

  • content
  • image
class Post < ApplicationRecord
  belongs_to :user
end
Categories
Uncategorized

How to Setup PostgreSQL (PG) for Deploying Rails Applications to Heroku

First, install PostgreSQL.

sudo apt-get install postgresql libpq-dev

Create a user with the same name as your computer. Mine is greg.

sudo -u postgres createuser greg

After you create your user, log into the PostgreSQL console under the ‘postgres’ account. This account was created be default when you installed the software.

Name your user whatever is before the “@”.
sudo -u postgres psql

Now we simply make our user a SUPERUSER.

postgres=# ALTER USER greg WITH SUPERUSER;
postgres=# \du
postgres=# ^Z

Now you have a SUPERUSER setup in PostgreSQL that matches the default username Rails will look for when doing anything with a PostgreSQL databases.

For each rails application…

You will need to create both a development and testing database. Be sure that you are creating these databases from under the SUPERUSER you just created.

Simply plug in your own user and application name where I user ‘greg’ & ‘local-flickr’.

sudo -u greg createdb local-flickr_development
sudo -u greg createdb local-flickr_test

Categories
Uncategorized

College todo list

Fall 2019 – (11 credit hours)

CodeTitleScheduleContactBook
CIS 205C++ Prog II (3) Wednesday 6:00 PM – 9:55 pfifer@ncmich.edu View

Exercises
IT 102NET ESSENTIALS (2)ONLINE fharrington@ncmich.edu
CIS 120Intro to PC OS (3)DIRECTED mmalleis@ncmich.edu
CIS 141WEB DEV II – JS (3)DIRECTED mmalleis@ncmich.edu
Categories
Uncategorized

How to setup a “has_many :through association” in Rails 5

I am writing this short article in hopes that I fully understand how to do a has_many relationship in Ruby on Rails. I am no pro.

A has_many :through association is often used to set up a many-to-many connection with another model. This association indicates that the declaring model can be matched with zero or more instances of another model by proceeding through a third model.

For example, consider the case of Users, Events and RSVP’s. A user can create an event, and also attend events. This will require 3 data models. Rails is particular on how to setup this up so I will explain it simply.

Its important to recall that each data model must has a corresponding table. Sense RSVP can be seen as the relationship between the two models, it will contain just the id of each model.

First we must create a migration to add the RSVP table.

> rails generate migration CreateRsvps attended_event_id:integer attendee:integer

In our migration, we will add each column as an index in both our RSVP and Events table.

db/migrate/20190717191812_create_rsvps.rb

class CreateRsvps < ActiveRecord::Migration[5.2]
  def change
    create_table :rsvps do |t|
      t.integer :attended_event_id
      t.integer :attendee_id

      t.timestamps
    end

    add_foreign_key :rsvps, :events, :column => :attended_event_id
    add_index :rsvps, :attended_event_id

    add_foreign_key :rsvps, :events, :column => :attendee_id
    add_index :rsvps, :attendee_id
  end
end

Next we create our associations between our 3 models.

RSVP.rb

The RSVP model is our relationship table. Wthin our RSVP model we define which column refers to which model.

class Rsvp < ApplicationRecord
    belongs_to :attendee, class_name: 'User'
    belongs_to :attended_event, class_name: 'Event'
end

User.rb

Next, we setup our relationship from Users to RSVP’s. In any through: table, you must first reference which column in the RSVP table your User is a foreign_key to. After we define our RSVP relationship, we can create a attended_events relationship which will return the data from the :attended_event column (referenced above) in each row where our User.id is in the :attendee column.

class User < ApplicationRecord
  has_many :rsvps, foreign_key: 'attendee_id'
  has_many :attended_events, :through => :rsvps,
                               :source => :attended_event
end

Event.rb

Here, we do the same thing as above but in reverse. Now you can do something like Event.first.attendees to return all users who are attending the event. Again, first we declare which column references the event in the Event > RSVP relationship. After that, we create the :attendees relationship which simply returns all the :attendees in the RSVPS relationship.

class Event < ApplicationRecord
    has_many :rsvps, foreign_key: 'attended_event_id'
    has_many :attendees, :through => :rsvps,
                         :source => :attendee
end
Categories
Uncategorized

How to get all users who purchased a product by ID – Woocommerce

So, I needed a query which would return a list of user ID’s who purchased a certain product.

function msp_get_customers_who_purchased_product( $product_id ){
    global $wpdb;
    $order_item = $wpdb->prefix . 'woocommerce_order_items';
    $order_item_meta = $wpdb->prefix . 'woocommerce_order_itemmeta';

    $sql = "SELECT DISTINCT u.ID
            FROM $wpdb->users u, $wpdb->posts p, $order_item i, $order_item_meta meta
            WHERE p.post_type = 'shop_order'
            AND p.post_status = 'wc-completed'
            AND p.ID = i.order_id
            AND i.order_item_type = 'line_item'
            AND i.order_item_id = meta.order_item_id
            AND meta.meta_key = '_product_id'
            AND meta.meta_value = $product_id";
            
    return $wpdb->get_results( $sql );
}