Advertisement
Guest User

WP Plugin Upload

a guest
Aug 19th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.40 KB | None | 0 0
  1. # WordPress Plugin Uploader
  2. # Version 0.1
  3. # By Brandon Hammond
  4.  
  5. # Import required modules
  6. import os
  7. import sys
  8. import time
  9. import logging
  10. import getopt
  11. import ftplib
  12. from wordpress import API
  13.  
  14. # Specify the version and author
  15. __version__ = "0.1"
  16. __author__ = "Brandon Hammond"
  17.  
  18. # Define the activate_plugin() function
  19. def activate_plugin():
  20.     # Authenticate with the API
  21.     wpapi = API(
  22.         url="",
  23.         consumer_key="", # Public API key
  24.         consumer_secret="", # Private API key
  25.         api="wp-json",
  26.         version="wp/v2",
  27.         wp_user="", # WordPress username
  28.         wp_pass="", # WordPress password
  29.         oauth1a_3leg=True,
  30.         creds_store="~/.wc-api-creds.json"
  31.     )
  32.    
  33.     # Specify the headers
  34.     headers = {}
  35.    
  36.     # Specify the endpoint and request
  37.     endpoint = ""
  38.     request = ""
  39.    
  40.    
  41.  
  42. # Define the upload_plugin() function
  43. def upload_plugin(plugin, ftp):
  44.     # Function: upload_plugin()
  45.     # Purpose: Upload the WordPress plugin
  46.     # Arguments:
  47.     #   plugin - The path to the folder of the WordPress plugin
  48.     #   ftp - The FTP object
  49.     # Returns: None
  50.    
  51.     # List the files and change directory
  52.     files = os.listdir()
  53.     os.chdir(plugin)
  54.    
  55.     # For every file found
  56.     for f in files:
  57.         # If f is a file
  58.         if os.path.isfile(path + r'\{}'.format(f)):
  59.             # Open it as binary and store it
  60.             fh = open(f, 'rb')
  61.             myFTP.storbinary('STOR %s' % f, fh)
  62.             fh.close()
  63.            
  64.         # If f is a directory
  65.         elif os.path.isdir(path + r'\{}'.format(f)):
  66.         # Make a directory on the FTP server and change to it, then recursively call upload_plugin()
  67.             myFTP.mkd(f)
  68.             myFTP.cwd(f)
  69.             upload_plugin(path + r'\{}'.format(f))
  70.     ftp.cwd('..')
  71.     os.chdir('..')
  72.  
  73. # Define the main() function
  74. def main():
  75.     # Function: main()
  76.     # Purpose: Process command line arguments and control base program flow
  77.     # Arguments: None
  78.     # Returns: None
  79.    
  80.     # Attempt to process command line arguments with getopt()
  81.     try:
  82.         opts, args = getopt.getopt(sys.argv[1:], "hvp:t:f:u:k:", ("help", "version", "plugin=", "plugin-path=", "ftp=", "username=", "password="))
  83.     except getopt.GetoptError:
  84.         # Display the error message and exit
  85.         print("[E] Error processing command line arguments: {}!")
  86.         exit(0)
  87.        
  88.     # Define important variables and set their default values
  89.     plugin = None
  90.     plugin_path = None
  91.     ftp_server = None
  92.     username = None
  93.     password = None
  94.    
  95.     # Build a for loop to process command line arguments
  96.     for opt, arg in opts:
  97.         # If the -h or --help command line argument is used
  98.         if opt in ("-h", "--help"):
  99.             # Display the help message and exit
  100.             print("USAGE:")
  101.             print("\twp-plugin-upload [-h] [-v] [-p PLUGIN] [-t PLUGIN PATH] [-f FTP SERVER] [-u USERNAME] [-k PASSWORD]")
  102.             print("")
  103.             print("A simple script to upload and install a WordPress plugin")
  104.             print("")
  105.             print("REQUIRED ARGUMENTS:")
  106.             print("\t-p, --plugin\tThe path to the plugin you want to install")
  107.             print("\t-t, --plugin-path\tThe path to the plugins directory on WordPress")
  108.             print("\t-f, --ftp\tSpecify the FTP server to use")
  109.             print("\t-u, --username\tThe username for the FTP server")
  110.             print("\t-k, --password\tThe password for the FTP server")
  111.             print("")
  112.             print("OPTIONAL ARGUMENTS:")
  113.             print("\t-h, --help\tDisplay the help message and exit")
  114.             print("\t-v, --version\tDisplay the version message and exit")
  115.             exit(0)
  116.            
  117.         # If the -v or --version command line argument is used
  118.         elif opt in ("-v", "--version"):
  119.             # Display the version message and exit
  120.             print("WordPress Plugin Upload")
  121.             print("Version {}".format(__version__))
  122.             print("By {}".format(__author__))
  123.             exit(0)
  124.            
  125.         # If the -p or --plugin command line argument is used
  126.         elif opt in ("-p", "--plugin"):
  127.             # Specify the plugin path
  128.             plugin = arg
  129.            
  130.         # If the -t or --plugin-path command line argument is used
  131.         elif opt in ("-t", "--plugin-path"):
  132.             # Specify the installation path on the server
  133.             plugin_path = arg
  134.            
  135.         # If the -f or --ftp command line argument is used
  136.         elif opt in ("-f", "--ftp"):
  137.             # Specify the FTP server to use
  138.             ftp_server = arg
  139.            
  140.         # If the -u or --username command line argument is used
  141.         elif opt in ("-u", "--username"):
  142.             # Specify the username
  143.             username = arg
  144.            
  145.         # If the -k or --password command line argument is used
  146.         elif opt in ("-k", "--password"):
  147.             # Specify the password
  148.             password = arg
  149.            
  150.        
  151.    
  152.     # Connect to the FTP server
  153.     ftp = FTP(ftp_server)
  154.     ftp.login(user=username, passwd=password)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement